By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,959 Members | 1,196 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,959 IT Pros & Developers. It's quick & easy.

XML File Question?

P: n/a
Can XML files be easily encrypted?

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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 CipherKeyFinalize()

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.Dispose
_ciphertext.SetLength(0)
_ciphertext = Nothing
_cipherkey = Nothing
End Sub

Public Sub Save(ByVal xmldoc As System.Xml.XmlDocument, ByVal output
As Stream)
Dim alg As New RijndaelManaged
Dim encryptor As ICryptoTransform
Dim streamwriter As New BinaryWriter(output)
Dim headerinfo As New SXMLDataHeader
Dim cipherStream As CryptoStream

_cipherkey.CipherKeyFinalize()
alg.Key = _cipherkey.CipherKey
alg.IV = _cipherIV
encryptor = alg.CreateEncryptor()

cipherStream = New CryptoStream(_ciphertext, encryptor,
CryptoStreamMode.Write)
'Save XML Encrypted
xmldoc.Save(cipherStream)

'Flush Our CipherStream Data to our CipherText Stream
cipherStream.FlushFinalBlock()

headerinfo.token = SXML_DATAHEADER_TOKEN
headerinfo.Length = _ciphertext.Length

'Output our header info to the output stream
WriteHeader(headerinfo, streamwriter)
'Write Our CipherText to the end of the output stream
_ciphertext.WriteTo(output)

'Reset for next use
Reset()
End Sub

Public Function Load(ByVal input As Stream) As
System.Xml.XmlDocument
Dim streamreader As New BinaryReader(input)
Dim headerinfo As SXMLDataHeader
Dim alg As New RijndaelManaged
Dim encryptor As ICryptoTransform
Dim cipherStream As CryptoStream

Dim xmldoc As New System.Xml.XmlDocument

If Not ReadHeader(headerinfo, streamreader) Then
Return Nothing
End If

'Buffer the data from the input stream into our ciphertext
stream
_ciphertext.Write(streamreader.ReadBytes(headerinf o.Length), 0,
headerinfo.Length)

'Seek to the beginning of the ciphertext stream
_ciphertext.Seek(0, SeekOrigin.Begin)

'Init Decryption Alg
_cipherkey.CipherKeyFinalize()
alg.Key = _cipherkey.CipherKey
alg.IV = _cipherIV
encryptor = alg.CreateDecryptor()

cipherStream = New CryptoStream(_ciphertext, encryptor,
CryptoStreamMode.Read)

'Load our unencrypted XML Data
xmldoc.Load(cipherStream)

'Reset for next use
Reset()

'Return the new XML Doc to the user
Return xmldoc
End Function

Private Sub WriteHeader(ByVal header As SXMLDataHeader, ByVal output
As BinaryWriter)
output.Write(header.token)
output.Write(header.Length)
End Sub

Private Function ReadHeader(ByRef 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.BaseStream.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 SXMLComputerCipherKey.vb
----------------------------------------------------------------
Imports System
Imports System.Security.Cryptography
Imports System.IO

Public Class SXMLComputerCipherKey
Implements ISXMLCipherKey

Private _ComputerDrive As String
Private _cipherKey As Byte()

Public Sub New(ByVal harddriveletter As String)
_ComputerDrive = harddriveletter
End Sub

Public Sub CipherKeyFinalize() Implements
ISXMLCipherKey.CipherKeyFinalize
Dim _md5 As New MD5CryptoServiceProvider
Dim encoder As New Text.ASCIIEncoding
Dim _tmpStr As String

Throw New
NotSupportedException("SXMLComputerCipherKey::Ciph erKeyFinalize() 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.vb
----------------------------------------------------------------
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 CipherKeyFinalize() Implements
ISXMLCipherKey.CipherKeyFinalize
Dim encoder As New Text.ASCIIEncoding
Dim _tmpbytes As Byte()
Dim _md5 As New MD5CryptoServiceProvider

_tmpbytes = encoder.GetBytes(_keydata)
_cipherkey = _md5.ComputeHash(_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.
InitializeComponent()

'Add any initialization after the InitializeComponent() 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.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
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.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(4, 23)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(264, 20)
Me.TextBox1.TabIndex = 0
Me.TextBox1.Text = ""
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 56)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Encrypt"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(100, 56)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text = "Decrypt"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(20, -1)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(248, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Enter XML file to encrypt"
Me.Label1.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(270, 24)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(28, 23)
Me.Button3.TabIndex = 4
Me.Button3.Text = "..."
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(196, 56)
Me.btnExit.Name = "btnExit"
Me.btnExit.TabIndex = 8
Me.btnExit.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
files(0) = TextBox1.Text
files(1) = TextBox1.Text.Substring(0, TextBox1.Text.Length -
sFile.Length) & "Encrypted.xml"
Me.EncryptXML()
MessageBox.Show("XML file is encrypted")
End Sub

'decrypt
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
files(1) = TextBox1.Text.Substring(0, TextBox1.Text.Length -
sFile.Length) & "Encrypted.xml"
files(2) = TextBox1.Text.Substring(0, TextBox1.Text.Length -
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(files(1), IO.FileMode.OpenOrCreate)

xmldoc.Load(files(0))

sxml.Save(xmldoc, encstrm)
encstrm.Flush()
encstrm.Close()

End Sub
Private Sub DecryptXML()
'create decrypted xml
encstrm = New IO.FileStream(files(1), IO.FileMode.OpenOrCreate)
encstrm.Seek(0, IO.SeekOrigin.Begin)

sxml.Reset()
Dim newxml As Xml.XmlDocument
newxml = sxml.Load(encstrm)

newxml.Save(files(2))
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Dim od As New OpenFileDialog
With od
.InitialDirectory = "..\XMLFiles\"
.Filter = "XML Files (*)|;*.xml"
If .ShowDialog() = DialogResult.OK Then
TextBox1.Text = GetFullPath(.FileName) 'see import on
the top
sFile = GetFileName(.FileName)
End If
End With

If TextBox1.Text.Length = 0 Then
MessageBox.Show("Enter XML file")
Exit Sub
End If

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

End Class


"Debbie Carter" <dc*********@sbcglobal.net> wrote in message
news:M4******************@newssvr31.news.prodigy.c om:
Can XML files be easily encrypted?


Nov 21 '05 #2

P: n/a
Scorpion...thank you VERY much!

Debbie

Nov 21 '05 #3

P: n/a
Anytime....
"Debbie Carter" <dc*********@sbcglobal.net> wrote in message
news:oV******************@newssvr33.news.prodigy.c om:
Scorpion...thank you VERY much!

Debbie


Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.