EncryptionMethod Class
Encapsulates the encryption algorithm used for XML encryption.
Assembly: System.Security (in System.Security.dll)
The <EncryptionMethod> tag uses a Uniform Resource Identifier (URI) to identify the encryption algorithm that must be used to decrypt XML data. The EncryptionMethod class encapsulates the <EncryptionMethod> tag defined by the World Wide Web Consortium XML Encryption specification located at http://www.w3.org/TR/xmlenc-core/.
Use the EncryptionMethod class to identify the encryption algorithm associated with the EncryptedData element or the EncryptedKey element.
Use one of the URI identifiers defined by the World Wide Web Consortium XML Encryption specification located at http://www.w3.org/TR/xmlenc-core/. All URI identifiers are accessible as static fields of the EncryptedXml class.
The following code example demonstrates how to create a simple utility class that uses the TripleDES algorithm to encrypt an XML document.
Imports System Imports System.Xml Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Module Program Sub Main(ByVal args() As String) ' Create an XmlDocument object. Dim xmlDoc As New XmlDocument() ' Load an XML file into the XmlDocument object. xmlDoc.PreserveWhitespace = True xmlDoc.Load("test.xml") ' Create a new TripleDES key. Dim tDESkey As New TripleDESCryptoServiceProvider() ' Create a new instance of the TrippleDESDocumentEncryption object ' defined in this sample. Dim xmlTDES As New TrippleDESDocumentEncryption(xmlDoc, tDESkey) Try ' Encrypt the "creditcard" element. xmlTDES.Encrypt("creditcard") ' Display the encrypted XML to the console. Console.WriteLine("Encrypted XML:") Console.WriteLine() Console.WriteLine(xmlTDES.Doc.OuterXml) ' Decrypt the "creditcard" element. xmlTDES.Decrypt() ' Display the encrypted XML to the console. Console.WriteLine() Console.WriteLine("Decrypted XML:") Console.WriteLine() Console.WriteLine(xmlTDES.Doc.OuterXml) Catch e As Exception Console.WriteLine(e.Message) Finally ' Clear the TripleDES key. xmlTDES.Clear() End Try End Sub 'Main End Module 'Program Class TrippleDESDocumentEncryption Protected docValue As XmlDocument Protected algValue As TripleDES Public Sub New(ByVal Doc As XmlDocument, ByVal Key As TripleDES) If Not (Doc Is Nothing) Then docValue = Doc Else Throw New ArgumentNullException("Doc") End If If Not (Key Is Nothing) Then algValue = Key Else Throw New ArgumentNullException("Key") End If End Sub Public Property Doc() As XmlDocument Get Return docValue End Get Set(ByVal value As XmlDocument) docValue = value End Set End Property Public Property Alg() As TripleDES Get Return algValue End Get Set(ByVal value As TripleDES) algValue = value End Set End Property Public Sub Clear() If Not (algValue Is Nothing) Then algValue.Clear() Else Throw New Exception("No TripleDES key was found to clear.") End If End Sub Public Sub Encrypt(ByVal Element As String) ' Find the element by name and create a new ' XmlElement object. Dim inputElement As XmlElement = docValue.GetElementsByTagName(Element)(0) ' If the element was not found, throw an exception. If inputElement Is Nothing Then Throw New Exception("The element was not found.") End If ' Create a new EncryptedXml object. Dim exml As New EncryptedXml(docValue) ' Encrypt the element using the symmetric key. Dim rgbOutput As Byte() = exml.EncryptData(inputElement, algValue, False) ' Create an EncryptedData object and populate it. Dim ed As New EncryptedData() ' Specify the namespace URI for XML encryption elements. ed.Type = EncryptedXml.XmlEncElementUrl ' Specify the namespace URI for the TrippleDES algorithm. ed.EncryptionMethod = New EncryptionMethod(EncryptedXml.XmlEncTripleDESUrl) ' Create a CipherData element. ed.CipherData = New CipherData() ' Set the CipherData element to the value of the encrypted XML element. ed.CipherData.CipherValue = rgbOutput ' Replace the plaintext XML elemnt with an EncryptedData element. EncryptedXml.ReplaceElement(inputElement, ed, False) End Sub Public Sub Decrypt() ' XmlElement object. Dim encryptedElement As XmlElement = docValue.GetElementsByTagName("EncryptedData")(0) ' If the EncryptedData element was not found, throw an exception. If encryptedElement Is Nothing Then Throw New Exception("The EncryptedData element was not found.") End If ' Create an EncryptedData object and populate it. Dim ed As New EncryptedData() ed.LoadXml(encryptedElement) ' Create a new EncryptedXml object. Dim exml As New EncryptedXml() ' Decrypt the element using the symmetric key. Dim rgbOutput As Byte() = exml.DecryptData(ed, algValue) ' Replace the encryptedData element with the plaintext XML elemnt. exml.ReplaceData(encryptedElement, rgbOutput) End Sub End Class
The following code example demonstrates how to encrypt an XML document using a symmetric key. This example does not include any key information in the encrypted XML document.
Imports System Imports System.Xml Imports System.Security.Cryptography Imports System.Security.Cryptography.Xml Module Program Sub Main(ByVal args() As String) ' Create an XmlDocument object. Dim xmlDoc As New XmlDocument() ' Load an XML file into the XmlDocument object. Try xmlDoc.PreserveWhitespace = True xmlDoc.Load("test.xml") Catch e As Exception Console.WriteLine(e.Message) Return End Try ' Create a new TripleDES key. Dim tDESkey As New TripleDESCryptoServiceProvider() Try ' Encrypt the "creditcard" element. Encrypt(xmlDoc, "creditcard", tDESkey) ' Display the encrypted XML to the console. Console.WriteLine("Encrypted XML:") Console.WriteLine() Console.WriteLine(xmlDoc.OuterXml) ' Decrypt the "creditcard" element. Decrypt(xmlDoc, tDESkey) ' Display the encrypted XML to the console. Console.WriteLine() Console.WriteLine("Decrypted XML:") Console.WriteLine() Console.WriteLine(xmlDoc.OuterXml) Catch e As Exception Console.WriteLine(e.Message) Finally ' Clear the TripleDES key. tDESkey.Clear() End Try End Sub Sub Encrypt(ByVal Doc As XmlDocument, ByVal ElementToEncrypt As String, ByVal Alg As SymmetricAlgorithm) ' Check the arguments. If Doc Is Nothing Then Throw New ArgumentNullException("Doc") End If If ElementToEncrypt Is Nothing Then Throw New ArgumentNullException("ElementToEncrypt") End If If Alg Is Nothing Then Throw New ArgumentNullException("Alg") End If ''''''''''''''''''''''''''''''''''''''''''''''''''' ' Find the specified element in the XmlDocument ' object and create a new XmlElemnt object. ''''''''''''''''''''''''''''''''''''''''''''''''''' Dim elementEncrypt As XmlElement = Doc.GetElementsByTagName(ElementToEncrypt)(0) ' Throw an XmlException if the element was not found. If ElementToEncrypt Is Nothing Then Throw New XmlException("The specified element was not found") End If ''''''''''''''''''''''''''''''''''''''''''''''''''' ' Create a new instance of the EncryptedXml class ' and use it to encrypt the XmlElement with the ' symmetric key. ''''''''''''''''''''''''''''''''''''''''''''''''''' Dim eXml As New EncryptedXml() Dim encryptedElement As Byte() = eXml.EncryptData(elementEncrypt, Alg, False) ''''''''''''''''''''''''''''''''''''''''''''''''''' ' Construct an EncryptedData object and populate ' it with the desired encryption information. ''''''''''''''''''''''''''''''''''''''''''''''''''' Dim edElement As New EncryptedData() edElement.Type = EncryptedXml.XmlEncElementUrl ' Create an EncryptionMethod element so that the ' receiver knows which algorithm to use for decryption. ' Determine what kind of algorithm is being used and ' supply the appropriate URL to the EncryptionMethod element. Dim encryptionMethod As String = Nothing If TypeOf Alg Is TripleDES Then encryptionMethod = EncryptedXml.XmlEncTripleDESUrl ElseIf TypeOf Alg Is DES Then encryptionMethod = EncryptedXml.XmlEncDESUrl ElseIf TypeOf Alg Is Rijndael Then Select Case Alg.KeySize Case 128 encryptionMethod = EncryptedXml.XmlEncAES128Url Case 192 encryptionMethod = EncryptedXml.XmlEncAES192Url Case 256 encryptionMethod = EncryptedXml.XmlEncAES256Url End Select Else ' Throw an exception if the transform is not in the previous categories Throw New CryptographicException("The specified algorithm is not supported for XML Encryption.") End If edElement.EncryptionMethod = New EncryptionMethod(encryptionMethod) ' Add the encrypted element data to the ' EncryptedData object. edElement.CipherData.CipherValue = encryptedElement ''''''''''''''''''''''''''''''''''''''''''''''''''' ' Replace the element from the original XmlDocument ' object with the EncryptedData element. ''''''''''''''''''''''''''''''''''''''''''''''''''' EncryptedXml.ReplaceElement(elementEncrypt, edElement, False) End Sub 'Encrypt Sub Decrypt(ByVal Doc As XmlDocument, ByVal Alg As SymmetricAlgorithm) ' Check the arguments. If Doc Is Nothing Then Throw New ArgumentNullException("Doc") End If If Alg Is Nothing Then Throw New ArgumentNullException("Alg") End If ' Find the EncryptedData element in the XmlDocument. Dim encryptedElement As XmlElement = Doc.GetElementsByTagName("EncryptedData")(0) ' If the EncryptedData element was not found, throw an exception. If encryptedElement Is Nothing Then Throw New XmlException("The EncryptedData element was not found.") End If ' Create an EncryptedData object and populate it. Dim edElement As New EncryptedData() edElement.LoadXml(encryptedElement) ' Create a new EncryptedXml object. Dim exml As New EncryptedXml() ' Decrypt the element using the symmetric key. Dim rgbOutput As Byte() = exml.DecryptData(edElement, Alg) ' Replace the encryptedData element with the plaintext XML element. exml.ReplaceData(encryptedElement, rgbOutput) End Sub End Module
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.