방법: X.509 인증서로 XML 요소 해독

System.Security.Cryptography.Xml 네임스페이스의 클래스를 사용하여 XML 문서 내의 요소를 암호화하고 해독할 수 있습니다. XML 암호화는 데이터가 쉽게 읽힐 염려 없이 암호화된 XML 데이터를 교환하거나 저장할 수 있는 표준 방식입니다. XML 암호화 표준에 대한 자세한 내용은 http://www.w3.org/TR/xmldsig-core/에서 XML 암호화에 대한 W3C(World Wide Web 컨소시엄) 사양을 참조하십시오.

이 예제에서는 방법: X.509 인증서로 XML 요소 암호화에서 설명한 메서드를 사용하여 암호화된 XML 요소를 해독합니다. <EncryptedData> 요소를 찾아 해독한 다음 이 요소를 원래의 일반 텍스트 XML 요소로 바꿉니다.

이 절차의 코드 예제에서는 현재 사용자 계정의 로컬 인증서 저장소에서 가져온 X.509 인증서를 사용하여 XML 요소를 해독합니다. 이 예제에서는 DecryptDocument 메서드를 사용하여 X.509 인증서를 자동으로 검색하고 <EncryptedData> 요소의 <EncryptedKey> 요소에 저장된 세션 키를 해독합니다. 그런 다음 DecryptDocument 메서드는 세션 키를 자동으로 사용하여 XML 요소를 해독합니다.

이 예제는 여러 응용 프로그램에서 암호화된 데이터를 공유해야 하거나 단일 응용 프로그램에서 실행 시점 사이에 암호화된 데이터를 저장해야 하는 경우에 적절합니다.

X.509 인증서를 사용하여 XML 요소를 해독하려면

  1. 디스크에서 XML 파일을 로드하여 XmlDocument 개체를 만듭니다. XmlDocument 개체는 해독할 XML 요소를 포함하고 있습니다.

    Dim xmlDoc As New XmlDocument()
    
    XmlDocument xmlDoc = new XmlDocument();
    
  2. XmlDocument 개체를 생성자에게 전달하여 새 EncryptedXml 개체를 만듭니다.

    Dim exml As New EncryptedXml(Doc)
    
    EncryptedXml exml = new EncryptedXml(Doc);
    
  3. DecryptDocument 메서드를 사용하여 XML 문서를 해독합니다.

    exml.DecryptDocument()
    
    exml.DecryptDocument();
    
  4. XmlDocument 개체를 저장합니다.

    xmlDoc.Save("test.xml")
    
    xmlDoc.Save("test.xml");
    

예제

Imports System
Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Security.Cryptography.X509Certificates



Module Program

    Sub Main(ByVal args() As String)
        Try
            ' Create an XmlDocument object.
            Dim xmlDoc As New XmlDocument()
            ' Load an XML file into the XmlDocument object.
            xmlDoc.PreserveWhitespace = True
            xmlDoc.Load("test.xml")

            ' Decrypt the document.
            Decrypt(xmlDoc)

            ' Save the XML document.
            xmlDoc.Save("test.xml")
            ' Display the decrypted XML to the console.
            Console.WriteLine("Decrypted XML:")
            Console.WriteLine()
            Console.WriteLine(xmlDoc.OuterXml)

        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

    End Sub


    Sub Decrypt(ByVal Doc As XmlDocument)
        ' Check the arguments.  
        If Doc Is Nothing Then
            Throw New ArgumentNullException("Doc")
        End If

        ' Create a new EncryptedXml object.
        Dim exml As New EncryptedXml(Doc)
        ' Decrypt the XML document.
        exml.DecryptDocument()
    End Sub
End Module
using System;
using System.Xml;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Security.Cryptography.X509Certificates;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            // Create an XmlDocument object.
            XmlDocument xmlDoc = new XmlDocument();

            // Load an XML file into the XmlDocument object.
            xmlDoc.PreserveWhitespace = true;
            xmlDoc.Load("test.xml");

            // Decrypt the document.
            Decrypt(xmlDoc);

            // Save the XML document.
            xmlDoc.Save("test.xml");

            // Display the decrypted XML to the console.
            Console.WriteLine("Decrypted XML:");
            Console.WriteLine();
            Console.WriteLine(xmlDoc.OuterXml);

        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

    }

    public static void Decrypt(XmlDocument Doc)
    {
        // Check the arguments.  
        if (Doc == null)
            throw new ArgumentNullException("Doc");


        // Create a new EncryptedXml object.
        EncryptedXml exml = new EncryptedXml(Doc);

        // Decrypt the XML document.
        exml.DecryptDocument();

    }
}

이 예제에서는 컴파일된 프로그램과 같은 디렉터리에 "test.xml" 파일이 있다고 가정합니다. 또한 "test.xml"에 "creditcard" 요소가 포함되고 있다고 가정합니다. 다음 XML을 test.xml 파일에 추가한 다음 이 예제에서 사용할 수 있습니다.

<root>
    <creditcard>
        <number>19834209</number>
        <expiry>02/02/2002</expiry>
    </creditcard>
</root>

코드 컴파일

보안

이 예제에서 사용한 X.509 인증서는 테스트 전용입니다. 응용 프로그램에서는 신뢰할 수 있는 인증 기관에서 생성한 X.509 인증서를 사용하거나 Microsoft Windows Certificate Server에서 생성한 인증서를 사용해야 합니다.

참고 항목

작업

방법: X.509 인증서로 XML 요소 암호화

참조

System.Security.Cryptography.Xml

기타 리소스

XML 암호화 및 디지털 서명