To decrypt an XML document using a certificate

Source: Internet
Author: User
Tags decrypt end
xml| decryption

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> You can use classes in the System.Security.Cryptography.Xml namespace to encrypt the elements in an XML document.) XML encryption is a standard way to exchange or store encrypted XML data without having to worry about data being easily read. For more information about the XML Encryption standard, see the WWW Federation (World Wide Web) specification on XML encryption, located on Http://www.w3.org/TR/xmldsig-core/. </test> ")

' Open ' X.509 ' current User ' store in read only mode.
Dim Store as New X509store (Storelocation.currentuser)
Store. Open (openflags.readonly)
' Place all certificates in a 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 to 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 is not found.
If Elementtoencrypt is nothing Then
Throw New xmlexception ("The specified element is 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

' Of course, you can also use the Loadxml method to decrypt a piece of text



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.