RSACryptoServiceProvider::Decrypt Method
Decrypts data with the RSA algorithm.
Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)
Parameters
- rgb
- Type: array<System::Byte>
The data to be decrypted.
- fOAEP
- Type: System::Boolean
true to perform direct RSA decryption using OAEP padding (only available on a computer running Microsoft Windows XP or later); otherwise, false to use PKCS#1 v1.5 padding.
Return Value
Type: array<System::Byte>The decrypted data, which is the original plain text before encryption.
| Exception | Condition |
|---|---|
| CryptographicException | The cryptographic service provider (CSP) cannot be acquired. -or- The fOAEP parameter is true and the length of the rgb parameter is greater than KeySize. -or- The fOAEP parameter is true and OAEP is not supported. -or- The key does not match the encrypted data. However, the exception wording may not be accurate. For example, it may say Not enough storage is available to process this command. |
| ArgumentNullException | rgb is nullptr. |
The following code example encrypts and decrypts data.
This example uses the ASCIIEncoding class; however, the UnicodeEncoding class may be preferable in large data operations. The encrypted value can be saved as an nvarchar data type in Microsoft SQL Server 2005.
using namespace System; using namespace System::Security::Cryptography; using namespace System::Text; int main() { try { //Create a UnicodeEncoder to convert between byte array and string. ASCIIEncoding^ ByteConverter = gcnew ASCIIEncoding; String^ dataString = "Data to Encrypt"; //Create byte arrays to hold original, encrypted, and decrypted data. array<Byte>^dataToEncrypt = ByteConverter->GetBytes( dataString ); array<Byte>^encryptedData; array<Byte>^decryptedData; //Create a new instance of the RSACryptoServiceProvider class // and automatically create a new key-pair. RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider; //Display the origianl data to the console. Console::WriteLine( "Original Data: {0}", dataString ); //Encrypt the byte array and specify no OAEP padding. //OAEP padding is only available on Microsoft Windows XP or //later. encryptedData = RSAalg->Encrypt( dataToEncrypt, false ); //Display the encrypted data to the console. Console::WriteLine( "Encrypted Data: {0}", ByteConverter->GetString( encryptedData ) ); //Pass the data to ENCRYPT and boolean flag specifying //no OAEP padding. decryptedData = RSAalg->Decrypt( encryptedData, false ); //Display the decrypted plaintext to the console. Console::WriteLine( "Decrypted plaintext: {0}", ByteConverter->GetString( decryptedData ) ); } catch ( CryptographicException^ e ) { //Catch this exception in case the encryption did //not succeed. Console::WriteLine( e->Message ); } }
- KeyContainerPermissionAccessEntryCollection
for permission to decrypt. Security action: Demand. Associated enumeration: KeyContainerPermissionFlags::Decrypt
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.