Partilhar via


RSACryptoServiceProvider.Decrypt Método

Definição

Descriptografa dados que foram criptografados anteriormente.

Sobrecargas

Nome Description
Decrypt(Byte[], Boolean)
Obsoleto.

Descriptografa dados com o RSA algoritmo.

Decrypt(Byte[], RSAEncryptionPadding)

Descriptografa dados que foram criptografados anteriormente com o RSA algoritmo usando o preenchimento especificado.

Decrypt(Byte[], Boolean)

Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs

Cuidado

RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead.

Descriptografa dados com o RSA algoritmo.

public:
 cli::array <System::Byte> ^ Decrypt(cli::array <System::Byte> ^ rgb, bool fOAEP);
public byte[] Decrypt(byte[] rgb, bool fOAEP);
[System.Obsolete("RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead.", DiagnosticId="SYSLIB0064", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public byte[] Decrypt(byte[] rgb, bool fOAEP);
override this.Decrypt : byte[] * bool -> byte[]
[<System.Obsolete("RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead.", DiagnosticId="SYSLIB0064", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
override this.Decrypt : byte[] * bool -> byte[]
member this.Decrypt : byte[] * bool -> byte[]
Public Function Decrypt (rgb As Byte(), fOAEP As Boolean) As Byte()

Parâmetros

rgb
Byte[]

Os dados a serem descriptografados.

fOAEP
Boolean

true para executar a descriptografia direta RSA usando o preenchimento OAEP; caso contrário, false para usar o preenchimento PKCS nº 1 v1.5.

Retornos

Byte[]

Os dados descriptografados, que é o texto sem formatação original antes da criptografia.

Atributos

Exceções

O CSP (provedor de serviços criptográficos) não pode ser adquirido.

- ou -

O fOAEP parâmetro é true e o comprimento do rgb parâmetro é maior que KeySize.

- ou -

A chave não corresponde aos dados criptografados. No entanto, a redação de exceção pode não ser precisa. Por exemplo, ele pode dizer Not enough storage is available to process this command.

rgb é null.

Exemplos

O exemplo de código a seguir criptografa e descriptografa dados.

Este exemplo usa a ASCIIEncoding classe; no entanto, a UnicodeEncoding classe pode ser preferível em operações de dados grandes. O valor criptografado pode ser salvo como um tipo de dados nvarchar no Microsoft SQL Server.

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

Comentários

Use Encrypt para criptografar dados para descriptografia com esse método.

Confira também

Aplica-se a

Decrypt(Byte[], RSAEncryptionPadding)

Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs
Origem:
RSACryptoServiceProvider.Unix.cs

Descriptografa dados que foram criptografados anteriormente com o RSA algoritmo usando o preenchimento especificado.

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()

Parâmetros

data
Byte[]

Os dados a serem descriptografado.

padding
RSAEncryptionPadding

O preenchimento.

Retornos

Byte[]

Os dados descriptografados.

Exceções

data é null.

- ou -

padding é null.

Não há suporte para o modo de preenchimento.

Comentários

padding deve ser RSAEncryptionPadding.Pkcs1 ou RSAEncryptionPadding.OaepSHA1.

Aplica-se a