Imports System
Imports System.XML
Imports System.Security.Cryptography
Imports System.Security.Cryptography.Xml
Public Class Encrypt
Public Function startEncrypt(ByVal filename As String) As Boolean
Dim xmldoc As New XmlDocument()
Try
xmldoc.Load(filename)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
' Create a new TripleDES object, TripleDes will be the algorithm used to encrypt the XML data
Dim sharedkey As New TripleDESCryptoServiceProvider()
''Save this key to disk to enable the recipient to decrypt
Dim writer2 As IO.StreamWriter = New IO.StreamWriter("SharedTDESKey.txt")
Dim str As String = Convert.ToBase64String(sharedkey.Key)
writer2.WriteLine(str)
writer2.Close()
'Create a new EncryptedXML object
Dim exml As EncryptedXml = New EncryptedXml(xmldoc)
'Select the Billing element to be encrypted
Dim conifigElem As XmlElement = CType(xmldoc.SelectSingleNode("/configuration/appSettings"), XmlElement)
'Encrypt the billing element data using the TripleDES alogrithm, save the results into a byte array
Dim encryptedBilling As Byte() = exml.EncryptData(conifigElem, sharedkey, 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 = encryptedBilling
' Replace the plaintext XML elemnt with an EncryptedData element.
EncryptedXml.ReplaceElement(conifigElem, ed, False)
'Write the encrypted data to disk
Try
xmldoc.Save(filename)
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Return True
End Function
End Class