다음을 통해 공유


RijndaelManaged 클래스

Rijndael 알고리즘의 관리되는 버전에 액세스합니다. 이 클래스는 상속될 수 없습니다.

네임스페이스: System.Security.Cryptography
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
Public NotInheritable Class RijndaelManaged
    Inherits Rijndael
‘사용 방법
Dim instance As RijndaelManaged
[ComVisibleAttribute(true)] 
public sealed class RijndaelManaged : Rijndael
[ComVisibleAttribute(true)] 
public ref class RijndaelManaged sealed : public Rijndael
/** @attribute ComVisibleAttribute(true) */ 
public final class RijndaelManaged extends Rijndael
ComVisibleAttribute(true) 
public final class RijndaelManaged extends Rijndael

설명

이 알고리즘은 128, 192 또는 256비트 길이의 키를 지원합니다.

항목 위치
방법: 대칭 키를 사용하여 XML 요소 암호화 .NET Framework: Security
방법: 대칭 키를 사용하여 XML 요소 해독 .NET Framework: Security
방법: 대칭 키를 사용하여 XML 요소 해독 .NET Framework: 보안
방법: 대칭 키를 사용하여 XML 요소 암호화 .NET Framework: 보안

예제

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
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);
        }
    }
}
#using <System.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 = gcnew ASCIIEncoding;
   RijndaelManaged^ myRijndael = gcnew RijndaelManaged;
   array<Byte>^fromEncrypt;
   array<Byte>^encrypted;
   array<Byte>^toEncrypt;
   array<Byte>^key;
   array<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 = gcnew MemoryStream;
   CryptoStream^ csEncrypt = gcnew 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 = gcnew MemoryStream( encrypted );
   CryptoStream^ csDecrypt = gcnew CryptoStream( msDecrypt,decryptor,CryptoStreamMode::Read );
   fromEncrypt = gcnew array<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 );
}
package RijndaelManaged_Examples; 

import System.*;
import System.IO.*;
import System.Text.*;
import System.Security.Cryptography.*;

class MyMainClass
{
    public static void main(String[] args)
    {      
        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();
        ubyte fromEncrypt[];
        ubyte encrypted[];
        ubyte toEncrypt[];
        ubyte key[];
        ubyte iv[];
        //Create a new key and initialization vector.
        myRijndael.GenerateKey();
        myRijndael.GenerateIV();
        //Get the key and iv.
        key = myRijndael.get_Key();
        iv = myRijndael.get_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.get_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 ubyte[encrypted.get_Length()];
        //Read the data out of the crypto stream.
        csDecrypt.Read(fromEncrypt, 0, fromEncrypt.get_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);
    } //main
} //MyMainClass

상속 계층 구조

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

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

RijndaelManaged 멤버
System.Security.Cryptography 네임스페이스

기타 리소스

암호화 서비스