zach
zach

Reputation: 65

c# decode base64 - Invalid length for a Base-64 char array or string

I have problem decode this base64 string, it always return ' Invalid length for a Base-64 char array or string'. But I managed to decode online at 'https://www.base64decode.org/'

These are my codes:

string base64Encoded = "eyJ4NXQjUzI1NiI6IndVWUdZWXp5dTJPMzNtVjY1WHFBVzBiTFlMeU5TU2VKZFlGTldKNnpzY0kiLCJ4NXQiOiJ0V3lmNTA0aTM3TXZxN0t1eEVyQTY4VTE4c1kiLCJraWQiOiJTSUdOSU5HX0tFWSIsImFsZyI6IlJTMjU2In0";

string base64Decoded;

byte[] data = Convert.FromBase64String(base64Encoded);

base64Decoded = Encoding.UTF8.GetString(data);

Upvotes: 0

Views: 2146

Answers (1)

Lucca Ferri
Lucca Ferri

Reputation: 1347

I've made some tests, and according to the comments you should not need the padding, but if you try to encode the decoded string: {"x5t#S256":"wUYGYYzyu2O33mV65XqAW0bLYLyNSSeJdYFNWJ6zscI","x5t":"tWyf504i37Mvq7KuxErA68U18sY","kid":"SIGNING_KEY","alg":"RS256"}

it gives you: eyJ4NXQjUzI1NiI6IndVWUdZWXp5dTJPMzNtVjY1WHFBVzBiTFlMeU5TU2VKZFlGTldKNnpzY0kiLCJ4NXQiOiJ0V3lmNTA0aTM3TXZxN0t1eEVyQTY4VTE4c1kiLCJraWQiOiJTSUdOSU5HX0tFWSIsImFsZyI6IlJTMjU2In0=

Notice the padding at the end.

Upvotes: 3

Related Questions