I don't think that the accepted answer from Eugene Ryabtsev is correct.
If you try it with "\xff", you'll see that :
DecodeFrom64(EncodeTo64("\xff")) == "?" (i.e. "\x3f")
The reason is that ASCIIEncoding doesn't go further than the code 127. All characters from 128 to 255 won't be understood and will be converted to "?".
So, an extended encoding is necessary as follow :
static public string EncodeTo64(string toEncode) {
var e = Encoding.GetEncoding("iso-8859-1");
byte[] toEncodeAsBytes = e.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData) {
var e = Encoding.GetEncoding("iso-8859-1");
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = e.GetString(encodedDataAsBytes);
return returnValue;
}