다음을 통해 공유


방법: 디지털 서명을 사용하여 XML 문서 서명

네임스페이스의 클래스를 System.Security.Cryptography.Xml 사용하여 디지털 서명으로 XML 문서 또는 XML 문서의 일부에 서명할 수 있습니다. XMLDSIG(XML 디지털 서명)를 사용하면 데이터가 서명된 후 변경되지 않은지 확인할 수 있습니다. XMLDSIG 표준에 대한 자세한 내용은 W3C(World Wide Web 컨소시엄) 권장 사항 XML 서명 구문 및 처리를 참조하세요.

메모

이 문서의 코드는 Windows에 적용됩니다.

이 절차의 코드 예제에서는 전체 XML 문서에 디지털 서명하고 요소의 문서에 <Signature> 서명을 첨부하는 방법을 보여 줍니다. 이 예제에서는 RSA 서명 키를 만들고, 보안 키 컨테이너에 키를 추가한 다음, 키를 사용하여 XML 문서에 디지털 서명합니다. 그런 다음 키를 검색하여 XML 디지털 서명을 확인하거나 다른 XML 문서에 서명하는 데 사용할 수 있습니다.

이 절차를 사용하여 만든 XML 디지털 서명을 확인하는 방법에 대한 자세한 내용은 방법: XML 문서의 디지털 서명 확인

XML 문서에 디지털 서명하려면

  1. 개체를 CspParameters 만들고 키 컨테이너의 이름을 지정합니다.

    CspParameters cspParams = new()
    {
        KeyContainerName = "XML_DSIG_RSA_KEY"
    };
    
    Dim cspParams As New CspParameters With {
        .KeyContainerName = "XML_DSIG_RSA_KEY"
    }
    
  2. 클래스를 사용하여 비대칭 키를 생성합니다 RSACryptoServiceProvider . CspParameters 개체를 RSACryptoServiceProvider 클래스의 생성자에 전달하면 키가 자동으로 키 컨테이너에 저장됩니다. 이 키는 XML 문서에 서명하는 데 사용됩니다.

    RSACryptoServiceProvider rsaKey = new(cspParams);
    
    Dim rsaKey As New RSACryptoServiceProvider(cspParams)
    
  3. XmlDocument 디스크에서 XML 파일을 로드하여 개체를 만듭니다. 개체에는 XmlDocument 암호화할 XML 요소가 포함되어 있습니다.

    XmlDocument xmlDoc = new()
    {
        // Load an XML file into the XmlDocument object.
        PreserveWhitespace = true
    };
    xmlDoc.Load("test.xml");
    
    ' Load an XML file into the XmlDocument object.
    Dim xmlDoc As New XmlDocument With {
        .PreserveWhitespace = True
    }
    xmlDoc.Load("test.xml")
    
  4. SignedXml 개체를 만들고 XmlDocument 개체를 그 개체에 전달합니다.

    SignedXml signedXml = new(xmlDoc)
    {
    
    Dim signedXml As New SignedXml(xmlDoc)
    
  5. 서명 RSA 키를 개체에 SignedXml 추가합니다.

        SigningKey = rsaKey
    };
    
    signedXml.SigningKey = rsaKey
    
  6. Reference 서명할 내용을 설명하는 개체를 만듭니다. 전체 문서에 서명하려면 속성을 Uri.로 설정합니다"".

    // Create a reference to be signed.
    Reference reference = new()
    {
        Uri = ""
    };
    
    ' Create a reference to be signed.
    Dim reference As New Reference()
    reference.Uri = ""
    
  7. XmlDsigEnvelopedSignatureTransform 개체를 Reference 개체에 추가합니다. 변환을 통해 검증 도구는 서명자가 사용한 것과 동일한 방식으로 XML 데이터를 나타낼 수 있습니다. XML 데이터는 여러 가지 방법으로 나타낼 수 있으므로 이 단계는 확인에 매우 중요합니다.

    XmlDsigEnvelopedSignatureTransform env = new();
    reference.AddTransform(env);
    
    Dim env As New XmlDsigEnvelopedSignatureTransform()
    reference.AddTransform(env)
    
  8. Reference 개체를 SignedXml 개체에 추가하십시오.

    signedXml.AddReference(reference);
    
    signedXml.AddReference(reference)
    
  9. 메서드를 호출하여 서명을 계산합니다 ComputeSignature .

    signedXml.ComputeSignature();
    
    signedXml.ComputeSignature()
    
  10. 서명(요소)의 XML 표현을 <Signature> 검색하여 새 XmlElement 개체에 저장합니다.

    XmlElement xmlDigitalSignature = signedXml.GetXml();
    
    Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
    
  11. XmlDocument 개체에 요소를 추가합니다.

    xmlDoc.DocumentElement?.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
    
    xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True))
    
  12. 문서를 저장합니다.

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

예시

이 예제에서는 이름이 지정된 test.xml 파일이 컴파일된 프로그램과 동일한 디렉터리에 있다고 가정합니다. XML 파일 test.xml에 다음을 넣고 이 예제에 사용할 수 있습니다.

