0

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!

4
  • Try this : string input = "amFuZUBkb2UuY29tOkpATjNEb2UxCg=="; string output = Encoding.UTF8.GetString(Convert.FromBase64String(input)); Should be From not To. Commented Jun 10, 2017 at 18:14
  • No, you're not understanding well. I'm trying to find the right way in order to encode [email protected]:J@N3Doe1 to amFuZUBkb2UuY29tOkpATjNEb2UxCg== @jdweng Thanks! Commented Jun 10, 2017 at 18:20
  • @TheNoob: Post your answer back! Commented Jun 10, 2017 at 18:25
  • Use reverse the process to get the 64 string. Commented Jun 10, 2017 at 19:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That new line char.
I caught that one by noticing the output from the decoding contained a newline, so I checked the output in the debugger :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.