Export (0) Print
Expand All

RSACryptoServiceProvider.Decrypt Method

Decrypts data with the RSA algorithm.

Namespace: System.Security.Cryptography
Assembly: mscorlib (in mscorlib.dll)

public byte[] Decrypt (
	byte[] rgb,
	bool fOAEP
)
public byte[] Decrypt (
	byte[] rgb, 
	boolean fOAEP
)
public function Decrypt (
	rgb : byte[], 
	fOAEP : boolean
) : byte[]
Not applicable.

Parameters

rgb

The data to be decrypted.

fOAEP

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

The decrypted data, which is the original plain text before encryption.

Exception typeCondition

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.

ArgumentNullException

rgb is a null reference (Nothing in Visual Basic).

Use Encrypt to encrypt data for decryption with this method.

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 System;
using System.Security.Cryptography;
using System.Text;

class RSACSPSample
{
    static void Main()
    {
        try
        {
            //Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding ByteConverter = new ASCIIEncoding();

            string dataString = "Data to Encrypt";

            //Create byte arrays to hold original, encrypted, and decrypted data.
            byte[] dataToEncrypt = ByteConverter.GetBytes(dataString);
            byte[] encryptedData;
            byte[] decryptedData;

            //Create a new instance of the RSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            RSACryptoServiceProvider RSAalg = new 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);

        }
    }
}

import System.*;
import System.Security.Cryptography.*;
import System.Text.*;

class RSACSPSample
{
    public static void main(String[] args)
    {
        try {
            // Create a UnicodeEncoder to convert between byte array and string.
            ASCIIEncoding byteConverter = new ASCIIEncoding();
            String dataString = "Data to Encrypt";

            // Create byte arrays to hold original, encrypted, and decrypted 
            // data.
            ubyte dataToEncrypt[] = byteConverter.GetBytes(dataString);
            ubyte encryptedData[];
            ubyte decryptedData[];

            // Create a new instance of the RSACryptoServiceProvider class 
            // and automatically create a new key-pair.
            RSACryptoServiceProvider rsaAlg = new 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.get_Message());
        }
    } //main
} //RSACSPSample

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft