SymmetricAlgorithm.CreateEncryptor 메서드

정의

대칭 암호기 개체를 만듭니다.

오버로드

CreateEncryptor()

현재 Key 속성 및 초기화 벡터(IV)를 사용하여 대칭 encryptor 개체를 만듭니다.

CreateEncryptor(Byte[], Byte[])

파생 클래스에서 재정의된 경우 지정된 Key 속성 및 초기화 벡터(IV)를 사용하여 대칭 encryptor 개체를 만듭니다.

CreateEncryptor()

현재 Key 속성 및 초기화 벡터(IV)를 사용하여 대칭 encryptor 개체를 만듭니다.

public:
 virtual System::Security::Cryptography::ICryptoTransform ^ CreateEncryptor();
public virtual System.Security.Cryptography.ICryptoTransform CreateEncryptor ();
abstract member CreateEncryptor : unit -> System.Security.Cryptography.ICryptoTransform
override this.CreateEncryptor : unit -> System.Security.Cryptography.ICryptoTransform
Public Overridable Function CreateEncryptor () As ICryptoTransform

반환

ICryptoTransform

대칭 encryptor 개체입니다.

예제

다음 예제에서는 메서드에서 반환된 변환 개체를 사용하여 문자열을 암호화합니다 CreateEncryptor .

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

class EncryptorExample
{
public:
     static void Main()
     {
         TripleDESCryptoServiceProvider^ tdesCSP = gcnew TripleDESCryptoServiceProvider();

         tdesCSP->GenerateKey();
         tdesCSP->GenerateIV();
         String^ quote =
             "Things may come to those who wait, but only the " +
             "things left by those who hustle. -- Abraham Lincoln";
         array<Byte>^ encQuote = EncryptString(tdesCSP, quote);

         Console::WriteLine("Encrypted Quote:\n");
         Console::WriteLine(Convert::ToBase64String(encQuote));

         Console::WriteLine("\nDecrypted Quote:\n");
         Console::WriteLine(DecryptBytes(tdesCSP, encQuote));
     }

public:
     static array<Byte>^ EncryptString(SymmetricAlgorithm^ symAlg, String^ inString)
     {
         array<Byte>^ inBlock = UnicodeEncoding::Unicode->GetBytes(inString);
         ICryptoTransform^ xfrm = symAlg->CreateEncryptor();
         array<Byte>^ outBlock = xfrm->TransformFinalBlock(inBlock, 0, inBlock->Length);

         return outBlock;
     }

     static String^ DecryptBytes(SymmetricAlgorithm^ symAlg, array<Byte>^ inBytes)
     {
         ICryptoTransform^ xfrm = symAlg->CreateDecryptor();
         array<Byte>^ outBlock = xfrm->TransformFinalBlock(inBytes, 0, inBytes->Length);

         return UnicodeEncoding::Unicode->GetString(outBlock);
     }
};

int main()
{
    EncryptorExample::Main();
}
using System;
using System.Security.Cryptography;
using System.Text;

class EncryptorExample
{
     private static string quote =
         "Things may come to those who wait, but only the " +
         "things left by those who hustle. -- Abraham Lincoln";

     public static void Main()
     {
         AesCryptoServiceProvider aesCSP = new AesCryptoServiceProvider();

         aesCSP.GenerateKey();
         aesCSP.GenerateIV();
         byte[] encQuote = EncryptString(aesCSP, quote);

         Console.WriteLine("Encrypted Quote:\n");
         Console.WriteLine(Convert.ToBase64String(encQuote));

         Console.WriteLine("\nDecrypted Quote:\n");
         Console.WriteLine(DecryptBytes(aesCSP, encQuote));
     }

     public static byte[] EncryptString(SymmetricAlgorithm symAlg, string inString)
     {
         byte[] inBlock = UnicodeEncoding.Unicode.GetBytes(inString);
         ICryptoTransform xfrm = symAlg.CreateEncryptor();
         byte[] outBlock = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length);

         return outBlock;
     }

     public static string DecryptBytes(SymmetricAlgorithm symAlg, byte[] inBytes)
     {
         ICryptoTransform xfrm = symAlg.CreateDecryptor();
         byte[] outBlock = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length);

         return UnicodeEncoding.Unicode.GetString(outBlock);
     }
}
Imports System.Security.Cryptography
Imports System.Text

Class EncryptorExample
     Private Shared quote As String = _
         "Things may come to those who wait, but only the " + _
         "things left by those who hustle. -- Abraham Lincoln"

     Public Shared Sub Main()
         Dim aesCSP As New AesCryptoServiceProvider()

         aesCSP.GenerateKey()
         aesCSP.GenerateIV()
         Dim encQuote() As Byte = EncryptString(aesCSP, quote)

         Console.WriteLine("Encrypted Quote:" + Environment.NewLine)
         Console.WriteLine(Convert.ToBase64String(encQuote))

         Console.WriteLine(Environment.NewLine + "Decrypted Quote:" + Environment.NewLine)
         Console.WriteLine(DecryptBytes(aesCSP, encQuote))
     End Sub

     Public Shared Function EncryptString(symAlg As SymmetricAlgorithm, inString As String) As Byte()
         Dim inBlock() As Byte = UnicodeEncoding.Unicode.GetBytes(inString)
         Dim xfrm As ICryptoTransform = symAlg.CreateEncryptor()
         Dim outBlock() As Byte = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length)

         Return outBlock
     End Function

     Public Shared Function DecryptBytes(symAlg As SymmetricAlgorithm, inBytes() As Byte) As String
         Dim xfrm As ICryptoTransform = symAlg.CreateDecryptor()
         Dim outBlock() As Byte = xfrm.TransformFinalBlock(inBytes, 0, inBytes.Length)

         return UnicodeEncoding.Unicode.GetString(outBlock)
     End Function
End Class

설명

현재 Key 속성이 null면 메서드가 GenerateKey 호출되어 새 임 Key의를 만듭니다. 현재 IV 속성이 null면 메서드가 GenerateIV 호출되어 새 임 IV의를 만듭니다.

동일한 서명이 CreateDecryptor 있는 오버로드를 사용하여 이 메서드의 결과를 해독합니다.

추가 정보

적용 대상

CreateEncryptor(Byte[], Byte[])

파생 클래스에서 재정의된 경우 지정된 Key 속성 및 초기화 벡터(IV)를 사용하여 대칭 encryptor 개체를 만듭니다.

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

매개 변수

rgbKey
Byte[]

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

rgbIV
Byte[]

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

반환

ICryptoTransform

대칭 encryptor 개체입니다.

설명

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

추가 정보

적용 대상