I have the below openssl code to decode json content, it works fine if the encoded data is "eyJ0ZXN0MSI6eyJ2YWwyIjotOTEuNiwidmFsMyI6NDAuMTIzNH19" but it does not work if the encoded data is "eyJ0ZXN0MSI6eyJsYXRpdHVkZSI6LTkxLjYsInZhbDMiOjQwfX0". Its not decoding the last 2 right curly braces. Can somebody help me to understand why this is happening?
Note: the encoded string is the payload generated from jwt.io
I don't have control over the encoded data, i get it from other party. Looks like its a valid encoded data because all online websites are decoding it properly
Those sites are obviously padding the string with '=' to make it a multiple of 4, since that's what base64 needs for proper decoding. You should do the same.
padding the string with '=' to make it a multiple of 4
Just to clarify something. Base64 uses 3 bytes to encode 4 characters. So the encoded string (the input to your function) should have a length that is a multiple of 3. You should pad with '=' to make the string length a multiple of 3.
If no padding were required, the decoded string will have a length that is a multiple of 4. The decoded string will not be a multiple of 4 is padding is required.