次の方法で共有


RijndaelManaged クラス

Rijndael アルゴリズムのマネージ バージョンにアクセスします。このクラスは継承できません。

この型のすべてのメンバの一覧については、RijndaelManaged メンバ を参照してください。

System.Object
   System.Security.Cryptography.SymmetricAlgorithm
      System.Security.Cryptography.Rijndael
         System.Security.Cryptography.RijndaelManaged

NotInheritable Public Class RijndaelManaged
   Inherits Rijndael
[C#]
public sealed class RijndaelManaged : Rijndael
[C++]
public __gc __sealed class RijndaelManaged : public Rijndael
[JScript]
public class RijndaelManaged extends Rijndael

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

使用例

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

Namespace RijndaelManaged_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 myRijndael As New RijndaelManaged()
            Dim fromEncrypt() As Byte
            Dim encrypted() As Byte
            Dim toEncrypt() As Byte
            Dim key() As Byte
            Dim IV() As Byte

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

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

            'Get an encryptor.
            Dim encryptor As ICryptoTransform = myRijndael.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 RijnDael
            ' encrypted message.            
            'Get a decryptor that uses the same key and IV as the encryptor.
            Dim decryptor As ICryptoTransform = myRijndael.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 'RijndaelManaged_Examples

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

namespace RijndaelManaged_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();
            RijndaelManaged myRijndael = new RijndaelManaged();
            byte[] fromEncrypt;
            byte[] encrypted;
            byte[] toEncrypt;
            byte[] key;
            byte[] IV;

            //Create a new key and initialization vector.
            myRijndael.GenerateKey();
            myRijndael.GenerateIV();

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

            //Get an encryptor.
            ICryptoTransform encryptor = myRijndael.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 RijnDael
            // encrypted message.            

            //Get a decryptor that uses the same key and IV as the encryptor.
            ICryptoTransform decryptor = myRijndael.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 <System.dll>

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

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

    //Create a new key and initialization vector.
    myRijndael->GenerateKey();
    myRijndael->GenerateIV();

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

    //Get an encryptor.
    ICryptoTransform* encryptor = myRijndael->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 RijnDael
    // encrypted message.            

    //Get a decryptor that uses the same key and IV as the encryptor.
    ICryptoTransform* decryptor = myRijndael->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(S"Original:   {0}", original);
    Console::WriteLine(S"Round Trip: {0}", roundtrip);
}

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

必要条件

名前空間: System.Security.Cryptography

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

RijndaelManaged メンバ | System.Security.Cryptography 名前空間 | 暗号サービス