次の方法で共有


RC2CryptoServiceProvider.CreateEncryptor メソッド

対称 RC2 暗号化オブジェクトを作成します。

オーバーロードの一覧

指定した Key および初期化ベクタ (IV) を使用して、対称 RC2 暗号化オブジェクトを作成します。

[Visual Basic] Overloads Overrides Public Function CreateEncryptor(Byte(), Byte()) As ICryptoTransform

[C#] public override ICryptoTransform CreateEncryptor(byte[], byte[]);

[C++] public: ICryptoTransform* CreateEncryptor(unsigned char __gc[], unsigned char __gc[]);

[JScript] public override function CreateEncryptor(Byte[], Byte[]) : ICryptoTransform;

SymmetricAlgorithm から継承されます。

[Visual Basic] Overloads Public Overridable Function CreateEncryptor() As ICryptoTransform

[C#] public virtual ICryptoTransform CreateEncryptor();

[C++] public: virtual ICryptoTransform* CreateEncryptor();

[JScript] public function CreateEncryptor() : ICryptoTransform;

使用例

[Visual Basic, C#, C++] メモ   ここでは、CreateEncryptor のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
Imports System
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography

Namespace RC2CryptoServiceProvider_Examples
    Class MyMainClass
        Public Shared Sub Main()
            Dim original As String = "This is a much longer string of data than a public/private key algorithm will accept."
            Dim roundtrip As String
            Dim textConverter As New ASCIIEncoding()
            Dim rc2CSP As New RC2CryptoServiceProvider()
            Dim fromEncrypt() As Byte
            Dim encrypted() As Byte
            Dim toEncrypt() As Byte
            Dim key() As Byte
            Dim IV() As Byte

            Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize)

            'Create a new key and initialization vector.
            rc2CSP.GenerateKey()
            rc2CSP.GenerateIV()

            'Get the key and IV.
            key = rc2CSP.Key
            IV = rc2CSP.IV

            'Get an encryptor.
            Dim encryptor As ICryptoTransform = rc2CSP.CreateEncryptor(key, IV)

            'Encrypt the data.
            Dim msEncrypt As New MemoryStream()
            Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

            'Convert the data to a byte array.
            toEncrypt = textConverter.GetBytes(original)

            'Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length)
            csEncrypt.FlushFinalBlock()

            'Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray()

            'This is where the message would be transmitted to a recipient
            ' who already knows your secret key. Optionally, you can
            ' also encrypt your secret key using a public key algorithm
            ' and pass it to the mesage recipient along with the RC2
            ' encrypted message.            
            'Get a decryptor that uses the same key and IV as the encryptor.
            Dim decryptor As ICryptoTransform = rc2CSP.CreateDecryptor(key, IV)

            'Now decrypt the previously encrypted message using the decryptor
            ' obtained in the above step.
            Dim msDecrypt As New MemoryStream(encrypted)
            Dim csDecrypt As New CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)

            fromEncrypt = New Byte(encrypted.Length) {}

            'Read the data out of the crypto stream.
            csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length)

            'Convert the byte array back into a string.
            roundtrip = textConverter.GetString(fromEncrypt)

            'Display the original data and the decrypted data.
            Console.WriteLine("Original:   {0}", original)
            Console.WriteLine("Round Trip: {0}", roundtrip)
        End Sub 'Main
    End Class 'MyMainClass
End Namespace 'RC2CryptoServiceProvider_Examples

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

namespace RC2CryptoServiceProvider_Examples
{
    class MyMainClass
    {
        public static void Main()
        {
            string original = "This is a much longer string of data than a public/private key algorithm will accept.";
            string roundtrip;
            ASCIIEncoding textConverter = new ASCIIEncoding();
            RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();
            byte[] fromEncrypt;
            byte[] encrypted;
            byte[] toEncrypt;
            byte[] key;
            byte[] IV;

            Console.WriteLine("Effective key size is {0} bits.", rc2CSP.EffectiveKeySize);
            
            //Create a new key and initialization vector.
            rc2CSP.GenerateKey();
            rc2CSP.GenerateIV();

            //Get the key and IV.
            key = rc2CSP.Key;
            IV = rc2CSP.IV;

            //Get an encryptor.
            ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);
            
