php base64 编码的字符串可以包含空格吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3092019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:40:33  来源:igfitidea点击:

Can a base64 encoded string contain whitespace?

phpmysqlencodingwhitespacebase64

提问by Dougal

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

base64 编码的字符串可能包含空格吗?具体来说,它是否可以在字符串末尾包含空格?

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

附注。我正在考虑整个“在 VARCHAR 字段中存储字符串时,MySQL 将修剪尾随空格”;-)

采纳答案by Stefan Gehrig

No it can't. See Base64for 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).

不,不能。见Base64编码通过使用允许的字符库base64,这是人物A-Za-z0-9+/(最后两个可能因实施不同),以及填充字符=(不过这也取决于实现一些实现不使用填充在全部)。

回答by Gavin Hymanson

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 + /

有效的 base64 字符串不应包含空格,因为编码字母表应仅包含 AZ az 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.

但是,如果编码的数据恰好包含一个“+”字符,并且该数据在 URL 中传递,则可能会无意中将其转换为空格。因此,在这些情况下,您可能会遇到一个假定的 base64 字符串,其中似乎有空格。

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

附注。我正在考虑整个“在 VARCHAR 字段中存储字符串时,MySQL 将修剪尾随空格”

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

顺便说一句,从 MySQL 5.0.3 开始,varchar 的尾随空格不会被随意删除

回答by ZZ Coder

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

是的。Base64 编码的字符串可以包含空格,但这些字符并不重要。所以如果数据库修剪空格就可以了。

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.

事实上,原始 MIME 规范建议将 Base64 字符串分成 72 个字符的行。XML 的 base64Binary 还可能包括换行符、制表符、空格。

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

在 PHP 中,base64_decode()删除所有空格字符,因此您不必担心。

回答by álvaro González

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

维基百科表明 Base64 编码有无数种变体:

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

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:

因此,答案可能取决于您需要对字符串做什么。但我敢说你是用 base64_encode() 在 PHP 中创建的,所以附加空格似乎是安全的:

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

?>

回答by Ivan Zlatanov

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.

据我所知不能。基本上,一个 Base64 字符串必须由一组 64 个字符构成。AZ, az, 0-9 make 62 - 另外两个取决于实现。

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.

根据我所知道的,现在有使用空格作为字符的实现。主要原因是可读性 - 即 Base64 字符串必须易于打印和识别。

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

您可能会在Wikipedia上找到有关它的更多信息。

回答by mcw

No, but- some implementations of the base64utility 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实用程序的某些实现会在输出中添加换行符,这会使它看起来好像空格是输出的一部分。如果您遇到这种情况,根据您的 版本base64,您可以通过执行以下操作之一来关闭此行为或去除换行符:

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

有关更多详细信息,请参阅此问题:https: //superuser.com/questions/1225134/why-does-the-base64-of-a-string-contain-n/1225334