Is a space possible in a base64 encoding?

蓝咒 提交于 2020-02-01 10:31:25

问题


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


回答1:


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.




回答2:


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?




回答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




回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/5110731/is-a-space-possible-in-a-base64-encoding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!