            //Encrypt the data.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            //Convert the data to a byte array.
            toEncrypt = textConverter.GetBytes(original);

            //Write all data to the crypto stream and flush it.
            csEncrypt.Write(toEncrypt, 0, toEncrypt.Length);
            csEncrypt.FlushFinalBlock();

            //Get encrypted array of bytes.
            encrypted = msEncrypt.ToArray();

            //This is where the message would be transmitted to a recipient
            // who already knows your secret key. Optionally, you can
            // also encrypt your secret key using a public key algorithm
            // and pass it to the mesage recipient along with the RC2
            // encrypted message.            

            //Get a decryptor that uses the same key and IV as the encryptor.
            ICryptoTransform decryptor = rc2CSP.CreateDecryptor(key, IV);

            //Now decrypt the previously encrypted message using the decryptor
            // obtained in the above step.
            MemoryStream msDecrypt = new MemoryStream(encrypted);
            CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);

            fromEncrypt = new byte[encrypted.Length];

            //Read the data out of the crypto stream.
            csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

            //Convert the byte array back into a string.
            roundtrip = textConverter.GetString(fromEncrypt);

            //Display the original data and the decrypted data.
            Console.WriteLine("Original:   {0}", original);
            Console.WriteLine("Round Trip: {0}", roundtrip);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Security::Cryptography;

int main()
{
    String *original = "This is a much longer string of data than a public/private key algorithm will accept.";
    String *roundtrip;
    ASCIIEncoding * textConverter = new ASCIIEncoding();
    RC2CryptoServiceProvider * rc2CSP = new RC2CryptoServiceProvider();
    Byte fromEncrypt __gc[];
    Byte encrypted __gc[];
    Byte toEncrypt __gc[];
    Byte key __gc[];
    Byte IV __gc[];

    Console::WriteLine("Effective key size is {0} bits.", (rc2CSP->EffectiveKeySize).ToString());
    
    //Create a new key and initialization vector.
    rc2CSP->GenerateKey();
    rc2CSP->GenerateIV();

    //Get the key and IV.
    key = rc2CSP->Key;
    IV = rc2CSP->IV;

    //Get an encryptor.
    ICryptoTransform * encryptor = rc2CSP->CreateEncryptor(key, IV);
        
    //Encrypt the data.
    MemoryStream * msEncrypt = new MemoryStream();
    CryptoStream * csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode::Write);

    //Convert the data to a Byte array.
    toEncrypt = textConverter->GetBytes(original);

    //Write all data to the crypto stream and flush it.
    csEncrypt->Write(toEncrypt, 0, toEncrypt->Length);
    csEncrypt->FlushFinalBlock();

    //Get encrypted array of Bytes.
    encrypted = msEncrypt->ToArray();

    //This is where the message would be transmitted to a recipient
        // who already knows your secret key. Optionally, you can
    // also encrypt your secret key using a public key algorithm
    // and pass it to the mesage recipient along with the RC2
    // encrypted message.            

    //Get a decryptor that uses the same key and IV as the encryptor.
    ICryptoTransform * decryptor = rc2CSP->CreateDecryptor(key, IV);

    //Now decrypt the previously encrypted message using the decryptor
    // obtained in the above step.
    MemoryStream * msDecrypt = new MemoryStream(encrypted);
    CryptoStream * csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode::Read);

    fromEncrypt = new Byte[encrypted->Length];

    //Read the data out of the crypto stream.
    csDecrypt->Read(fromEncrypt, 0, fromEncrypt->Length);

    //Convert the Byte array back into a string.
    roundtrip = textConverter->GetString(fromEncrypt);

    //Display the original data and the decrypted data.
    Console::WriteLine("Original:   {0}", original);
    Console::WriteLine("Round Trip: {0}", roundtrip);
}

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

参照

RC2CryptoServiceProvider クラス | RC2CryptoServiceProvider メンバ | System.Security.Cryptography 名前空間