Base64 Decode

Every four characters of Base64-encoded text represent three octets of unencoded text or data because Base64 is a six-bit encoding and the decoded values are split into eight-bit octets on current computers (4 sextets = 4 6 = 24 bits). This indicates that padding must be applied to the encoded output to make it a multiple of four when the length of the unencoded input is not a multiple of three. The padding character = denotes that all bits required to fully encode the input have already been used. (This contrasts with A, where the last few bits are entirely zeros.) The example below shows how altering the input of the aforementioned quote affects the result.

Base64 encoding without padding

Without padding, fewer than four encoded characters can be left after the repetitive usual decoding of four characters to three bytes. There can only be two or three characters left in this scenario. A minimum of two Base64 characters are needed because a single Base64 character only contains 6 bits and a byte requires 8 bits, making it impossible to have just one remaining encoded character: Six bits are contributed by the first character, and the first two bits of the second character. For instance:

Until there are no more than four octets left, the procedure is repeated on the remaining data. Three remaining octets are processed regularly at this point. In order to create an integral multiple of six bits, the input data is right-padded with zero bits if there are fewer than three octets (24 bits) left to encode.

After encoding the non-padded data, two = characters are added to the output if two octets of the 24-bit buffer are filled with padded-zeros; one = character is added if one octet is filled with padded-zeros. This informs the decoder that the padding-induced zero bits should not be included in the data that is being rebuilt. Additionally, this ensures that the output length of the encoded data is a multiple of 4 bytes.

PEM mandates that every line of encoding

We care about your data and would love to use cookies to improve your experience.