I'm to encode a string.
First I used shell command: $ echo "teststring" | base64 dGVzdHN0cmluZwo=
But when I use a online tool, http://www.motobit.com/util/base64-decoder-encoder.asp the result is: dGVzdHN0cmluZw==
Why are they different?
The short answer is yes, unique binary/hex values will always encode to a unique base64 encoded string. BUT, multiple base64 encoded strings may represent a single binary/hex value. This is because hex bytes are not aligned with base64 'digits'.
The equals sign "=" represents a padding, usually seen at the end of a Base64 encoded sequence. The size in bytes is divisible by three (bits divisible by 24): All bits are encoded normally.
You can encode arbitrary bytes in base64 (which is why the encoding functions don't return errors). Only decoding can fail. The whole point of Base64 encoding is to take arbitrary bytes and reduce them to printable ASCII characters. There is no such thing as an invalid character for encoding, only for decoding.
echo
outputs the string and a newline at the end. If you want just the string you provided, use echo -n
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With