다음을 통해 공유


RC2CryptoServiceProvider.CreateEncryptor(Byte[], Byte[]) 메서드

정의

지정된 키(RC2)와 초기화 벡터(Key)를 사용하여 대칭 IV encryptor 개체를 만듭니다.

public:
 override System::Security::Cryptography::ICryptoTransform ^ CreateEncryptor(cli::array <System::Byte> ^ rgbKey, cli::array <System::Byte> ^ rgbIV);
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[]? rgbIV);
public override System.Security.Cryptography.ICryptoTransform CreateEncryptor (byte[] rgbKey, byte[] rgbIV);
override this.CreateEncryptor : byte[] * byte[] -> System.Security.Cryptography.ICryptoTransform
Public Overrides Function CreateEncryptor (rgbKey As Byte(), rgbIV As Byte()) As ICryptoTransform

매개 변수

rgbKey
Byte[]

대칭 알고리즘에 사용할 비밀 키입니다.

rgbIV
Byte[]

대칭 알고리즘에 사용할 초기화 벡터입니다.

반환

대칭 RC2 encryptor 개체입니다.

예외

OFB 암호화 모드가 사용된 경우

또는

사용된 CFB 암호화 모드의 피드백 크기가 8비트가 아닌 경우

또는

잘못된 키 크기가 사용된 경우

또는

알고리즘 키 크기를 사용할 수 없는 경우

예제

다음 코드 예제에서는 문자열을 암호화한 다음 암호를 해독합니다.

using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace RC2CryptoServiceProvider_Examples
{
    class MyMainClass
    {
        public static void Main()
        {

            // Create a new instance of the RC2CryptoServiceProvider class
            // and automatically generate a Key and IV.
            RC2CryptoServiceProvider rc2CSP = new RC2CryptoServiceProvider();

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

            // Get the key and IV.
            byte[] key = rc2CSP.Key;
            byte[] IV = rc2CSP.IV;

            // Get an encryptor.
            ICryptoTransform encryptor = rc2CSP.CreateEncryptor(key, IV);

            // Encrypt the data as an array of encrypted bytes in memory.
            MemoryStream msEncrypt = new MemoryStream();
            CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

            // Convert the data to a byte array.
            string original = "Here is some data to encrypt.";
            byte[] toEncrypt = Encoding.ASCII.GetBytes(original);

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

            // Get the encrypted array of bytes.
            byte[] encrypted = msEncrypt.ToArray();

            ///////////////////////////////////////////////////////
            // This is where the data could be transmitted or saved.
            ///////////////////////////////////////////////////////

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

            // Read the decrypted bytes from the decrypting stream
            // and place them in a StringBuilder class.

            StringBuilder roundtrip = new StringBuilder();

            int b = 0;

            do
            {
                b = csDecrypt.ReadByte();

                if (b != -1)
                {
                    roundtrip.Append((char)b);
                }
            } while (b != -1);

            // Display the original data and the decrypted data.
            Console.WriteLine("Original:   {0}", original);
            Console.WriteLine("Round Trip: {0}", roundtrip);
        }
    }
}
Imports System.IO
Imports System.Text
Imports System.Security.Cryptography



Module Crypto

    Sub Main()

        ' Create a new instance of the RC2CryptoServiceProvider class
        ' and automatically generate a Key and IV.
        Dim rc2CSP As New RC2CryptoServiceProvider()

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

        ' Get the key and IV.
        Dim key As Byte() = rc2CSP.Key
        Dim IV As Byte() = rc2CSP.IV

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

        ' Encrypt the data as an array of encrypted bytes in memory.
        Dim msEncrypt As New MemoryStream()
        Dim csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)

        ' Convert the data to a byte array.
        Dim original As String = "Here is some data to encrypt."
        Dim toEncrypt As Byte() = Encoding.ASCII.GetBytes(original)

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

        ' Get the encrypted array of bytes.
        Dim encrypted As Byte() = msEncrypt.ToArray()

        '''''''''''''''''''''''''''''''''''''''''''''''''''''''
        ' This is where the data could be transmitted or saved.
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''

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

        ' Read the decrypted bytes from the decrypting stream
        ' and place them in a StringBuilder class.
        Dim roundtrip As New StringBuilder()

        Dim b As Integer = 0

        Do
            b = csDecrypt.ReadByte()

            If b <> -1 Then
                roundtrip.Append(ChrW(b))
            End If
        Loop While b <> -1

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

    End Sub
End Module

설명

CreateDecryptor 동일한 매개 변수와 함께 오버로드를 사용하여 이 메서드의 결과를 해독합니다.

적용 대상

추가 정보