1

I'm using the RSACryptoServiceProvider to encrypt and decrypt simple strings and while it works with normal certificates, it's giving me some grief when it comes to smart cards.

Here is the code I'm using:

private X509Certificate2 _cert // this certificate is set early on in the program

private string Encrypt(string Data)
{
    if (string.IsNullOrEmpty(Data)) return default(string);

    RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)_cert.PublicKey.Key;
    return Convert.ToBase64String(((rsa.Encrypt(Encoding.Unicode.GetBytes(Data), true))));
}

private string Decrypt(string CipherText)
{
    if (string.IsNullOrEmpty(CipherText)) return default(string);

    RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)_cert.PrivateKey;
    return Encoding.Unicode.GetString(((rsa.Decrypt(Convert.FromBase64String(CipherText), true))));
}

It does decrypt successfully (no errors) but the encoding isn't correct. It's giving me results like

\u08d8黔㡉Ẑ༴쨳층器\u0888諬翉烽偪䚘螷퓰薑낯ꄯ鯪ꘇ台ᾨ鳞텟칆蘟マ⺁ൿ䤳譻宐Ṹ鉱㒎艴偃堎え뢈癘蚰૩�⸮賆슉ଞ맿댿䀵㓹摵�뼚⡨ቾ᳓낣쏀ꖌ엷ὦ楐豎⸌ꅑ뙳餱Ч鎋筧粅嚄罜칮嬒쐞ڮ묭泊䘐쫦⊗邀☇仇挃箍絁绺罽华⏓፦귪ﻳ咷믭鹺簽艉闼敹Ԓ嵯젨泪ꔤ狫ꆙ\uab41軧\n"

What's also interesting is no matter what I encrypt, the decrypted byte[] length is always 256.

I know smart card private key operations always take place on the card itself so I'm sure that has something to do with it but I was hoping some of you have experience with this and can save me the mounds of trial-and-error.

Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Brian Mitchell
  • 849
  • 1
  • 8
  • 24
  • What about RSA message length limitations? If you try to encrypt/decrypt long messages, you can face some troubles: http://stackoverflow.com/a/8418238/3899583 – vojta Aug 07 '15 at 14:50
  • Length was one of the things I checked. My key size is 2048 and my plain text is simply the word "test" (no quotes). – Brian Mitchell Aug 07 '15 at 21:04

1 Answers1

0

It seems your Smart Card performs so called raw or textbook RSA. In other words, it only performs modular exponentiation, leaving the PKCS#1 v1.5 (EME-PKCS1-v1_5) or - in your case - OAEP (EME-OAEP decoding) decoding up to you. This padding has the same size as the key size / modulus size. In your case that would be 256 bytes or 2048 bits.

So something is very wrong with the Smart Card that's used. Unpadding should be an integral part of the functionality provided by it; it makes little sense to do that on the PC.

The padding largely consists of random (looking) data so it's little wonder that it looks like random text, especially now UTF-16 LE is being used (incorrectly called Unicode by .NET).

Community
  • 1
  • 1
Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • I agree that something is going on inside the Smart Card and your post makes sense to the constant length I'm getting. I use this same code on an HSM protected certificate and it works perfectly. The cards I'm using are Gemalto ID Prime (.NET). I'm dumping CAPI and working with CNG and as soon as I get to the Smart Card KSP I'm going to swing back around on try to get this working. – Brian Mitchell Aug 19 '15 at 01:53