1

So for example I have a base64 encoded string and want to decode it. The length of the string is 29 characters so obviously to decode it I have to use 32 characters.

Convert.FromBase64() only allows up to 2 padding charcaters. Is there simply no case where I'd need 3 padding characters because of base64 encoding?

2 Answers 2

3

Please refer to base64 definition. It allows up to 2 padding characters by desing. See description here: https://en.wikipedia.org/wiki/Base64#Padding

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

Comments

3

The length of the string is 29 characters

It is impossible for a byte[] to be encoded in base64 and produce a string of 29 chars, regardless of padding. One base64 char produces 6 bits. A byte[21] has 21 x 8 = 168 bits => 168 / 6 = 28.0 base64 chars, perfect fit. A byte[22] has 22 x 8 = 176 bits => 176 / 6 = 29.33 chars, rounds up to 30 chars. No possible byte[] can produce 29 chars.

So Convert.FromBase64() correctly tells you that the data is garbage.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.