public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
{
if (data == null)
throw new ArgumentNullException(nameof(data));
if (padding == null)
throw new ArgumentNullException(nameof(padding));
Interop.Crypto.RsaPadding rsaPadding = GetInteropPadding(padding);
SafeRsaHandle key = _key.Value;
CheckInvalidKey(key);
byte[] buf = new byte[Interop.Crypto.RsaSize(key)];
int returnValue = Interop.Crypto.RsaPrivateDecrypt(
data.Length,
data,
buf,
key,
rsaPadding);
CheckReturn(returnValue);
// If the padding mode is RSA_NO_PADDING then the size of the decrypted block
// will be RSA_size, so let's just return buf.
//
// If any padding was used, then some amount (determined by the padding algorithm)
// will have been reduced, and only returnValue bytes were part of the decrypted
// body, so copy the decrypted bytes to an appropriately sized array before
// returning it.
if (returnValue == buf.Length)
{
return buf;
}
byte[] plainBytes = new byte[returnValue];
Buffer.BlockCopy(buf, 0, plainBytes, 0, returnValue);
return plainBytes;
}