Page 1 of 1

Encrypting XML Made simple... Rate Topic: ****- 1 Votes

#1 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Posted 01 May 2010 - 11:38 AM

Do you like the functionality of XML, but found the tutorial for encrypting elements way to complicated - then this tutorial is for you!

First decide what encryption you want to use - RSA, SHA, etc...

For my example I will use a MD5/TripleDes.
Imports System.Security.Cryptography
Imports System.Text

Public Class CryptoZ
  Private Shared DES As New TripleDESCryptoServiceProvider
  Private Shared MD5 As New MD5CryptoServiceProvider

  Public Shared Function MD5Hash(ByVal value As String) As Byte()
    Return MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(value))
  End Function

  Public Shared Function Encrypt(ByVal stringToEncrypt As String, ByVal key As String) As String
    DES.Key = MD5Hash(key)
    DES.Mode = CipherMode.ECB
    Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt)
    Return Convert.ToBase64String(DES.CreateEncryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
  End Function

  Public Shared Function Decrypt(ByVal encryptedString As String, ByVal key As String) As String
    Try
      DES.Key = MD5Hash(key)
      DES.Mode = CipherMode.ECB
      Dim Buffer As Byte() = Convert.FromBase64String(encryptedString)
      Return ASCIIEncoding.ASCII.GetString(DES.CreateDecryptor().TransformFinalBlock(Buffer, 0, Buffer.Length))
    Catch
      MessageBox.Show("Wrong Key Number, decryption not available!")
      Return Nothing
    End Try
  End Function
End Class



Set up a valid XML format:

Dim sXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
sXML = String.Concat(sXML, Environment.NewLine, "<Root>")
sXML = String.Concat(sXML, Environment.NewLine, "     ", "<One />")
sXML = String.Concat(sXML, Environment.NewLine, "     ", "<Two />")
sXML = String.Concat(sXML, Environment.NewLine, "</Root>")



Make the file:

Using sr As New StreamWriter({file path})
   sr.Write(CryptoZ.Encrypt(sXML, "mykey"))
End Using




Open the file:

Using sr As New StreamReader({file path})
  'tb is a textbox for viewing
   tb.Text = CryptoZ.Encrypt(sr.ReadToEnd, "mykey")
End Using




Now lets make this a usable XML document:

Dim encryptedXmlFile As New XmlDocument
Using sr As New StreamReader({file path})
  encryptedXmlFile.LoadXml(CryptoZ.Decrypt(sr.ReadToEnd, "mykey"))
End Using


Mod Edit: said:

{file path} is being used a indication to where the file is being loaded from. Replace it with the location must appropriate to you.


Now lets make some changes, using the normal ways you work with an XmlDocument:

For Each node As XmlNode In encryptedXmlFile.DocumentElement.ChildNodes
  'lsb is a listbox, just to show the elements
   lsb.Items.Add(node.Name) 
  'make some changes 
   node.InnerText = "test"
Next




Now instead of calling encyrptedXmlFile.Save({file path}) cause we don't want to make the regular XML file, we do this:

Using sr As New StreamWriter({file path})
   sr.Write(CryptoZ.Encrypt(encyrptedXmlFile.OuterXml, "mykey"))
End Using


The XmlDocument only exists in memory so no one can see it! Just call encyrptedXmlFile = Nothing when done. that it, enjoy!!!

This post has been edited by AdamSpeight2008: 01 May 2010 - 01:33 PM
Reason for edit:: Added a clarification.


Is This A Good Question/Topic? 1
  • +

Replies To: Encrypting XML

#2 zela  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-November 10

Posted 24 November 2010 - 02:17 AM

I have read this but need clarification regarding on my search. The .xml database file was already created which contain various information/data and my software reads the file and display from them. How do i decrypt the .xml file and how do i put a code for the decryption in my program code so that the .xml database file be decrypt at runtime and read from the file and put an output on my form as needed. I am not lookin for a complicated encryption or decryption method, just a simple one will be sufficient.

thanks for the respond though.

dude.
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9073
  • View blog
  • Posts: 34,115
  • Joined: 12-June 08

Posted 24 November 2010 - 10:23 AM

Simply... hmm... well here's the steps.

1. Start off with no XML.
2. Have a datasaet created.
3. Program runs.. adds to the dataset.
4. Here you would usually have the dataset 'save as xml'.
4.a. You can do that but then would have to take that file.. run your encryption on it... and delete the xml.
4.b. Alternatively you would have your dataset 'save as xml' but to a memory stream. Encrypt that memory stream.. save that as a file.
5. Reloading would branch as well..
5.a. Take your file.. run your decryption to get the xml file.. load that back into your dataset. Delete the xml file you decrypted.
5.b. Read the encrypted file into a file stream... decrypt it in memory.. and push that into the dataset.
Was This Post Helpful? 0
  • +
  • -

#4 ???  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 48
  • Joined: 25-November 10

Posted 25 April 2011 - 03:55 PM

Thanks it works!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1