I hope this will help
1 interface, 3 classes and 1 form
1.Interface ISXMLCipherKey. vb (create as class)
----------------------------------------------------------------
Imports System
Public Interface ISXMLCipherKey
ReadOnly Property CipherKey() As Byte()
Sub CipherKeyFinali ze()
End Interface
=============== =============== =============== ==========
2.Class SecureXML.vb
----------------------------------------------------------------
Imports System
Imports System.Xml
Imports System.IO
Imports System.Security .Cryptography
Public Structure SXMLDataHeader
Public token As Int32
Public Length As Int32
End Structure
Public Class SecureXML
Implements IDisposable
Public Const SXML_DATAHEADER _TOKEN As Int32 = &H190298
Private _ciphertext As MemoryStream
Private _cipherkey As ISXMLCipherKey
Private _cipherIV As Byte() = {&HA1, &HFF, &H1F, &H22, &HF0, &HBB,
&HBF, &HFB, &HCB, &H9A, &H2C, &H87, &H0, &HF, &H7D, &HDC}
Public Sub New(ByVal cipherkey As ISXMLCipherKey)
_ciphertext = New MemoryStream
_cipherkey = cipherkey
End Sub
Public Sub Dispose() Implements IDisposable.Dis pose
_ciphertext.Set Length(0)
_ciphertext = Nothing
_cipherkey = Nothing
End Sub
Public Sub Save(ByVal xmldoc As System.Xml.XmlD ocument, ByVal output
As Stream)
Dim alg As New RijndaelManaged
Dim encryptor As ICryptoTransfor m
Dim streamwriter As New BinaryWriter(ou tput)
Dim headerinfo As New SXMLDataHeader
Dim cipherStream As CryptoStream
_cipherkey.Ciph erKeyFinalize()
alg.Key = _cipherkey.Ciph erKey
alg.IV = _cipherIV
encryptor = alg.CreateEncry ptor()
cipherStream = New CryptoStream(_c iphertext, encryptor,
CryptoStreamMod e.Write)
'Save XML Encrypted
xmldoc.Save(cip herStream)
'Flush Our CipherStream Data to our CipherText Stream
cipherStream.Fl ushFinalBlock()
headerinfo.toke n = SXML_DATAHEADER _TOKEN
headerinfo.Leng th = _ciphertext.Len gth
'Output our header info to the output stream
WriteHeader(hea derinfo, streamwriter)
'Write Our CipherText to the end of the output stream
_ciphertext.Wri teTo(output)
'Reset for next use
Reset()
End Sub
Public Function Load(ByVal input As Stream) As
System.Xml.XmlD ocument
Dim streamreader As New BinaryReader(in put)
Dim headerinfo As SXMLDataHeader
Dim alg As New RijndaelManaged
Dim encryptor As ICryptoTransfor m
Dim cipherStream As CryptoStream
Dim xmldoc As New System.Xml.XmlD ocument
If Not ReadHeader(head erinfo, streamreader) Then
Return Nothing
End If
'Buffer the data from the input stream into our ciphertext
stream
_ciphertext.Wri te(streamreader .ReadBytes(head erinfo.Length), 0,
headerinfo.Leng th)
'Seek to the beginning of the ciphertext stream
_ciphertext.See k(0, SeekOrigin.Begi n)
'Init Decryption Alg
_cipherkey.Ciph erKeyFinalize()
alg.Key = _cipherkey.Ciph erKey
alg.IV = _cipherIV
encryptor = alg.CreateDecry ptor()
cipherStream = New CryptoStream(_c iphertext, encryptor,
CryptoStreamMod e.Read)
'Load our unencrypted XML Data
xmldoc.Load(cip herStream)
'Reset for next use
Reset()
'Return the new XML Doc to the user
Return xmldoc
End Function
Private Sub WriteHeader(ByV al header As SXMLDataHeader, ByVal output
As BinaryWriter)
output.Write(he ader.token)
output.Write(he ader.Length)
End Sub
Private Function ReadHeader(ByRe f header As SXMLDataHeader, ByVal
input As BinaryReader) As Boolean
Dim hdr As New SXMLDataHeader
hdr.token = input.ReadInt32 ()
If hdr.token <> SXML_DATAHEADER _TOKEN Then
Return False
End If
hdr.Length = input.ReadInt32 ()
If hdr.Length <= 0 Or hdr.Length > (input.BaseStre am.Length)
Then
Return False
End If
header = hdr
Return True
End Function
Public Sub Reset()
_ciphertext = Nothing
_ciphertext = New MemoryStream
End Sub
End Class
=============== =============== =============== ==========
3.Class SXMLComputerCip herKey.vb
----------------------------------------------------------------
Imports System
Imports System.Security .Cryptography
Imports System.IO
Public Class SXMLComputerCip herKey
Implements ISXMLCipherKey
Private _ComputerDrive As String
Private _cipherKey As Byte()
Public Sub New(ByVal harddriveletter As String)
_ComputerDrive = harddriveletter
End Sub
Public Sub CipherKeyFinali ze() Implements
ISXMLCipherKey. CipherKeyFinali ze
Dim _md5 As New MD5CryptoServic eProvider
Dim encoder As New Text.ASCIIEncod ing
Dim _tmpStr As String
Throw New
NotSupportedExc eption("SXMLCom puterCipherKey: :CipherKeyFinal ize() has
not yet been implmented and/or completed!")
End Sub
Public ReadOnly Property CipherKey() As Byte() Implements
ISXMLCipherKey. CipherKey
Get
Return _cipherKey
End Get
End Property
End Class
=============== =============== =============== ==========
4.Class SXMLCipherKey.v b
----------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Security .Cryptography
Public Class SXMLCipherKey
Implements ISXMLCipherKey
Private _keydata As String
Private _cipherkey As Byte()
Public Sub New(ByVal pass As String)
_keydata = pass
End Sub
Public Sub CipherKeyFinali ze() Implements
ISXMLCipherKey. CipherKeyFinali ze
Dim encoder As New Text.ASCIIEncod ing
Dim _tmpbytes As Byte()
Dim _md5 As New MD5CryptoServic eProvider
_tmpbytes = encoder.GetByte s(_keydata)
_cipherkey = _md5.ComputeHas h(_tmpbytes)
End Sub
Public ReadOnly Property CipherKey() As Byte() Implements
ISXMLCipherKey. CipherKey
Get
Return _cipherkey
End Get
End Property
End Class
=============== =============== =============== ==========
Form1
Imports System.IO.Path
Public Class Form1
Inherits System.Windows. Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows. Forms.TextBox
Friend WithEvents Button1 As System.Windows. Forms.Button
Friend WithEvents Button2 As System.Windows. Forms.Button
Friend WithEvents Label1 As System.Windows. Forms.Label
Friend WithEvents Button3 As System.Windows. Forms.Button
Friend WithEvents btnExit As System.Windows. Forms.Button
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Me.TextBox1 = New System.Windows. Forms.TextBox
Me.Button1 = New System.Windows. Forms.Button
Me.Button2 = New System.Windows. Forms.Button
Me.Label1 = New System.Windows. Forms.Label
Me.Button3 = New System.Windows. Forms.Button
Me.btnExit = New System.Windows. Forms.Button
Me.SuspendLayou t()
'
'TextBox1
'
Me.TextBox1.Loc ation = New System.Drawing. Point(4, 23)
Me.TextBox1.Nam e = "TextBox1"
Me.TextBox1.Siz e = New System.Drawing. Size(264, 20)
Me.TextBox1.Tab Index = 0
Me.TextBox1.Tex t = ""
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(8, 56)
Me.Button1.Name = "Button1"
Me.Button1.TabI ndex = 1
Me.Button1.Text = "Encrypt"
'
'Button2
'
Me.Button2.Loca tion = New System.Drawing. Point(100, 56)
Me.Button2.Name = "Button2"
Me.Button2.TabI ndex = 2
Me.Button2.Text = "Decrypt"
'
'Label1
'
Me.Label1.Locat ion = New System.Drawing. Point(20, -1)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing. Size(248, 23)
Me.Label1.TabIn dex = 3
Me.Label1.Text = "Enter XML file to encrypt"
Me.Label1.TextA lign =
System.Drawing. ContentAlignmen t.MiddleCenter
'
'Button3
'
Me.Button3.Loca tion = New System.Drawing. Point(270, 24)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing. Size(28, 23)
Me.Button3.TabI ndex = 4
Me.Button3.Text = "..."
'
'btnExit
'
Me.btnExit.Loca tion = New System.Drawing. Point(196, 56)
Me.btnExit.Name = "btnExit"
Me.btnExit.TabI ndex = 8
Me.btnExit.Text = "Exit"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(308, 86)
Me.Controls.Add (Me.btnExit)
Me.Controls.Add (Me.Button3)
Me.Controls.Add (Me.Label1)
Me.Controls.Add (Me.Button2)
Me.Controls.Add (Me.Button1)
Me.Controls.Add (Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout (False)
End Sub
#End Region
Dim files(2) As String
Dim mykey As New SXMLCipherKey(" Iouri")
Dim encstrm As IO.FileStream
Dim sxml As New SecureXML(mykey )
Dim sFile As String
'encrypt
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
files(0) = TextBox1.Text
files(1) = TextBox1.Text.S ubstring(0, TextBox1.Text.L ength -
sFile.Length) & "Encrypted. xml"
Me.EncryptXML()
MessageBox.Show ("XML file is encrypted")
End Sub
'decrypt
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
files(1) = TextBox1.Text.S ubstring(0, TextBox1.Text.L ength -
sFile.Length) & "Encrypted. xml"
files(2) = TextBox1.Text.S ubstring(0, TextBox1.Text.L ength -
sFile.Length) & "Decrypted. xml"
Me.DecryptXML()
MessageBox.Show ("XML file is decrypted")
End Sub
Private Sub EncryptXML()
Dim xmldoc As New Xml.XmlDocument
'crate Encrypted xml
encstrm = New IO.FileStream(f iles(1), IO.FileMode.Ope nOrCreate)
xmldoc.Load(fil es(0))
sxml.Save(xmldo c, encstrm)
encstrm.Flush()
encstrm.Close()
End Sub
Private Sub DecryptXML()
'create decrypted xml
encstrm = New IO.FileStream(f iles(1), IO.FileMode.Ope nOrCreate)
encstrm.Seek(0, IO.SeekOrigin.B egin)
sxml.Reset()
Dim newxml As Xml.XmlDocument
newxml = sxml.Load(encst rm)
newxml.Save(fil es(2))
End Sub
Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button3.Click
Dim od As New OpenFileDialog
With od
.InitialDirecto ry = "..\XMLFile s\"
.Filter = "XML Files (*)|;*.xml"
If .ShowDialog() = DialogResult.OK Then
TextBox1.Text = GetFullPath(.Fi leName) 'see import on
the top
sFile = GetFileName(.Fi leName)
End If
End With
If TextBox1.Text.L ength = 0 Then
MessageBox.Show ("Enter XML file")
Exit Sub
End If
End Sub
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExit.Click
Me.Close()
End Sub
End Class
"Debbie Carter" <dc*********@sb cglobal.net> wrote in message
news:M4******** **********@news svr31.news.prod igy.com:
Can XML files be easily encrypted?