I have the following implementation for decrypting a value. This implementation works, but in some small case the values I'm attempting to decrypt are throwing a Cryptographic Exception, with the message 'The Parameter is incorrect'. This occurs where I call the Decrypt method on the private key below.
All values are encrypted with the public key, base64 encoded in transit, and passed along to this method the same way, so I don't understand why it would be blowing up sometimes. If I re-encrypt with the public key, that new value can usually be unencrypted successfully.
Certificate in this case below is the X509Certificate2
public string Decrypt(CertificateType certificateType, byte[] encryptedString)
{
string result = null;
var certificate = GetCertificate(certificateType);
var privateKey = certificate?.GetRSAPrivateKey();
if (privateKey != null)
{
var decryptedBytes = privateKey.Decrypt(encryptedString, RSAEncryptionPadding.Pkcs1);
result = Encoding.Default.GetString(decryptedBytes);
}
return result;
}
Any ideas as to what would cause that exception?