데이터 해독

업데이트: 2007년 11월

해독은 암호화의 반대 작업입니다. 비밀 키 암호화의 경우에는 데이터를 암호화하는 데 사용된 키와 IV를 모두 알고 있어야 합니다. 공개 키 암호화의 경우에는 데이터를 암호화하는 데 사용된 키가 개인 키인지 공개 키인지 여부에 따라 각각 공개 키 또는 개인 키를 알고 있어야 합니다.

대칭 해독

대칭 알고리즘을 사용하여 암호화된 데이터를 해독하는 프로세스는 대칭 알고리즘을 사용하여 데이터를 암호화하는 데 사용되는 프로세스와 유사합니다. CryptoStream 클래스는 관리되는 스트림 개체에서 읽은 데이터를 해독하기 위해 .NET Framework의 대칭 암호화 클래스와 함께 사용됩니다.

다음 예제에서는 RijndaelManaged 클래스의 새 인스턴스를 만들고 이 인스턴스를 사용하여 CryptoStream 개체에 대한 해독을 수행하는 방법을 보여 줍니다. 이 예제에서는 먼저 RijndaelManaged 클래스의 새 인스턴스를 만든 다음 CryptoStream 개체를 만들고 이 개체를 MyStream이라는 관리되는 스트림의 값으로 초기화합니다. 그런 다음에는 암호화에 사용된 키와 IV가 RijndaelManaged 클래스의 CreateDecryptor 메서드에 전달되고 이 메서드는 CryptoStream 생성자에 전달됩니다. 마지막으로, CryptoStreamMode.Read 열거형이 CryptoStream 생성자에 전달되어 해당 스트림에 대한 읽기 권한을 지정합니다.

Dim RMCrypto As New RijndaelManaged()
Dim CryptStream As New CryptoStream(MyStream, RMCrypto.CreateDecryptor(RMCrypto.Key, RMCrypto.IV), CryptoStreamMode.Read)
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream CryptStream = new CryptoStream(MyStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read);

다음 예제에서는 스트림을 만들고 스트림을 해독하고 스트림에서 데이터를 읽은 다음 스트림을 닫는 전체 프로세스를 보여 줍니다. 이 예제에서는 수신하는 개체에 연결될 때 네트워크 스트림을 초기화하는 TCPListener 개체가 만들어집니다. 그런 다음에는 CryptoStream 클래스와 RijndaelManaged 클래스를 사용하여 네트워크 스트림이 해독됩니다. 이 예제에서는 키와 IV 값이 전송되었거나 사전에 동의된 것으로 가정합니다. 이 두 값을 암호화하고 전송하는 데 필요한 코드는 이 예제에 포함되어 있지 않습니다.

Imports System
Imports System.Net.Sockets
Imports System.Threading
Imports System.IO
Imports System.Net
Imports System.Security.Cryptography

Module Module1
    Sub Main()
            'The key and IV must be the same values that were used
            'to encrypt the stream.  
            Dim Key As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
            Dim IV As Byte() = {&H1, &H2, &H3, &H4, &H5, &H6, &H7, &H8, &H9, &H10, &H11, &H12, &H13, &H14, &H15, &H16}
        Try
            'Initialize a TCPListener on port 11000
            'using the current IP address.
            Dim TCPListen As New TcpListener(IPAddress.Any, 11000)

            'Start the listener.
            TCPListen.Start()

            'Check for a connection every five seconds.
            While Not TCPListen.Pending()
                Console.WriteLine("Still listening. Will try in 5 seconds.")

                Thread.Sleep(5000)
            End While

            'Accept the client if one is found.
            Dim TCP As TcpClient = TCPListen.AcceptTcpClient()

            'Create a network stream from the connection.
            Dim NetStream As NetworkStream = TCP.GetStream()

            'Create a new instance of the RijndaelManaged class
            'and decrypt the stream.
            Dim RMCrypto As New RijndaelManaged()


            'Create an instance of the CryptoStream class, pass it the NetworkStream, and decrypt 
            'it with the Rijndael class using the key and IV.
            Dim CryptStream As New CryptoStream(NetStream, RMCrypto.CreateDecryptor(Key, IV), CryptoStreamMode.Read)

            'Read the stream.
            Dim SReader As New StreamReader(CryptStream)

            'Display the message.
            Console.WriteLine("The decrypted original message: {0}", SReader.ReadToEnd())

            'Close the streams.
            SReader.Close()
            NetStream.Close()
            TCP.Close()
            'Catch any exceptions. 
        Catch
            Console.WriteLine("The Listener Failed.")
        End Try
    End Sub
