hs2d
hs2d

Reputation: 6199

RSACryptoServiceProvider.ToXmlString(); encoding

Im creating xml string from my RSA key:

var keyXmlString = rsaPrivKey.ToXmlString(true);

And now i would like to convert the string to bytes, something like this:

var xmlBytes = Encoding.Unicode.GetBytes(keyXmlString);

The question is wehat would be the right encoding to use there wehn converting xml string to bytes?

Upvotes: 0

Views: 544

Answers (2)

Eugen Rieck
Eugen Rieck

Reputation: 65324

There is no "right" encoding here - it depends, on who is your consumer. You need to use the encoding, your consumer expects.

I strongly suggest to do something like

var xmlBytes = Encoding.YOURENCODINGHERE.GetBytes("<?xml version=\"1.0\" encoding=\"YOURENCODINGHERE\" ?>\n"+keyXmlString);

to make this more foolproof.

Upvotes: 2

Sani Huttunen
Sani Huttunen

Reputation: 24395

As long as you're consistent with the encoding and decoding it really doesn't matter which encoding you use.

Upvotes: 0

Related Questions