Encryption in XML file

M

Mike

I am encrypting string values and storing them in an XML file using the
Crypto class in the MSDN Signature sample and the DataSet class to
read/write XML.

Which encoding method should I use (Unicode, UTF8?) to convert strings to
byte arrays? The Crypto class uses byte arrays for encrypting and
decrypting.
 
J

Jon Skeet [C# MVP]

Mike said:
I am encrypting string values and storing them in an XML file using the
Crypto class in the MSDN Signature sample and the DataSet class to
read/write XML.

Which encoding method should I use (Unicode, UTF8?) to convert strings to
byte arrays? The Crypto class uses byte arrays for encrypting and
decrypting.

Use Convert.ToBase64String and Convert.FromBase64String.
 
M

Mike

convert.FromBase64String("9/8/2004 10:18AM")

Run-time exception thrown : System.FormatException - FormatException

The documentation for FromBase64String seems to say that it doesn't support
the : character. How should I convert a time string to a byte array?
 
J

Jon Skeet [C# MVP]

Mike said:
convert.FromBase64String("9/8/2004 10:18AM")

Run-time exception thrown : System.FormatException - FormatException

The documentation for FromBase64String seems to say that it doesn't support
the : character. How should I convert a time string to a byte array?

My mistake - I thought you were asking about how to store the binary
data once it had been encrypted.

I'd suggest using UTF-8 to convert from a string to a byte array to
start with. So, you'll end up with:

Original data -> UTF-8 encoded string -> encrypted bytes->Base64 string

That's what you put in your XML file. Then you go the other way:

Base64 string->encrypted bytes -> UTF-8 encoded string -> original data
 
M

Mike

Thanks. I am using Unicode instead of UTF-8 and it seems to be working. Do
you think there will be a problem if I leave it as Unicode?
 
J

Jon Skeet [C# MVP]

Mike said:
Thanks. I am using Unicode instead of UTF-8 and it seems to be
working. Do you think there will be a problem if I leave it as
Unicode?

Well, it'll usually be twice as big as it would be with UTF-8 (the
"usually" meaning "if all the text data is just ASCII", but it'll still
work.
 
M

Mike

Cool! Thanks Jon - that helped a lot!!!

Jon Skeet said:
Well, it'll usually be twice as big as it would be with UTF-8 (the
"usually" meaning "if all the text data is just ASCII", but it'll still
work.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top