Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 693
user1726119 Avatar asked Apr 19 '14 23:04

user1726119


2 Answers

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%
like image 157
Johannes Mittendorfer Avatar answered Oct 25 '22 02:10

Johannes Mittendorfer


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

like image 45
mleko Avatar answered Oct 25 '22 01:10

mleko