2

I've been using the DOMDocument object from VB6 (MSXML) to create and save an XML file that has an encrypted string. However, this string I think has certain special characters...

<EncryptedPassword>ÆÔ¤ïÎ
    ߯8KHÖN›¢)Þ,qiãÔÙ</EncryptedPassword>

With this, I go into my C# Project, and de-serialise this XML file in UTF-8 encoding and it fails on this string. I've tried serialisation via ASCII and this gets a couple characters more, but still fails. If I put a plain text string in this place, all is ok! :(

I'm thinking that maybe I'm better converting the string into an MD5 type string from VB6 first, and decoding the MD5 string in .NET and then decrypting the actual string with special characters, but it's an extra step to code all this up and was hoping someone might have a better idea for me here?

Thanks in advance!

Deanna
  • 23,876
  • 7
  • 71
  • 156
Max Power
  • 350
  • 4
  • 15
  • 1
    How are you writing out the XML from the `DOMDocument`? VB6 doesn't do UTF8 natively so may be subject to local codepage conversion. – Deanna May 15 '12 at 11:42
  • Also, do you know if the string in the XML is actually correct or not? This will tell if it's writing or reading incorrectly. – Deanna May 15 '12 at 11:43
  • 1
    MD5 is a hash, which will not allow you to decode it back. Use base64. – stracktracer May 15 '12 at 11:49
  • @Deanna - I'm appending child nodes on there and adding nodes. I then call the DOMDocument.Save() to save to a file. I can detail this a bit more if need. I do believe the string is correct that it is putting out into the file as that's what is shown in VB6 if I look at it, and also in .NET if I create the encrypted string via the same algorithm. – Max Power May 15 '12 at 12:08
  • @stracktracer - Someone suggested that by implementing a function in .NET to do this, then expose it in VB6, but do you know of a way to do it in VB6 as google's not coming up with much at the moment? Thanks. – Max Power May 15 '12 at 12:09
  • And how do you load it? Can you provide code samples? Anything else will be guesswork. How different is the read string from what you expect? – Deanna May 15 '12 at 12:34
  • 1
    Also note that binary data can't safely be stored in a string, especially with UTF-8 or codepage conversion in the middle. Is it actually a string (containing what character ranges)? – Deanna May 15 '12 at 12:37
  • @Deanna - I've concluded that VB6 allows writing of special characters to the XML File, whereas .NET doesn't allow this which doesn't help!! I'll edit the post above to show some more info rather than doing it here. – Max Power May 15 '12 at 14:22
  • Actually, found the answer! I'll post it up once I am permitted to do so (in 4 hours). Basically I converted it to Base64, saved it as Base64 in the XML as a type, and then found windows codepage number 1252 to convert it back (via another post). Thanks for you help all, much appreciated! – Max Power May 15 '12 at 14:33
  • So it was codepage conversion. You told .NET to load it as UTF-8. Base64 encoding the data is safest anyway. – Deanna May 15 '12 at 16:02

2 Answers2

2

The best thing for you to do is to encode your encrypted string in something that will use the ASCII charset. The easiest way to do this is to take your encrypted string and then encode it into Base64 and write this encoded value to the XML element.

And in .net, simply take the value of the XML element and decode it from Base64 and 'voila', you have your enrypted string.

.Net can easily decode a base64 string, see: http://msdn.microsoft.com/en-us/library/system.text.encoding.ascii.aspx. (This page may make it look a bit complicated than it really is).

VB6 does not have native support for Base64 encoding but a quick trawl on google throws up some examples on how it can be achieved quite easily:

http://www.vbforums.com/showthread.php?t=379072

http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp

http://www.mcmillan.org.nz/Programming/post/Base64-Class.aspx

http://www.freevbcode.com/ShowCode.asp?ID=2038

Kev
  • 1,832
  • 1
  • 19
  • 24
0

I've concluded that storing these characters in the XML file is wrong. VB6 allows this, but .NET doesn't! Therefore I have converted the string to a Base64 array in line with this link: -

http://www.nonhostile.com/howto-encode-decode-base64-vb6.asp

Now, on the .NET side, the file will de-serialise back into my class where I now store the password as a byte array. I then convert this back to the string I need to decrypt which now appears to raise another problem!

    string password = Encoding.UTF7.GetString(this.EncryptedPassword);

With this encoding conversion, I get the string almost exactly back to how I want, but there is a small greater than character that is just not translating properly! Then a colleague found a stack overflow post that had the final answer! There's a discrepancy between VB6 and .NET on this type of conversion. Doing the following instead did the trick: -

    string password = Encoding.GetEncoding(1252).GetString(this.EncryptedPassword);

Thanks for all the help, much appreciated. The original post about this is @ .Net unicode problem, vb6 legacy

Community
  • 1
  • 1
Max Power
  • 350
  • 4
  • 15