次の方法で共有


RSACryptoServiceProvider.Encrypt メソッド

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

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

パラメータ

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

戻り値

暗号化されたデータ。

例外

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

または

rgb パラメータの長さが許可された最大長を超えています。

または

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

解説

Microsoft Windows の各バージョンがサポートしているパディングと、オペレーティング システムとパディングの各組み合わせで許可される rgb の最大長の一覧表を次に示します。

パディング サポートされるオペレーティング システム rgb パラメータの最大長
OAEP パディング (PKCS#1 v2) Microsoft Windows XP 以降。 剰余サイズ -2 -2*hLen (hLen はハッシュのサイズ)。
Direct Encryption (PKCS#1 v1.5) 高度暗号化パックがインストールされている Microsoft Windows 2000 以降。 剰余サイズ - 11 (11 バイトは最小パディング サイズ)。
Direct Encryption、OAEP パディングともにサポートなし Microsoft Windows 98、Microsoft Windows Millennium、および高度暗号化パックがインストールされていない Windows 2000 以降。 対称キーで許容される最大サイズ。

このメソッドの結果を復号化するには、 Decrypt を使用します。

使用例

 
Imports System
Imports System.Security.Cryptography

Class RSACSPSample

    Shared Sub Main()
        Try
            'initialze the byte arrays to the public key information.
            Dim PublicKey As Byte() = {214, 46, 220, 83, 160, 73, 40, 39, 201, 155, 19, 202, 3, 11, 191, 178, 56, 74, 90, 36, 248, 103, 18, 144, 170, 163, 145, 87, 54, 61, 34, 220, 222, 207, 137, 149, 173, 14, 92, 120, 206, 222, 158, 28, 40, 24, 30, 16, 175, 108, 128, 35, 230, 118, 40, 121, 113, 125, 216, 130, 11, 24, 90, 48, 194, 240, 105, 44, 76, 34, 57, 249, 228, 125, 80, 38, 9, 136, 29, 117, 207, 139, 168, 181, 85, 137, 126, 10, 126, 242, 120, 247, 121, 8, 100, 12, 201, 171, 38, 226, 193, 180, 190, 117, 177, 87, 143, 242, 213, 11, 44, 180, 113, 93, 106, 99, 179, 68, 175, 211, 164, 116, 64, 148, 226, 254, 172, 147}

            Dim Exponent As Byte() = {1, 0, 1}

            'Values to store encrypted symmetric keys.
            Dim EncryptedSymmetricKey() As Byte
            Dim EncryptedSymmetricIV() As Byte

            'Create a new instance of RSACryptoServiceProvider.
            Dim RSA As New RSACryptoServiceProvider()

            'Create a new instance of RSAParameters.
            Dim RSAKeyInfo As New RSAParameters()

            'Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = PublicKey
            RSAKeyInfo.Exponent = Exponent

            'Import key parameters into RSA.
            RSA.ImportParameters(RSAKeyInfo)

            'Create a new instance of the RijndaelManaged class.
            Dim RM As New RijndaelManaged()

            'Encrypt the symmetric key and IV.
            EncryptedSymmetricKey = RSA.Encrypt(RM.Key, False)
            EncryptedSymmetricIV = RSA.Encrypt(RM.IV, False)

            Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider.")

            'Catch and display a CryptographicException  
            'to the console.
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub
End Class

[C#] 
using System;
using System.Security.Cryptography;

class RSACSPSample
{

    static void Main()
    {
        try
        {        //initialze the byte arrays to the public key information.
            byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
                                   74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
                                   207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
                                   108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
                                   240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
                                   168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
                                   38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
                                   106,99,179,68,175,211,164,116,64,148,226,254,172,147};

            byte[] Exponent = {1,0,1};
      
            //Values to store encrypted symmetric keys.
            byte[] EncryptedSymmetricKey;
            byte[] EncryptedSymmetricIV;

            //Create a new instance of RSACryptoServiceProvider.
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

            //Create a new instance of RSAParameters.
            RSAParameters RSAKeyInfo = new RSAParameters();

            //Set RSAKeyInfo to the public key values. 
            RSAKeyInfo.Modulus = PublicKey;
            RSAKeyInfo.Exponent = Exponent;

            //Import key parameters into RSA.
            RSA.ImportParameters(RSAKeyInfo);

            //Create a new instance of the RijndaelManaged class.
            RijndaelManaged RM = new RijndaelManaged();

            //Encrypt the symmetric key and IV.
            EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
            EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);

            Console.WriteLine("RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider."); 
        
        }
        //Catch and display a CryptographicException  
        //to the console.
        catch(CryptographicException e)
        {
            Console.WriteLine(e.Message);
        }
    }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>

using namespace System;
using namespace System::Security::Cryptography;

int main() {
    try {
        //initialze the Byte arrays to the public key information.
        Byte PublicKey[] = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
            74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
            207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
            108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
            240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
            168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
            38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
            106,99,179,68,175,211,164,116,64,148,226,254,172,147};

        Byte Exponent[] = {1,0,1};

        //Values to store encrypted symmetric keys.
        Byte EncryptedSymmetricKey[];
        Byte EncryptedSymmetricIV[];

        //Create a new instance of RSACryptoServiceProvider.
        RSACryptoServiceProvider* RSA = new RSACryptoServiceProvider();

        //Create a new instance of RSAParameters.
        RSAParameters RSAKeyInfo;

        //Set RSAKeyInfo to the public key values. 
        RSAKeyInfo.Modulus = PublicKey;
        RSAKeyInfo.Exponent = Exponent;

        //Import key parameters into RSA.
        RSA->ImportParameters(RSAKeyInfo);

        //Create a new instance of the RijndaelManaged class.
        RijndaelManaged* RM = new RijndaelManaged();

        //Encrypt the symmetric key and IV.
        EncryptedSymmetricKey = RSA->Encrypt(RM->Key, false);
        EncryptedSymmetricIV = RSA->Encrypt(RM->IV, false);

        Console::WriteLine(S"RijndaelManaged Key and IV have been encrypted with RSACryptoServiceProvider."); 

    } catch (CryptographicException* e) {
        //Catch and display a CryptographicException  
        //to the console.
        Console::WriteLine(e->Message);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および 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 名前空間 | 暗号サービス