473,325 Members | 2,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

XML File Question?

Can XML files be easily encrypted?

Nov 21 '05 #1
3 1561
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
Scorpion...thank you VERY much!

Debbie

Nov 21 '05 #3
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
1
by: Marc Cromme | last post by:
I would like to ask a question about (good ?) style and possibilities in mixing C FILE* and C++ file streams. The background is that I want to use the C libpng library from within C++, but I...
21
by: siroregano | last post by:
Hi Everyone- I'm new to this group, and almost-as-new to asking programming questions publicly, so please forgive me if I miss a convention or two! I have a text file, around 40,000 lines...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
22
by: petermichaux | last post by:
Hi, I'm curious about server load and download time if I use one big javascript file or break it into several smaller ones. Which is better? (Please think of this as the first time the scripts...
2
by: sani8888 | last post by:
Hi everybody I am a beginner with C++ programming. And I need some help. How can I start with this program *********** The program is using a text file of information as the source of the...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
6
by: portCo | last post by:
Hello there, I am creating a vb application which is some like like a questionare. Application read a text file which contains many questions and display one question and the input is needed...
4
by: saytri | last post by:
Hi guys! I am making a quiz. The questions are stored in a binary file. I made an option in the quiz where a user can add a question to the binary file. Altough this works, the problem is that...
14
by: =?Utf-8?B?R2lkaQ==?= | last post by:
Hi, In my windows applicationm, i need to excute a batch file. this batch file throws some text and questions to the screen, i need to catch the standard Output, check if it's a question, in...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.