End Module
using System;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Net;
using System.Security.Cryptography;

class Class1
{
   static void Main(string[] args)
   {
      //The key and IV must be the same values that were used
      //to encrypt the stream.  
      byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
      byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
      try
      {
         //Initialize a TCPListener on port 11000
         //using the current IP address.
         TcpListener TCPListen = new TcpListener(IPAdress.Any, 11000);

         //Start the listener.
         TCPListen.Start();

         //Check for a connection every five seconds.
         while(!TCPListen.Pending())
         {
            Console.WriteLine("Still listening. Will try in 5 seconds.");
            Thread.Sleep(5000);
         }

         //Accept the client if one is found.
         TcpClient TCP = TCPListen.AcceptTcpClient();

         //Create a network stream from the connection.
         NetworkStream NetStream = TCP.GetStream();

         //Create a new instance of the RijndaelManaged class
         // and decrypt the stream.
         RijndaelManaged RMCrypto = new RijndaelManaged();


         //Create a CryptoStream, pass it the NetworkStream, and decrypt 
         //it with the Rijndael class using the key and IV.
         CryptoStream CryptStream = new CryptoStream(NetStream, 
            RMCrypto.CreateDecryptor(Key, IV), 
            CryptoStreamMode.Read);

         //Read the stream.
         StreamReader SReader = new StreamReader(CryptStream);

         //Display the message.
         Console.WriteLine("The decrypted original message: {0}", SReader.ReadToEnd());

         //Close the streams.
         SReader.Close();
         NetStream.Close();
         TCP.Close();
      }
      //Catch any exceptions. 
      catch
      {
         Console.WriteLine("The Listener Failed.");
      }
   }
}

앞의 샘플이 올바르게 작동하려면 수신자에 대해 암호화된 연결이 만들어져야 합니다. 이 연결에서는 수신자에서 사용된 것과 동일한 키, IV 및 알고리즘을 사용해야 합니다. 이러한 연결이 만들어지면 해당 메시지가 해독되어 콘솔에 표시됩니다.

비대칭 해독

일반적으로 한 구성원(구성원 A)이 공개 키와 개인 키를 모두 생성하고 메모리 또는 암호화 키 컨테이너에 키를 저장합니다. 그런 다음 구성원 A가 다른 구성원(구성원 B)에게 공개 키를 보냅니다. 구성원 B는 공개 키를 사용하여 데이터를 암호화하고 데이터를 다시 구성원 A에게 보냅니다. 구성원 A는 데이터를 받은 다음 해당하는 개인 키를 사용하여 데이터를 해독합니다. 구성원 B가 데이터를 암호화하는 데 사용한 공개 키에 해당하는 개인 키를 구성원 A가 사용하는 경우에만 올바르게 해독됩니다.

보안 암호화 키 컨테이너에 비대칭 키를 저장하는 방법과 나중에 비대칭 키를 검색하는 방법에 대한 내용은 방법: 키 컨테이너에 비대칭 키 저장을 참조하십시오.

다음 예제에서는 대칭 키 및 IV를 나타내는 두 바이트 배열의 해독 방법을 보여 줍니다. RSACryptoServiceProvider 개체의 비대칭 공개 키를 상대방에게 쉽게 보낼 수 있는 형식으로 추출하는 방법에 대한 내용은 데이터 암호화를 참조하십시오.

'Create a new instance of the RSACryptoServiceProvider class.
Dim RSA As New RSACryptoServiceProvider()

' Export the public key information and send it to a third party.
' Wait for the third party to encrypt some data and send it back.

'Decrypt the symmetric key and IV.
SymmetricKey = RSA.Decrypt(EncryptedSymmetricKey, False)
SymmetricIV = RSA.Decrypt(EncryptedSymmetricIV, False)
//Create a new instance of the RSACryptoServiceProvider class.
RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();

// Export the public key information and send it to a third party.
// Wait for the third party to encrypt some data and send it back.

//Decrypt the symmetric key and IV.
SymmetricKey = RSA.Decrypt( EncryptedSymmetricKey, false);
SymmetricIV = RSA.Decrypt( EncryptedSymmetricIV , false);

참고 항목

개념

암호화 및 해독용 키 생성

데이터 암호화

기타 리소스

암호화 작업

암호화 서비스