php 为什么 base64_encode() 在结果中添加斜杠“/”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11449577/
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
Why is base64_encode() adding a slash "/" in the result?
提问by BadHorsie
I am encoding the URL suffix of my application:
我正在对我的应用程序的 URL 后缀进行编码:
$url = 'subjects?_d=1';
echo base64_encode($url);
// Outputs
c3ViamVjdHM/X2Q9MQ==
Notice the slash before 'X2'.
注意“X2”之前的斜线。
Why is this happening? I thought base64 only outputted A-Z, 0-9 and '=' as padding? I have tried using an online base64 encoder to check, and it seems base64 always does this. I can't tell if it's the underscore "_" or the question mark "?" or the "=" perhaps?
为什么会这样?我以为 base64 只输出 AZ、0-9 和 '=' 作为填充?我曾尝试使用在线 base64 编码器进行检查,似乎 base64 总是这样做。我不知道是下划线“_”还是问号“?” 或“=”也许?
回答by Artefact2
No. The Base64 alphabet includes A-Z, a-z, 0-9 and +and /.
不可以。Base64 字母表包括 AZ、az、0-9+和/。
You can replace them if you don't care about portability towards other applications.
如果您不关心向其他应用程序的可移植性,则可以替换它们。
See: http://en.wikipedia.org/wiki/Base64#Variants_summary_table
请参阅:http: //en.wikipedia.org/wiki/Base64#Variants_summary_table
You can use something like these to use your own symbols instead (replace -and _by anything you want, as long as it is not in the base64 base alphabet, of course!).
您可以使用类似这些使用自己的符号来代替(替换-,并_通过任何你想要的,只要它不是以base64基地字母,当然!)。
The following example converts the normal base64 to base64url as specified in RFC 4648:
以下示例将正常的 base64 转换为 RFC 4648 中指定的base64url :
function base64url_encode($s) {
return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}
function base64url_decode($s) {
return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}
回答by Snorbuckle
In addition to all of the answers above, pointing out that /is part of the expected base64alphabet, it should be noted that the particular reason you saw a /in your encoded string, is because when base64encoding ASCIItext, the only way to generate a /is to have a question mark in a position divisible by three.
除了上面的所有答案,指出它/是预期base64字母表的一部分,应该注意的是,您/在编码字符串中看到 a 的特殊原因是因为在base64编码ASCII文本时,生成 a 的唯一方法/是一个可以被三整除的位置上的问号。
回答by deceze
Sorry, you thought wrong. A-Za-z0-9 only gets you 62 characters. Base64 uses two additional characters, in PHP's case /and +.
对不起,你想错了。A-Za-z0-9 只能得到 62 个字符。Base64 使用了两个额外的字符,在 PHP 的情况下/和+.
回答by Igor Chubin
There is nothing special in that.
这没有什么特别之处。
The base 64 "alphabet" or "digits" are A-Z,a-z,0-9 plus two extra characters + (plus) and / (slash).
基数为 64 的“字母表”或“数字”是 AZ,az,0-9 加上两个额外的字符 +(加号)和 /(斜线)。
You can later encode / with %2f if you want.
如果需要,您可以稍后使用 %2f 对 / 进行编码。
回答by poussma
For base64 the valid charset is: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
对于 base64,有效字符集为:ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
the = is used as filler for the last bytes
= 用作最后字节的填充符
M.
M。
回答by Quentin
A-Z is 26 characters. 0-9 is 10 characters. = is one character. That gives a total of 37 characters, which is some way short of 64.
AZ 是 26 个字符。0-9 是 10 个字符。= 是一个字符。总共有 37 个字符,比 64 个字符少了一些。
/is one of the 64 characters. You can see a complete list on the wikipedia page.
/是 64 个字符之一。您可以在维基百科页面上查看完整列表。
回答by tfont
Not directly related, and enough people above have answered and explained solutions quite well.
没有直接关系,上面有足够多的人已经很好地回答和解释了解决方案。
However, going a bit outside of the scope of things. If you want readable base text, try looking into Base58. It's worth considering if you want only alphanumeric characters.
然而,有点超出了事情的范围。如果您想要可读的基本文本,请尝试查看Base58。如果您只想要字母数字字符,则值得考虑。
回答by Tayyab Hayat
Hope it may help you
希望它可以帮助你
public function generate_key($s) {
return str_replace(array('+', '/'), array('-', '_'), base64_encode($s));
}
$randomKey = generate_key(openssl_random_pseudo_bytes(12)); //you can change this number for different length of keys in base64 format
you can decode the above key by this function below. Just pass your encrypted string here
你可以通过下面的这个函数来解码上面的密钥。只需在此处传递您的加密字符串
function key_decode($s) {
return base64_decode(str_replace(array('-', '_'), array('+', '/'), $s));
}
there is no need to decode openssl_random_pseudo_bytes() , but you can use this for other cases like decode url...
无需解码 openssl_random_pseudo_bytes() ,但您可以将其用于其他情况,例如解码 url ...

