Freigeben über


RSACryptoServiceProvider.Decrypt-Methode

Entschlüsselt Daten mit dem RSA-Algorithmus.

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

Syntax

'Declaration
Public Function Decrypt ( _
    rgb As Byte(), _
    fOAEP As Boolean _
) As Byte()
'Usage
Dim instance As RSACryptoServiceProvider
Dim rgb As Byte()
Dim fOAEP As Boolean
Dim returnValue As Byte()

returnValue = instance.Decrypt(rgb, fOAEP)
public byte[] Decrypt (
    byte[] rgb,
    bool fOAEP
)
public:
array<unsigned char>^ Decrypt (
    array<unsigned char>^ rgb, 
    bool fOAEP
)
public byte[] Decrypt (
    byte[] rgb, 
    boolean fOAEP
)
public function Decrypt (
    rgb : byte[], 
    fOAEP : boolean
) : byte[]

Parameter

  • rgb
    Die zu entschlüsselnden Daten.
  • fOAEP
    true, um eine direkte RSA-Entschlüsselung mit OAEP-Padding (nur auf Computern mit einem Betriebssystem ab Microsoft Windows XP verfügbar) auszuführen, andernfalls false, um PKCS#1-Padding, Version 1.5, zu verwenden.

Rückgabewert

Die entschlüsselten Daten, d. h. der ursprüngliche Klartext, der vor der Verschlüsselung vorlag.

Ausnahmen

Ausnahmetyp Bedingung

CryptographicException

Der CSP kann nicht ermittelt werden.

– oder –

Der fOAEP-Parameter ist true, und die Länge des rgb-Parameters ist größer als KeySize.

– oder –

Der fOAEP-Parameter ist true, und OAEP wird nicht unterstützt.

Hinweise

Verwenden Sie Encrypt, um Daten so zu verschlüsseln, dass sie mit dieser Methode entschlüsselt werden können.

Beispiel

Im folgenden Codebeispiel werden Daten verschlüsselt und entschlüsselt.

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

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

}
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

.NET Framework-Sicherheit

Plattformen

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

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0

Siehe auch

Referenz

RSACryptoServiceProvider-Klasse
RSACryptoServiceProvider-Member
System.Security.Cryptography-Namespace

Weitere Ressourcen

Kryptografische Dienste