使用证书对XML文档进行加解密

80酷酷网    80kuku.com

  xml|解密

Imports System
Imports System.Xml
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Imports System.Security.Cryptography.X509Certificates
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Create an XmlDocument object.
        Dim xmlDoc As New XmlDocument()
        ' Load an XML file into the XmlDocument object.
        xmlDoc.PreserveWhitespace = True
        xmlDoc.LoadXml("<test>可以使用 System.Security.Cryptography.Xml 命名空间中的类对 XML 文档中的元素进行加密。XML 加密是交换或存储加密的 XML 数据的标准方法,无需担心数据被轻易读取。有关 XML 加密标准的更多信息,请参见位于 上的关于 XML 加密的 WWW 联合会 (W3C) 规范。 </test>")

        ' Open the X.509 "Current User" store in read only mode.
        Dim store As New X509Store(StoreLocation.CurrentUser)
        store.Open(OpenFlags.ReadOnly)
        ' Place all certificates in an X509Certificate2Collection object.
        Dim certCollection As X509Certificate2Collection = store.Certificates
        Dim cert As X509Certificate2 = Nothing

        ' Loop through each certificate and find the certificate
        ' with the appropriate name.
        Dim c As X509Certificate2
        For Each c In certCollection
            'If c.Subject = "CN=MASZSB" Then
            cert = c
            MsgBox(cert.Subject)
            'Exit For
            'End If
        Next c
        'If cert Is Nothing Then
        'Throw New CryptographicException("The X.509 certificate could not be found.")
        'End If

        ' Close the store.
        store.Close()
        ' Encrypt the "creditcard" element.
        Encrypt(xmlDoc, "test", cert)

        ' Save the XML document.
        xmlDoc.Save("C:\OK.XML")
        ' Display the encrypted XML to the console.
        MsgBox("Encrypted XML:")
        MsgBox(xmlDoc.OuterXml)
        Decrypt(xmlDoc)
        MsgBox(xmlDoc.OuterXml)

    End Sub 'Main

        Sub Encrypt(ByVal Doc As XmlDocument, ByVal ElementToEncryptName As String, ByVal Cert As X509Certificate2)
            ' Check the arguments. 
            If Doc Is Nothing Then
                Throw New ArgumentNullException("Doc")
            End If
            If ElementToEncryptName Is Nothing Then
                Throw New ArgumentNullException("ElementToEncrypt")
            End If
            If Cert Is Nothing Then
                Throw New ArgumentNullException("Cert")
            End If
            ''''''''''''''''''''''''''''''''''''''''''''''''
            ' Find the specified element in the XmlDocument
            ' object and create a new XmlElemnt object.
            ''''''''''''''''''''''''''''''''''''''''''''''''
            Dim elementToEncrypt As XmlElement = Doc.GetElementsByTagName(ElementToEncryptName)(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
            ' X.509 Certificate.
            ''''''''''''''''''''''''''''''''''''''''''''''''
            Dim eXml As New EncryptedXml()

            ' Encrypt the element.
            Dim edElement As EncryptedData = eXml.Encrypt(elementToEncrypt, Cert)
            ''''''''''''''''''''''''''''''''''''''''''''''''
            ' Replace the element from the original XmlDocument
            ' object with the EncryptedData element.
            ''''''''''''''''''''''''''''''''''''''''''''''''
            EncryptedXml.ReplaceElement(elementToEncrypt, edElement, False)
    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 Class

'当然 也可以使用LOADXML的方法 对一段文本进行加解密
 



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: