Amen Jlili
Amen Jlili

Reputation: 1944

Cannot find the right encoding parameters (string to base64)

The following string amFuZUBkb2UuY29tOkpATjNEb2UxCg== decodes to [email protected]:J@N3Doe1 (via online decoder).

But when I try to encode the string back ( tobase64) I get a different result. I've tried UT8, ASCII, Unicode. All give different results. I'm using C#.

   string str= u + ":" + v;
   byte[] rt = System.Text.ASCIIEncoding.ASCII.GetBytes(str.Trim());
   string t= Convert.ToBase64String(rt);

Thanks!

Upvotes: 0

Views: 45

Answers (1)

user2509848
user2509848

Reputation:

Starting from your base64 string, this gives us the correct answer of "[email protected]:J@N3Doe1\n" (the online encoder didn't clean up your end-of-line):

var test = "amFuZUBkb2UuY29tOkpATjNEb2UxCg==";
var output = Convert.FromBase64String(test);
foreach (var b in output){
    Console.Write((char)b);
}
Console.WriteLine();

Going back, this gives the string "amFuZUBkb2UuY29tOkpATjNEb2UxCg==":

var input = "[email protected]:J@N3Doe1\n";
var bytes = input.Select(c => (byte)c).ToArray();

var output = Convert.ToBase64String(bytes);
Console.WriteLine(output);

If you remove the end-of-line, you get the same string without the "Cg==" at the end.

Upvotes: 1

Related Questions