ryanzec
ryanzec

Reputation: 28050

Is a space possible in a base64 encoding?

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

Upvotes: 24

Views: 36869

Answers (6)

David Harvey
David Harvey

Reputation: 87

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==

Upvotes: 0

rsc
rsc

Reputation: 10679

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.

Upvotes: 8

hpavc
hpavc

Reputation: 1335

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

Upvotes: 2

Bobby Whitman
Bobby Whitman

Reputation: 41

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

Upvotes: 3

xanatos
xanatos

Reputation: 111930

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?

Upvotes: 11

Cyberax
Cyberax

Reputation: 1737

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.

Upvotes: 30

Related Questions