SymmetricAlgorithm.CreateEncryptor メソッド

定義

対称暗号化オブジェクトを作成します。Creates a symmetric encryptor object.

オーバーロード

CreateEncryptor()

現在の Key プロパティおよび初期化ベクター (IV) を使用して、対称暗号化オブジェクトを作成します。Creates a symmetric encryptor object with the current Key property and initialization vector (IV).

CreateEncryptor(Byte[], Byte[])

派生クラスでオーバーライドされると、指定した Key プロパティおよび初期化ベクター (IV) を使用して、対称暗号化オブジェクトを作成します。When overridden in a derived class, creates a symmetric encryptor object with the specified Key property and initialization vector (IV).

CreateEncryptor()

現在の Key プロパティおよび初期化ベクター (IV) を使用して、対称暗号化オブジェクトを作成します。Creates a symmetric encryptor object with the current Key property and initialization vector (IV).

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

戻り値

対称暗号化オブジェクト。A symmetric encryptor object.

次の例では、CreateEncryptor メソッドから返された transform オブジェクトを使用して文字列を暗号化します。The following example encrypts a string using the transform object returned from the CreateEncryptor method.

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:" + vbNewLine)
         Console.WriteLine(Convert.ToBase64String(encQuote))

         Console.WriteLine(vbNewLine + "Decrypted Quote:" + vbNewLine)
         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場合、新しいランダム Keyを作成するために GenerateKey メソッドが呼び出されます。If the current Key property is null, the GenerateKey method is called to create a new random Key. 現在の IV プロパティが null場合、新しいランダム IVを作成するために GenerateIV メソッドが呼び出されます。If the current IV property is null, the GenerateIV method is called to create a new random IV.

同じシグネチャを持つ CreateDecryptor オーバーロードを使用して、このメソッドの結果を復号化します。Use the CreateDecryptor overload with the same signature to decrypt the result of this method.

こちらもご覧ください

CreateEncryptor(Byte[], Byte[])

派生クラスでオーバーライドされると、指定した Key プロパティおよび初期化ベクター (IV) を使用して、対称暗号化オブジェクトを作成します。When overridden in a derived class, creates a symmetric encryptor object with the specified Key property and initialization vector (IV).

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);
abstract member CreateEncryptor : byte[] * byte[] -> System.Security.Cryptography.ICryptoTransform
Public MustOverride Function CreateEncryptor (rgbKey As Byte(), rgbIV As Byte()) As ICryptoTransform

パラメーター

rgbKey
Byte[]

対称アルゴリズムに使用する共有キー。The secret key to use for the symmetric algorithm.

rgbIV
Byte[]

対称アルゴリズムに使用する初期化ベクター。The initialization vector to use for the symmetric algorithm.

戻り値

対称暗号化オブジェクト。A symmetric encryptor object.

注釈

同じパラメーターで CreateDecryptor オーバーロードを使用して、このメソッドの結果を復号化します。Use the CreateDecryptor overload with the same parameters to decrypt the result of this method.

こちらもご覧ください

適用対象