简体   繁体   中英

RSA RSACryptoServiceProvider in .NET Compact Framework using public key from full .NET Framework

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM