Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it ok to remove the equal signs from a base64 string?

Tags:

python

base64

md5

I have a string that I'm encoding into base64 to conserve space. Is it a big deal if I remove the equal sign at the end? Would this significantly decrease entropy? What can I do to ensure the length of the resulting string is fixed?

>>> base64.b64encode(combined.digest(), altchars="AB") 'PeFC3irNFx8fuzwjAzAfEAup9cz6xujsf2gAIH2GdUM=' 

Thanks.

like image 828
ensnare Avatar asked Jan 26 '12 15:01

ensnare


People also ask

Does Base64 always end with equals?

Q Does a base64 string always end with = ? Q Why does an = get appended at the end? A: As a short answer: The last character ( = sign) is added only as a complement (padding) in the final process of encoding a message with a special number of characters.

Why is there == At the end of Base64?

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.

What is the == in Base64?

When decoding Base64 text, four characters are typically converted back to three bytes. The only exceptions are when padding characters exist. A single = indicates that the four characters will decode to only two bytes, while == indicates that the four characters will decode to only a single byte. For example: Encoded.

What characters are not allowed in Base64?

The base 64 digits in ascending order from zero are the uppercase characters 'A' to 'Z', lowercase characters 'a' to 'z', numerals '0' to '9', and the symbols '+' and '/'. % is not allowed in base64 encoding.


1 Answers

Every 3 bytes you need to encode as Base64 are converted to 4 ASCII characters and the '=' character is used to pad the result so that there are always a multiple of 4 encoded characters. If you have an exact multiple of 3 bytes then you will get no equal sign. One spare byte means you get two '=' characters at the end. Two spare bytes means you get one '=' character at the end. depending on how you decode the string it may or may not see this as a valid string. With the example string you have, it doesn't decode, but some simple strings I've tried do decode.

You can read this page for a better understanding of base64 strings and encoding/decoding.

http://www.nczonline.net/blog/2009/12/08/computer-science-in-javascript-base64-encoding/

There are free online encoder/decoders that you can use to check your output string

like image 112
Brian Avatar answered Sep 23 '22 19:09

Brian