Philip Lee
Philip Lee

Reputation: 189

Parsing an XML document from a decrypted string

I'm writing a Vb.Net application that reads an encrypted XML file from a PHP server. I'm using the code snippets found here:

PHP Encryption & VB.net Decryption

specifically Richard Varno's answer and code. I can compare the original XML file on the PHP server to the decrypted version on VB.Net and they are identical.

The problem is that when I load the decrypted version into an XML document in Vb.Net I just get an empty document.

If I load the unencrypted version from the PHP server it's fine. I can't see any obvious difference between the two other than that one has been encrypted and then de-encrypted. Both are strings, and both have been Gzipped so why would this not work ?

Here's my code to read in the unencrypted string:

Dim request As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create(lookupUrl)

' Tell the server that we want it compressed
request.AutomaticDecompression = DecompressionMethods.GZip
request.Timeout = 3000 ' Set 3 second timeout

' Parse the contents from the response to a stream object
stream = response.GetResponseStream()

' Create a reader for the stream object
Dim reader As New StreamReader(stream)

' Read from the stream object using the reader, put the encrypted contents in a string
Dim contents As String = reader.ReadToEnd()
' Put de-encrypted contents into another string
Dim decrypted As String = ""

' Create a new, empty XML document
Dim document As New System.Xml.XmlDocument()
Console.WriteLine("Received: " & contents)

' De-encrypt the data from the response from the server
decrypted = DecryptRJ256(Globals.sKy, Globals.sIV, contents)
Console.WriteLine("Decrypted: " & decrypted)

' Load the contents into the XML document
document.LoadXml(contents)

Dim nodes As XmlNodeList =     document.DocumentElement.SelectNodes("//results/Node1")

Now the above works but if I replace

document.LoadXml(contents)

with:

document.LoadXml(decrypted)

my XML document is empty.

Upvotes: 1

Views: 211

Answers (1)

Philip Lee
Philip Lee

Reputation: 189

It turns out that the decryption function was padding out the end of the decrypted string with null characters. When viewed as hex these appeared as 00 but the output I had via console.writeline wasn't showing these at all.

Null characters are not valid XML which is why I wasn't getting any output.

The solution was to write a function which walked the decrypted string and stripped these using (in my case on .Net 4.0) the XmlConvert.IsXmlChar(ch) function.

Once stripped of null characters I got the expected decrypted output.

Upvotes: 1

Related Questions