Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello all,

 

the predicament for the evening.

 

I have a well formed xml file used for configuration in an application. I want to decrypt/encrypt this file when it gets used - thus when the app loads it decrypts and loads the contents in an xmldocument - when the app exists it encrypts and saves the contents back to the file. I figured this would involve the following pseudo-code.

 

on app load

1. read file into a byte array

2. run byte array through class.decryptfunction - returns another byte array

3. read returned byte array into memory stream

4. serialize memorystream into well-formed xml doc

5. load memorystream into xmldocument object for processing

 

without any encryption, I tried declaring an array of bytes and loading the contents of the xmldoc into this array. I then created a memorystream and read the contents of the byte array into that stream. I finally tried load the memorystram into the xmldocument.

 

Try

Dim mymem As MemoryStream = New MemoryStream

Dim ar() As Byte = PrepFile(FileLocation)

 

mymem.Write(ar, 0, ar.Length)

 

Console.WriteLine("byte ar: " & mymem.Length)

configfile.Load(mymem)

Console.WriteLine(configfile.OuterXml)

Catch e As Exception

Console.WriteLine("Exception: {0}", e.ToString())

End Try

 

the array and mem stream have data - but I receive an XMLException - root element missing. I'm guessing that this has something to do with serialization.

 

Does anyone have any ideas how to accomplish this or can they point me to a good article or two.

 

thanks,

 

inzo

he who forgets will be destined to remember... (E.Vedder)
Posted

Still not working.

 

Okay - I took a look at the article you suggested PDamp and got some things out of it. I came up with the following test mod - which still isn't functioning.

 

Module Module1

 

Sub Main()

 

Dim cryptoprovider As RijndaelManaged = New RijndaelManaged

Dim key As Byte() = {11, 2, 7, 24, 16, 22, 4, 38, 27, 3, 11, 10, 17, 15, 6, 23}

Dim iv As Byte() = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}

Dim xtw As XmlTextWriter

Dim filename As String = "c:\testing\test.xml"

 

Dim FileWriter As FileStream = New FileStream(filename, FileMode.Create)

 

Dim CryptoWriter As CryptoStream = New CryptoStream(FileWriter, cryptoprovider.CreateEncryptor(key, iv), CryptoStreamMode.Write)

xtw = New XmlTextWriter(CryptoWriter, System.Text.Encoding.UTF8)

'FileWriter.Close()

 

Console.WriteLine("File Encrypted")

Console.ReadLine()

xtw.Close()

 

Dim FileReader As FileStream = New FileStream(filename, FileMode.Open)

Dim CryptoReader As CryptoStream = New CryptoStream(FileReader, cryptoprovider.CreateDecryptor(key, iv), CryptoStreamMode.Read)

'FileReader.Close()

 

Dim XmlDoc As XmlDocument = New XmlDocument

Dim XmlReader As XmlTextReader = New XmlTextReader(CryptoReader)

 

Try

XmlDoc.Load(XmlReader)

Console.WriteLine("file..............")

Console.WriteLine(XmlDoc.OuterXml.ToString)

Console.ReadLine()

Catch ex As XmlException

Console.WriteLine(ex.ToString)

Console.ReadLine()

 

End Try

 

End Sub

 

End Module

 

I get a system.xml.xmlexception - the root element is missing error.

 

The file is being encrypted but it seems that either the decryption is not working or there is garbage going in there that I can't parse out.

 

Any ideas?

 

inzo

he who forgets will be destined to remember... (E.Vedder)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...