次の方法で共有


RSACryptoServiceProvider.Decrypt メソッド

RSA アルゴリズムでデータを復号化します。

Public Function Decrypt( _
   ByVal rgb() As Byte, _   ByVal fOAEP As Boolean _) As Byte()
[C#]
public byte[] Decrypt(byte[] rgb,boolfOAEP);
[C++]
public: unsigned char Decrypt(unsigned charrgb __gc[],boolfOAEP)  __gc[];
[JScript]
public function Decrypt(
   rgb : Byte[],fOAEP : Boolean) : Byte[];

パラメータ

  • rgb
    復号化するデータ。
  • fOAEP
    Microsoft Windows XP 以降を実行するコンピュータだけで使用できる OAEP パディングを使用して、直接 RSA 復号化を実行する場合は true 。それ以外の場合は、PKCS#1 Version 1.5 パディングを使用することを表す false

戻り値

復号化されたデータ。暗号化する前の、元の平文です。

例外

例外の種類 条件
CryptographicException 暗号サービス プロバイダ (CSP) を取得できません。

または

fOAEP パラメータが true であり、 rgb パラメータの長さが KeySize を超える値です。

または

fOAEP パラメータが true で、OAEP がサポートされていません。

解説

このメソッドを使用して復号化できるデータを暗号化するには、 Encrypt を使用します。

使用例

[Visual Basic, C#] Decrypt メソッドを使用する方法を次の例に示します。

 
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

[C#] 
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);

        }
    }
}

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

RSACryptoServiceProvider クラス | RSACryptoServiceProvider メンバ | System.Security.Cryptography 名前空間 | 暗号サービス