3

In my work we are doing encryption to protect data and that data is encrypted by the .NET Compact Framework and must be read by the regular .NET framework on a server. We are running into an issue where the compact framework is unable to encrypt (throwing exception) using RSA with a known public key. The server passes the public key to the compact framework device. Below is a test app written for the compact framework to show the problem.

string mod = 
    "rgTcL0/ZK3j5Rt6CigEsfyLDiERh2PuVzmZVdHbb/2jQOG5JEcAqqBoscDZ4PwJR8aO19xNVTce7"
  + "vzbEued32z2PLAvCcHFKGtOgNEeZ+ZcD6uHobsKws76BdjBrI7Pigk2HSkak21n2WoVcBVHoRmcn"
  + "eX7DPaB4atamhkbLoRBF1VlautDfhX9lnOFA2zyZUCB5CproavKF6wl19pZne2Q4U1vMtBAA2Q9N"
  + "aZFsrj/KjE3UtYKvjd4Oy55Hmtpb5P3CZAVpiyCTKq3gTxDJn69giyctu428DgkKacmZ4yTvkLWB"
  + "Ym/zWtAf9o8pI+3MwgF7wzuK5ypGack3l4/Skw==";

string exp = "AQAB";

RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048);

RSAParameters p = new RSAParameters();
p.Modulus = Convert.FromBase64String(mod);
p.Exponent = Convert.FromBase64String(exp);

rsa.ImportParameters(p);

var bytes = rsa.Encrypt(System.Text.Encoding.ASCII.GetBytes("MIKE"), true);

This code produces the following exception when the "Encrypt" method is called:

Framework: 3.5.7283.0
Exception: fOAEP 
InnerException: Could not evaluate expression

Stack Trace:
   at System.Security.Cryptography.RSACryptoServiceProvider.Encrypt
    (Byte[] rgb, Boolean fOAEP)

Does anyone know anything else I should try/do? I have written this code in regular .NET and it works just fine. I can encrypt and decrypt using different instances. Any help would be appreciated.

Thanks!

ctacke
  • 66,480
  • 18
  • 94
  • 155
Mike Nicholson
  • 105
  • 2
  • 5
  • You don't Encrypt() with a public key, you Sign(). I'm guessing that is the issue. – H H May 27 '11 at 13:04
  • 3
    @Henk: Actually, you do encrypt with a public key. You sign with a private key. (The point being that only the holder of the private key can read the encrypted data, but anyone can verify a signature.) – Nicole Calinoiu May 27 '11 at 13:10
  • @Nicole: You're right, I was sleepreading. – H H May 27 '11 at 14:49

1 Answers1

5

Use of a true value for the fOAEP parameter is not supported in the Compact Framework (at least in version 3.5). For details of the parameter, see http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.encrypt.aspx.

In CF 3.5, support for fOAEP = true does not appear to be device-dependent. Instead, rejection of a true value is hard-coded as a parameter validation in the Encrypt method.

Nicole Calinoiu
  • 20,843
  • 2
  • 44
  • 49