RSACryptoServiceProvider.Decrypt Metodo

Definizione

Decrittografa i dati crittografati in precedenza.

Overload

Decrypt(Byte[], Boolean)

Decrittografa i dati con l'algoritmo RSA.

Decrypt(Byte[], RSAEncryptionPadding)

Decrittografa i dati precedentemente crittografati con l'algoritmo RSA usando il riempimento specificato.

Decrypt(Byte[], Boolean)

Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs

Decrittografa i dati con l'algoritmo RSA.

public:
 cli::array <System::Byte> ^ Decrypt(cli::array <System::Byte> ^ rgb, bool fOAEP);
public byte[] Decrypt (byte[] rgb, bool fOAEP);
override this.Decrypt : byte[] * bool -> byte[]
member this.Decrypt : byte[] * bool -> byte[]
Public Function Decrypt (rgb As Byte(), fOAEP As Boolean) As Byte()

Parametri

rgb
Byte[]

Dati da decrittografare.

fOAEP
Boolean

true per eseguire la decrittografia diretta di RSA tramite il riempimento OAEP; in caso contrario, false per usare il riempimento di PKCS#1 v1.5.

Restituisce

Byte[]

Dati decrittografati, ovvero il testo normale originale prima della crittografia.

Eccezioni

Non è possibile acquisire il provider del servizio di crittografia (CSP).

-oppure-

Il parametro fOAEP è true e la lunghezza del parametro rgb è maggiore di KeySize.

-oppure-

La chiave non corrisponde ai dati crittografati. Tuttavia, la formulazione dell'eccezione potrebbe non essere accurata. Ad esempio, potrebbe indicare Not enough storage is available to process this command.

rgb è null.

Esempio

L'esempio di codice seguente crittografa e decrittografa i dati.

In questo esempio viene utilizzata la ASCIIEncoding classe , ma la UnicodeEncoding classe può essere preferibile nelle operazioni di dati di grandi dimensioni. Il valore crittografato può essere salvato come nvarchar tipo di dati in Microsoft SQL Server.

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 );
   }

}
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);
        }
    }
}
Imports System.Security.Cryptography
Imports System.Text

Module RSACSPExample

    Sub Main()
        Try
            'Create a UnicodeEncoder to convert between byte array and string.
            Dim ByteConverter As New ASCIIEncoding

            Dim dataString As String = "Data to Encrypt"

            'Create byte arrays to hold original, encrypted, and decrypted data.
            Dim dataToEncrypt As Byte() = ByteConverter.GetBytes(dataString)
            Dim encryptedData() As Byte
            Dim decryptedData() As Byte

            'Create a new instance of the RSACryptoServiceProvider class 
            ' and automatically create a new key-pair.
            Dim RSAalg As 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 e As CryptographicException
            'Catch this exception in case the encryption did
            'not succeed.
            Console.WriteLine(e.Message)
        End Try
    End Sub 

End Module

Commenti

Usare Encrypt per crittografare i dati per la decrittografia con questo metodo.

Vedi anche

Si applica a

Decrypt(Byte[], RSAEncryptionPadding)

Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs
Source:
RSACryptoServiceProvider.Unix.cs

Decrittografa i dati precedentemente crittografati con l'algoritmo RSA usando il riempimento specificato.

public:
 override cli::array <System::Byte> ^ Decrypt(cli::array <System::Byte> ^ data, System::Security::Cryptography::RSAEncryptionPadding ^ padding);
public override byte[] Decrypt (byte[] data, System.Security.Cryptography.RSAEncryptionPadding padding);
override this.Decrypt : byte[] * System.Security.Cryptography.RSAEncryptionPadding -> byte[]
Public Overrides Function Decrypt (data As Byte(), padding As RSAEncryptionPadding) As Byte()

Parametri

data
Byte[]

Dati da decrittografare.

padding
RSAEncryptionPadding

Riempimento.

Restituisce

Byte[]

Dati decrittografati.

Eccezioni

data è null.

-oppure-

padding è null.

La modalità di riempimento non è supportata.

Commenti

padding deve essere RSAEncryptionPadding.Pkcs1 o RSAEncryptionPadding.OaepSHA1.

Si applica a