Base64-encoded value is slightly different at the end of string

Why is there slight differences in base64 encoding at the end of the encoded string. It could be one or more of characters that’s different.

Here an example done on base64encode.org using this text – Hey, Diddle Diddle – the output value is:

SGV5LCBEaWRkbGUgRGlkZGxl

But when I do same in base64 command line, I get this instead:

SGV5LCBEaWRkbGUgRGlkZGxlCg==

It is 4 more characters longer. Which one is correct encoding?

ANSWER

The most likely answer is caused by a non-visible character, or characters, being appended at the end of the text you are trying to encode that is causing the slight variation.

Normally this would be caused by the new line, or line feed character, that you won’t see but is there at the end of the text. It is a valid character that the base64 command will include when it encodes the text you give it.

It is not wrong, but most times it is not the intention to have the new line character encoded with it.

Thus, when doing so in command line make sure the new line character is trimmed off. It can be done this way,

:~$ echo -n "hello world" | base64

The result would be: aGVsbG8gd29ybGQ=

Without it, the output is: aGVsbG8gd29ybGQK

Of course, the case of the character/characters being encoded will also matter.

Leave a Reply

Your email address will not be published. Required fields are marked *