<root>
    <creditcard>
        <number>19834209</number>
        <expiry>02/02/2002</expiry>
    </creditcard>
</root>
using System;
using System.Runtime.Versioning;
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Xml;

[SupportedOSPlatform("Windows")]
public class SignXML
{
    public static void Main(String[] args)
    {
        try
        {
            // Create a new CspParameters object to specify
            // a key container.
            CspParameters cspParams = new()
            {
                KeyContainerName = "XML_DSIG_RSA_KEY"
            };

            // Create a new RSA signing key and save it in the container.
            RSACryptoServiceProvider rsaKey = new(cspParams);

            // Create a new XML document.
            XmlDocument xmlDoc = new()
            {
                // Load an XML file into the XmlDocument object.
                PreserveWhitespace = true
            };
            xmlDoc.Load("test.xml");

            // Sign the XML document.
            SignXml(xmlDoc, rsaKey);

            Console.WriteLine("XML file signed.");

            // Save the document.
            xmlDoc.Save("test.xml");
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    // Sign an XML file.
    // This document cannot be verified unless the verifying
    // code has the key with which it was signed.
    public static void SignXml(XmlDocument xmlDoc, RSA rsaKey)
    {
        // Check arguments.
        if (xmlDoc == null)
            throw new ArgumentException(null, nameof(xmlDoc));
        if (rsaKey == null)
            throw new ArgumentException(null, nameof(rsaKey));

        // Create a SignedXml object.
        SignedXml signedXml = new(xmlDoc)
        {

            // Add the key to the SignedXml document.
            SigningKey = rsaKey
        };

        // Create a reference to be signed.
        Reference reference = new()
        {
            Uri = ""
        };

        // Add an enveloped transformation to the reference.
        XmlDsigEnvelopedSignatureTransform env = new();
        reference.AddTransform(env);

        // Add the reference to the SignedXml object.
        signedXml.AddReference(reference);

        // Compute the signature.
        signedXml.ComputeSignature();

        // Get the XML representation of the signature and save
        // it to an XmlElement object.
        XmlElement xmlDigitalSignature = signedXml.GetXml();

        // Append the element to the XML document.
        xmlDoc.DocumentElement?.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
    }
}
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Xml

Module SignXML
    Sub Main(ByVal args() As String)
        Try
            ' Create a new CspParameters object to specify
            ' a key container.
            Dim cspParams As New CspParameters With {
                .KeyContainerName = "XML_DSIG_RSA_KEY"
            }
            ' Create a new RSA signing key and save it in the container. 
            Dim rsaKey As New RSACryptoServiceProvider(cspParams)
            ' Create a new XML document.
            ' Load an XML file into the XmlDocument object.
            Dim xmlDoc As New XmlDocument With {
                .PreserveWhitespace = True
            }
            xmlDoc.Load("test.xml")
            ' Sign the XML document. 
            SignXml(xmlDoc, rsaKey)

            Console.WriteLine("XML file signed.")

            ' Save the document.
            xmlDoc.Save("test.xml")
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try
    End Sub

    ' Sign an XML file. 
    ' This document cannot be verified unless the verifying 
    ' code has the key with which it was signed.
    Sub SignXml(ByVal xmlDoc As XmlDocument, ByVal rsaKey As RSA)
        ' Check arguments.
        If xmlDoc Is Nothing Then
            Throw New ArgumentException(
                "The XML doc cannot be nothing.", NameOf(xmlDoc))
        End If
        If rsaKey Is Nothing Then
            Throw New ArgumentException(
                "The RSA key cannot be nothing.", NameOf(rsaKey))
        End If
        ' Create a SignedXml object.
        Dim signedXml As New SignedXml(xmlDoc)
        ' Add the key to the SignedXml document.
        signedXml.SigningKey = rsaKey
        ' Create a reference to be signed.
        Dim reference As New Reference()
        reference.Uri = ""
        ' Add an enveloped transformation to the reference.
        Dim env As New XmlDsigEnvelopedSignatureTransform()
        reference.AddTransform(env)
        ' Add the reference to the SignedXml object.
        signedXml.AddReference(reference)
        ' Compute the signature.
        signedXml.ComputeSignature()
        ' Get the XML representation of the signature and save
        ' it to an XmlElement object.
        Dim xmlDigitalSignature As XmlElement = signedXml.GetXml()
        ' Append the element to the XML document.
        xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, True))
    End Sub
End Module

코드 컴파일

.NET 보안

비대칭 키 쌍의 프라이빗 키를 일반 텍스트로 저장하거나 전송하지 않습니다. 대칭 및 비대칭 암호화 키에 대한 자세한 내용은 암호화 및 암호 해독을 위한 키 생성을 참조하세요.

프라이빗 키를 소스 코드에 직접 포함하지 마세요. 포함된 키는 Ildasm.exe(IL 디스어셈블러)를 사용하거나 메모장과 같은 텍스트 편집기에서 어셈블리를 열어 어셈블리에서 쉽게 읽을 수 있습니다.

참고하십시오