简体   繁体   English

在php中进行base64编码/解码

[英]base64 encoding/decoding in php

I have implemented a cookie mechanism in my site. 我在我的网站上实现了cookie机制。 All cookies that i wanted to store are in a array with their TTL, value, name ... 我想要存储的所有cookie都在一个包含TTL,值,名称的数组中......

When i want to set the cookie, i serialize the tab, then i gzcompress and finally i base64_encode 当我想设置cookie时,我序列化选项卡,然后我gzcompress,最后我base64_encode

base64_encode(gzcompress(serialize($array)))

My problem is sometime, when i get the cookie with 我的问题是某个时候,当我得到cookie时

$_COOKIE[name]

the base64_encoded string is all in lowercase characters. base64_encoded字符串全部为小写字符。

So my question is : - is that possible that all characters are in lowercase format (base64_encoded) 所以我的问题是: - 可能所有字符都是小写格式(base64_encoded)

Or maybe i'am doing something wrong but the problem does not appear all the time and i can't reproduce the bug. 或者也许我做错了,但问题不会一直出现,我无法重现这个错误。

Yes, a Base64-encoded string can be all lowercase. 是的,Base64编码的字符串可以全部小写。 The probability of this happening (for random input data, which compressed data pretty well approximates) is about (36 / 64) 4 N / 3 , where N is the length of the input in bytes, and 4 N / 3 is thus the approximate length of the output (excluding padding). 发生这种情况的概率(对于随机输入数据,压缩数据非常接近)约为(36/64) 4 N / 3 ,其中N是输入的长度(以字节为单位),因此4 N / 3是近似值输出长度(不包括填充)。

As it happens, (36 / 64) 4 ≈ 0.100113 is very close to 1 / 10, and so, to a very good approximation, the probability that a random Base64 string contains no uppercase letters goes down by a factor of 10 for every four characters in length. 碰巧,(36/64)4≈0.100113非常接近1/10,因此,能够很好的近似,即随机Base64编码字符串不包含大写字母的概率为每四个下降由10倍字符的长度。

Thus, for example, a random 8-character Base64 string (corresponding to a random 6-byte input) has about a 1% chance of containing no uppercase letters, while the corresponding probability for a random 12-character Base64 string (corresponding to a random 9-byte input) is about 0.1%, and so on. 因此,例如,随机的8字符Base64字符串(对应于随机的6字节输入)有大约1%的可能性不包含大写字母,而随机12字符的Base64字符串的对应概率(对应于a随机9字节输入)约为0.1%,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM