Base64 decoding has "%" at the end of the result sometimes. Is it the result supposed to be? Any solution to that?

StackOverflow https://stackoverflow.com/questions/23176903

  •  06-07-2023
  •  | 
  •  

Question

I am just studying base64 encoding and decoding algorithms and try some programs. I found some example code online, but the result looks a little weird for me. Here is the link: http://knol2share.blogspot.com/2011/07/base64-encoding-and-decoding-in-c.html

I tried to use it to encode and decode a string.
Enter a string: 02613
Base64 Encoded value: MDI2MTM=
Base64 Decoded value: 02613% -- I do not know why there is a "%", is there a way to get the correct result

I even tried the Base64 program in linux and got the same result after removing the newline in encoding.

Here is the result: %echo -n 02613 |base64
MDI2MTM=
%echo -n MDI2MTM= | base64 --decode
02613%

Does anyone know how I can get the exact same result with the input string? Thanks.

Was it helpful?

Solution 2

Isn't the % sign a command prompt ? Add new line after decoded b64 and check.

OTHER TIPS

It is printed if the decoded text does not end with a newline.

$ printf "foobar\n" | base64 | base64 --decode
foobar
$ printf "foobar" | base64 | base64 --decode
foobar%

Here's the example with echo command, -n option will remove newline character, that's why in first case we don't have the symbol % and in the second case with have one appended.

➜  ~ echo "HELLO" | base64 | base64 --decode
HELLO
➜  ~ echo -n "HELLO" | base64 | base64 --decode
HELLO%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top