• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

xml 搞代码 7年前 (2018-06-15) 121次浏览 已收录 0个评论

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

http://www.gaodaima.com/33896.html使用证书对XML文档进行加解密_xml

        ‘ 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 加密标准的更多信息,请参见位于 http://www.w3.org/TR/xmldsig-core/ 上的关于 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的方法 对一段文本进行加解密
 

欢迎大家阅读《使用证书对XML文档进行加解密_xml》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:使用证书对XML文档进行加解密_xml

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址