Mono Class Library: System.Security.Cryptography.RSACryptoServiceProvider Overview | Members

System.Security.Cryptography.RSACryptoServiceProvider.Decrypt Method

Decrypt the provided data using the private key and the specified padding mechanism. [Edit]

public byte[] Decrypt (byte[] rgb, bool fOAEP)

Parameters

rgb
The encrypted data. [Edit]
fOAEP
True to use the OAEP padding, false to use PKCS#1 padding. [Edit]

Returns

An array of bytes containing the decrypted data. [Edit]

Remarks

RSA isn't normally used to do bulk encryption. The two main reasons are length restriction (public key size and padding limit the maximum length of data that can be encrypted in a single operation) and performance (as RSA is very slow compared to most symmetric ciphers).

The following example shows how to combine the use of RSA and a symmetric cipher, Rjindael, to "quickly" decrypt data of an unlimited length (provided is was encrypted with the reversed algorithm).

C# Example
static byte[] Decrypt (RSA rsa, byte[] input) 
{
     // by default this will create a 128 bits AES (Rijndael) object
     SymmetricAlgorithm sa = SymmetricAlgorithm.Create ();

     byte[] keyex = new byte [rsa.KeySize >> 3];
     Buffer.BlockCopy (input, 0, keyex, 0, keyex.Length);

     RSAPKCS1KeyExchangeDeformatter def = new RSAPKCS1KeyExchangeDeformatter (rsa);
     byte[] key = def.DecryptKeyExchange (keyex);

     byte[] iv = new byte [sa.IV.Length];
     Buffer.BlockCopy (input, keyex.Length, iv, 0, iv.Length);

     ICryptoTransform ct = sa.CreateDecryptor (key, iv);
     byte[] decrypt = ct.TransformFinalBlock (input, keyex.Length + iv.Length, input.Length - (keyex.Length + iv.Length));
     return decrypt;
}
  

Interoperability warning: Microsoft CryptoAPI only supports OAEP since Windows XP.

[Edit]

Requirements

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0