方法 : X.509 証明書で XML 要素を復号化する

更新 : 2007 年 11 月

System.Security.Cryptography.Xml 名前空間のクラスを使用して、XML ドキュメント内の要素を暗号化および復号化できます。XML 暗号化は暗号化された XML データを変換または格納する標準の方法で、データを簡単に読み取られるおそれがありません。XML 暗号化標準の詳細については、http://www.w3.org/TR/xmldsig-core/ の World Wide Web Consortium (W3C) specification for XML Encryption を参照してください。

この例では、「方法 : X.509 証明書で XML 要素を暗号化する」で説明したメソッドを使用して暗号化された XML 要素を復号化します。<EncryptedData> 要素を検索し、要素を復号化し、要素を元のプレーンテキスト要素に置き換えます。

この手順のコード例は、現在のユーザー アカウントのローカル証明書ストアから X.509 証明書を使用して XML 要素を復号化します。この例では、DecryptDocument メソッドを使用して、X.509 証明書を自動的に取得し、<EncryptedData> 要素の <EncryptedKey> 要素に格納されたセッション キーを復号化します。次に、DecryptDocument メソッドは、セッション キーを自動的に使用して XML 要素を復号化します。

この例は、複数のアプリケーションが暗号化されたデータを共有する必要がある状況や、1 つのアプリケーションが実行するたびに暗号化されたデータを保存する必要のある状況に適しています。

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" 要素が含まれていることも前提としています。test.xml という名前のファイルに次の XML を配置し、その 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 暗号化と XML デジタル署名