13

I did this:

 byte[] data = Convert.FromBase64String(str);
 string decodedString = Encoding.UTF8.GetString(data);
 Console.WriteLine(decodedString);

but got Unhandled Exception: System.FormatException: Invalid length for a Base-64 char array or string.

In javascript using atob(str) gives me the right decoded str.

javascript console:

atob("eyJpc3MiOiJodHRwczovL2lkZW50aXR5LXN0YWdpbmcuYXNjZW5kLnh5eiIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHktc3RhZ2luZy5hc2NlbmQueHl6L3Jlc291cmNlcyIsImNsaWVudF9pZCI6IjY5OTRBNEE4LTBFNjUtNEZFRC1BODJCLUM2ODRBMEREMTc1OCIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJzdWIucmVhZCIsImRhdGEud3JpdGUiLCJkYXRhLnJlYWQiLCJhbGcuZXhlY3V0ZSJdLCJzdWIiOiIzNzdjMDk1Yi03ODNiLTQ3ZTctOTdiMS01YWVkOThjMDM4ZmMiLCJhbXIiOiJleHRlcm5hbCIsImF1dGhfdGltZSI6MTQwNzYxNTUwNywiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvMDg0MGM3NjAtNmY3Yi00NTU2LWIzMzctOGMwOTBlMmQ0NThkLyIsIm5hbWUiOiJwa3NAYXNjZW5kLnh5eiIsImV4cCI6MTQwNzgzNjcxMSwibmJmIjoxNDA3ODMzMTExfQ")
"{"iss":"https://identity-staging.ascend.xyz","aud":"https://identity-staging.ascend.xyz/resources","client_id":"6994A4A8-0E65-4FED-A82B-C684A0DD1758","scope":["openid","profile","sub.read","data.write","data.read","alg.execute"],"sub":"377c095b-783b-47e7-97b1-5aed98c038fc","amr":"external","auth_time":1407615507,"idp":"https://sts.windows.net/0840c760-6f7b-4556-b337-8c090e2d458d/","name":"[email protected]","exp":1407836711,"nbf":1407833111}"
5
  • what is parts? a string array? Commented Aug 12, 2014 at 9:12
  • I will update the question to leave that out. yes its a array. the parts[1] is the same string i tested with in javascript that works Commented Aug 12, 2014 at 9:13
  • how is the string defined, are you sure the string is properly escaped? Commented Aug 12, 2014 at 9:16
  • I just copy pasted the same string that i tested in javascript into c# code. str = "my_jwt_token"; Will update the question again to show code Commented Aug 12, 2014 at 9:22
  • Reference: developer.mozilla.org/en-US/docs/Web/API/Window/atob Commented Sep 5 at 16:47

2 Answers 2

16
    var str = "eyJpc3MiOiJodHRwczovL2lkZW50aXR5LXN0YWdpbmcuYXNjZW5kLnh5eiIsImF1ZCI6Imh0dHBzOi8vaWRlbnRpdHktc3RhZ2luZy5hc2NlbmQueHl6L3Jlc291cmNlcyIsImNsaWVudF9pZCI6IjY5OTRBNEE4LTBFNjUtNEZFRC1BODJCLUM2ODRBMEREMTc1OCIsInNjb3BlIjpbIm9wZW5pZCIsInByb2ZpbGUiLCJzdWIucmVhZCIsImRhdGEud3JpdGUiLCJkYXRhLnJlYWQiLCJhbGcuZXhlY3V0ZSJdLCJzdWIiOiIzNzdjMDk1Yi03ODNiLTQ3ZTctOTdiMS01YWVkOThjMDM4ZmMiLCJhbXIiOiJleHRlcm5hbCIsImF1dGhfdGltZSI6MTQwNzYxNTUwNywiaWRwIjoiaHR0cHM6Ly9zdHMud2luZG93cy5uZXQvMDg0MGM3NjAtNmY3Yi00NTU2LWIzMzctOGMwOTBlMmQ0NThkLyIsIm5hbWUiOiJwa3NAYXNjZW5kLnh5eiIsImV4cCI6MTQwNzgzNjcxMSwibmJmIjoxNDA3ODMzMTExfQ";
    int mod4 = str.Length % 4;
    if (mod4 > 0)
    {
        str += new string('=', 4 - mod4);
    }

solved it in c#

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

3 Comments

would I need to use this on both encoding and decoding sides? or just decoding?
just for decoding, its due to that base64 string can omit the == at the end, but c# expect the length to be divisible by 4 (hence the often == at the end).
This fixes a broken encoded input string like fC4wMDI0MDIyNYMyNTczRIrXALxhHLMzMx and convert it to a valid encoded fC4wMDI0MDIyNYMyNTczRIrXALxhHLMzMx==. Now I am able to use System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(encoedString))
12

Use javascript's window.btoa function to encode the string in Base 64 Format in Javascript Frontend UI. To decode back same string in C# (the equivalent of javascript's window.atob function) please see the following code.

Sample usage of javascripts btoa method as shown as below
var encodedBase64String = window.btoa("base64 encoded string");

(Most probably you are trying to post back data that might contain special characters or line breaks [e.g input from a rich text editor etc] back to the controller or you might creating custom basic basic authentication filter on server side)

C# Code Below

string base64Encoded = "YmFzZTY0IGVuY29kZWQgc3RyaW5n";
string base64Decoded;
byte[] data = System.Convert.FromBase64String(base64Encoded);
base64Decoded = System.Text.UTF8Encoding.Default.GetString(data);
Console.WriteLine(base64Decoded)

You can see its working sample at https://dotnetfiddle.net/abxwSw

1 Comment

Thank you ! its working like a charm...Thanks

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.