简体   繁体   中英

Can RSACryptoServiceProvider.Decrypt() return incorrect data?

Can System.Security.Cryptography.RSACryptoServiceProvider.Decrypt() output incorrect data? Or will it always throw Exception with message "The parameter is incorrect" or any other Exception, whether it's unable to decrypt in a proper way?

Situation is:

  1. I generate RSA pair using RSACryptoServiceProvider.ExportParameters(true)
  2. I send modulus and exponent to the other side
  3. I receive encrypted data
  4. I successfully decrypt data with the same RSA parameters without any errors, but get data in length of 30 bytes
  5. The other side does state, that plain data was of 32 bytes length

Is this even possible? What am I missing here?

using System.Security.Cryptography;

var provider = new RSACryptoServiceProvider(2048);
var rsaParams = provider.ExportParameters(true);
rsaParams.Exponent = new byte[] { 0x65 };

byte[] publicKey = rsaParams.Modulus;

// Send public key to the other side with exponent 0x65, 
// Decrypt received data

byte[] decryptedData = provider.Decrypt(encryptedData, false);

You cannot just replace the public exponent like you do after key generation (which happens in the constructor). By default a public exponent is likely set to 65537 (fifth prime of Fermat, F4) and you just set it to 0x65 . If the public and private key do not match then the calculations will fail.

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