21

Is it possible for a string generated from a base64 encoding to have a space (' ') in it?

ryanzec
  • 27,284
  • 38
  • 112
  • 169
  • possible duplicate of [Can a base64 encoded string contain whitespace?](http://stackoverflow.com/questions/3092019/can-a-base64-encoded-string-contain-whitespace) – dan04 Feb 25 '11 at 15:28

6 Answers6

26

No. Next question?

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

Actually, spaces and CRLFs are usually silently skipped during the decoding, as they might appear as a result of splitting long strings.

Cyberax
  • 1,667
  • 16
  • 18
  • and if they aren't silently being skipped? can we force them to do so? I am having a problem with this described here: https://stackoverflow.com/questions/54164988/illegal-base64-character – Kleber Mota Jan 13 '19 at 12:29
  • @KleberMota any whitespace is supposed to be ignored by the decoder. If your decoder fails to do this, you could remove all whitespace from the input prior to decoding it. – still_dreaming_1 Dec 13 '21 at 15:19
8

By reading the http://en.wikipedia.org/wiki/Base64 wiki it seems that in Base64 transfer encoding for MIME (RFC 2045) spaces are allowed and discarded. In all other variants they are forbidden. Ah... and this question is a duplicate: Can a base64 encoded string contain whitespace?

Community
  • 1
  • 1
xanatos
  • 109,618
  • 12
  • 197
  • 280
6

I was receiving a base64 string with a space, but my decoder was just ignoring the space. Unfortunately ignoring the space was not providing the expected value.

The space (" ") had to be replaced with a "+" in order for my decoder output the correct value.

rsc
  • 10,348
  • 5
  • 39
  • 36
3

Base64 encoding output will never include a space. FooBabel has a nice (free) online encoding tool based on Apache Codec, where you can play with options like linebreaks and line terminators - foobabel base64 codec

2

I came across this question when debugging vbscript code.

Oddly MSFT encodes like this, rather than encoding with a + it will use a ' '. The MIME can be fixed with s/ /+/g and it will work with /usr/bin/base64.

Note that this is a well advertised pattern for encoding a file in vbscript, and if followed in reverse it MSFT will deal with the spaces and put the same file back. (it just won't be interoperable elsewhere)

Function b64(fqfn)
   Dim inputStream: Set inputStream = CreateObject("ADODB.Stream")
   inputStream.Open
   inputStream.Type = 1
   inputStream.LoadFromFile(fqfn)

   Dim bytes: bytes = inputStream.Read

   Dim dom: Set dom = CreateObject("Microsoft.XMLDOM")
   Dim elem: Set elem = dom.createElement("tmp")
   elem.dataType = "bin.base64"
   elem.nodeTypedValue = bytes
   b64 = elem.text
End Function
hpavc
  • 1,336
  • 7
  • 7
1

On Windows using bash, the encoding is adding a line wrap! Need to suppress that. $ echo -n "$OAUTH_CLIENT_SECRET" | base64 V3E5SDFvSHVvdm5Nck5aUHBJQ3NValg5WGlKdXBua25MYWJuLUxiNXozclhTRWNLdDFDY09wSDhi d2hNS3JMdA==

$ echo -n "$OAUTH_CLIENT_SECRET" | base64 --wrap 0 V3E5SDFvSHVvdm5Nck5aUHBJQ3NValg5WGlKdXBua25MYWJuLUxiNXozclhTRWNLdDFDY09wSDhid2hNS3JMdA==

David Harvey
  • 71
  • 1
  • 4