从解密字符串解析XML文档



我正在写一个Vb。Net应用程序,从PHP服务器读取加密的XML文件。我使用下面的代码片段:

PHP加密&VB.net解密

特别是Richard Varno的答案和代码。我可以将PHP服务器上的原始XML文件与VB上的解密版本进行比较。

问题是,当我加载解密版本到一个XML文档在Vb。我只得到一个空文档。

如果我从PHP服务器加载未加密的版本,它是好的。我看不出两者之间有什么明显的区别除了一个被加密了,然后又被解密了。它们都是字符串,都被压缩过,所以为什么不能工作呢?

下面是我在未加密字符串中读取的代码:

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")

现在上面的工作,但如果我替换

document.LoadXml(contents)

:

document.LoadXml(decrypted)

my XML document is empty.

结果是解密函数用空字符填充解密字符串的末尾。当被视为十六进制时,这些显示为00,但我通过控制台输出。Writeline根本没有显示这些

空字符不是有效的XML,这就是为什么我没有得到任何输出。

解决方案是编写一个函数,该函数遍历解密字符串并使用(在我的情况下是在。net 4.0) XmlConvert.IsXmlChar(ch)函数剥离这些。

去掉空字符后,我得到了预期的解密输出。

最新更新