34

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 ;-)

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Dougal
  • 1,047
  • 2
  • 13
  • 18
  • P'raps I mean "does" more than "can" ;-) – Dougal Jun 22 '10 at 16:07
  • 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 – My1 Feb 23 '16 at 10:17

6 Answers6

33

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).

Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • 9
    yes it can, they don't belong to the code, but in most implementations they can added to maintain readability. Most decoders ignore whitespaces. – jigfox Jun 22 '10 at 10:18
  • 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. – Stefan Gehrig Jun 22 '10 at 10:51
  • 3
    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 – Dougal Jun 22 '10 at 16:09
  • For string "apfjxkic-omyuobwd339805ak:60a06cd2ddfad610b9490d359d605407" base 64 would output a "\n", I don't know why – Tiina Jul 03 '17 at 04:59
  • 4
    As [Gavin Jackson](https://stackoverflow.com/users/1579602/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. – Egg Jul 27 '17 at 12:24
  • Quite a misleading answer... The question was "can" and the correct answer is "yes, it can". This is a quote from the document you refer to: "newlines and white spaces may be present anywhere but are to be ignored on decoding" – Igor Mikushkin Nov 27 '19 at 12:35
20

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

Gavin Jackson
  • 1,907
  • 1
  • 22
  • 28
  • This should not be a problem however if you use a URL safe Base64 encoding algorithm :) – Ren Oct 31 '12 at 15:24
  • 3
    +1 for mentioning the '+'/space problem. Just had a URL supplied Base64 string with the +'s converted to spaces. – iCollect.it Ltd Jan 14 '14 at 16:12
  • 1
    I was going crazy with this one. Thank you! – Everton Lenger Mar 02 '17 at 18:55
  • 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. – Egg Jul 26 '17 at 11:51
  • This is my case. The API from the client sometime toss in a space and it's driving me nuts to convert those base64. – IcyHerrscher Jun 21 '23 at 08:22
4

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.

ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
1

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

http://en.wikipedia.org/wiki/Base64

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:

<?php

$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'

?>
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

No, but - some implementations of the base64 utility will add linebreaks to the output, which can make it appear as though whitespace is part of the output. If you're running into this case, depending on your version of base64, you can either turn off this behavior, or strip newline characters, by doing one of the following:

base64 -w 0 < input.txt
base64 < input | tr -d \\n

See this question for more detail: https://superuser.com/questions/1225134/why-does-the-base64-of-a-string-contain-n/1225334

mcw
  • 3,500
  • 1
  • 31
  • 33
0

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.

Ivan Zlatanov
  • 5,146
  • 3
  • 29
  • 45