Can a base64 encoded string contain whitespace?

Might a base64 encoded string contain whitespace? Specifically, could it contain whitespace at the end of the string?

PS. I'm thinking about the whole "MySQL will trim trailing whitespace when storing strings in VARCHAR fields" here ;-)

No it can't. See Base64 for the allowed character repository used by base64, which are the characters A-Z, a-z, 0-9, + and / (the last two may differ depending on the implementation) as well as the padding character = (but that's also implementation dependent as some implementations don't use padding at all).

Is a space possible in a base64 encoding?, Might a base64 encoded string contain whitespace? Specifically, could it contain whitespace at the end of the string? PS. I'm thinking about the� Note that a Base64 can contain whitespace (e.g. line breaks) without issue. { // If no exception is caught, then it is possibly a base64 encoded string byte

It shouldn't, but it might do.

A valid base64 string should not contain whitespace since the encoding alphabet should only consist of A-Z a-z 0-9 + /

However, if the encoded data happens to contain a '+' character, and the data is passed in a URL, it can be unintentionally converted into a space. So you may come across a supposed base64 string that appears to have spaces in it under these circumstances.

If this is the case, simply replace spaces with pluses before decoding.

PS. I'm thinking about the whole "MySQL will trim trailing whitespace when storing strings in VARCHAR fields" here

As an aside, the trailing whitespaces of a varchar won't be casually stripped as of MySQL 5.0.3

Can a base64 encoded string contain whitespace?, You misunderstand the premise of base64. Base64 is for encoding binary blobs into a printable ascii text, the way it does this is taking 6 bits of� [[ If the Base64 encoded %Stream was not generated using the code described above then I am also assuming the Base64 encoded BinaryText does not contain any additional white-space characters, or if it does contain white space then those white-space characters were removed before building a substring that contains a perfect multiple of 4 bytes.

Yes. Base64-encoded string can contain white-spaces but the characters are not significant. So it's ok if database trims spaces.

As a matter of fact, the original MIME specification recommends to break Base64 strings into lines of 72 characters. base64Binary of XML may also include newlines, tabs, spaces.

In PHP, base64_decode() strips all whiltespace characters so you don't have to worry about it.

about spaces in base64 encoding/decoding, Note that in case of textual data the encoding scheme does not contain their into chunks: The encoded data will be a continuous text without any whitespaces, into their percent-encoded form, which makes the string unnecessarily longer. Also, check our Base64 Encoder tool. The default btoa() function works well for binary data consisted of 8-bit bytes. However, the btoa() function accepts a string where each character represents an 8-bit byte. If a string contains characters that can't be represented in 8 bits(e.g. UTF16), it will break.

Wikipedia suggests that there're like a gazillion variations of the Base64 encoding:

So the answer probably depends on what you need to do with the string. But I'd dare say you created in PHP with base64_encode() so it appears to be safe to append blanks:


$original_data = 'Lorem ipsum dolor sit amet';
$encoded_data = base64_encode($original_data);
$padded_data = '    ' . chunk_split($encoded_data, 3, '  ') . '    ';

echo base64_decode($padded_data); // Prints 'Lorem ipsum dolor sit amet'


Base64 Encoding of "space", Character set: In case of textual data the encoding scheme does not contain their Prior decoding all non-encoded whitespaces are stripped from the input to� Now let's see how we can decode a Base64 string to its raw representation. Decoding Strings with Python. Decoding a Base64 string is essentially a reverse of the encoding process. We decode the Base64 string into bytes of unencoded data. We then convert the bytes-like object into a string. In a new file called, write the

As far as I know it cannot. Basically a Base64 string must be constructed from a set of 64 characters. A-Z, a-z, 0-9 make 62 - the other two depend on the implementation.

Based on what I know, there is now implementation that will use white space as a character. Main reason for that is readability - i.e. a Base64 string must be easily printed and recognized.

You'd probably find more info about it on Wikipedia.

Base64 Decoding of "Y2FyYQ==", Just load your string and it will automatically get all whitespace deleted. Quickly decode a base64-encoded string. Replace a set of strings with a new set. Read the source for commons-codec-1.4 Base64.isArrayByteBase64() it only checks that each character in the string is valid to be considered for Base64 encoding and allows white space. – Brad Apr 27 '17 at 11:30

Remove All Whitespace from a String, This module provides functions for encoding binary data to printable ASCII The RFC 3548 encodings are suitable for encoding binary data so that it can Encode byte string s using the standard Base64 alphabet. This should only contain whitespace characters, and by default contains all whitespace characters in� Thus you can safely use base64 on the Web without too much fear. In some instances, base64 encoding might even improve performance, because it avoids the need for distinct server requests. In other instances, base64 can make things worse, since it tends to defeat browser and server caching.

19.6. base64 — Base16, Base32, Base64, Base85 Data Encodings , -- Base64 encoding and decoding Prolog-based base64 encoding using DCG rules. base64. Base64URL encoded strings do not contain white space. Base64URL encoded values can safely be used as URLs and file names. This tool converts a plain string to base64 encoding. Base64 encoding uses only letters a-z, A-Z, 0-9 and plus, slash and equals sign characters. This encoding scheme takes every 6 bits of the input string and assigns one of 64 letters to it. It's easy to calculate that base64 produces a larger output than the original string. -- Base64 encoding and decoding, In computer science, Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string padding characters might be added to make the last encoded block contain four Base64 characters. MIME does not specify a fixed length for Base64-encoded lines, but it does specify a maximum line� In this snippet, we’re going to demonstrate how you can display Base64 images in HTML. Use the HTML element to embed Base64 encoded image into HTML.

  • P'raps I mean "does" more than "can" ;-)
  • well you can add whitespace depending on the implementation but mostly they just will be stripped as they are actually valid because they are not part of the b64 "alphabet" but often included for display purposes, "readbility" doesnt actually hit it because a "normal" Human cant read b64 strings in the first place
  • yes it can, they don't belong to the code, but in most implementations they can added to maintain readability. Most decoders ignore whitespaces.
  • Yes that's true but then they are not significant to the encoded string which means that they can safely be ignored and it doesn't matter if MySQL or whoever will strip them away. The OP's intention is obviously to store base64-encoded strings in a MySQL VARCHAR column which is absolutely save.
  • THanks guys, I meant more "does" a base64 string contain spaces - not "can" - my fault. So in conclusion it CAN but they are irrelevant. :-) I get it now
  • For string "apfjxkic-omyuobwd339805ak:60a06cd2ddfad610b9490d359d605407" base 64 would output a "\n", I don't know why
  • As Gavin Jackson mentions below, if you have an unexpected space in your base64 string, then it it most likely the result of URL parsing converting this from a + to a white space. A simple $str = str_replace(' ', '+', $str) before you base64_decode($str) will resolve it.
  • This should not be a problem however if you use a URL safe Base64 encoding algorithm :)
  • +1 for mentioning the '+'/space problem. Just had a URL supplied Base64 string with the +'s converted to spaces.
  • I was going crazy with this one. Thank you!
  • I had a space in base64 string (as a result of json encode/decode) which this post resolved for me. This should be mentioned in the accepted answer so I'm going to suggest an edit to include this.
  • A roundabout way of proving it I suppose! Thanks