Occasional openssl_decrypt error : "failed to base64 decode the input"
Occasional openssl_decrypt error : "failed to base64 decode the input"
我有2个PHP页,其中第1页加密了一个字符串onLoad
,加密后的字符串将通过URL传递给第2页。
偶尔(大约 8 个中有 1 个)第 2 页无法解密字符串,并给我这个错误消息:failed to base64 decode the input ...
这是我正在使用的:
- 加密方式:
AES-128-CBC
- iv :
openssl_random_pseudo_bytes(16);
这里有一些额外的信息:
- PHP 5.4.16
- nginx 1.8.0
- centos 7
有什么建议吗?
要在 URL 中包含字符串,您需要在 URL 中包含字符串时 URL encode it. For base 64, you need to replace =
with %3D
, but don't do that. Instead, use a proper URL encoding function。
感谢@Simba 和@David Schwartz 的评论,这是应该做的事情:
第 1 页:
- encrypt the string via openssl_encrypt();
- encode the encrypted string for sending via url by rawurlencode();
- concatenate those "encrypted+encoded" strings and send via url;
第 2 页:
- take the var out from $_GET;
- decode the var via rawurldecode();
- decrypt the decoded var via openssl_decrypt();
现在可以正常工作了~再次感谢~
我有2个PHP页,其中第1页加密了一个字符串onLoad
,加密后的字符串将通过URL传递给第2页。
偶尔(大约 8 个中有 1 个)第 2 页无法解密字符串,并给我这个错误消息:failed to base64 decode the input ...
这是我正在使用的:
- 加密方式:
AES-128-CBC
- iv :
openssl_random_pseudo_bytes(16);
这里有一些额外的信息:
- PHP 5.4.16
- nginx 1.8.0
- centos 7
有什么建议吗?
要在 URL 中包含字符串,您需要在 URL 中包含字符串时 URL encode it. For base 64, you need to replace =
with %3D
, but don't do that. Instead, use a proper URL encoding function。
感谢@Simba 和@David Schwartz 的评论,这是应该做的事情:
第 1 页:
- encrypt the string via openssl_encrypt();
- encode the encrypted string for sending via url by rawurlencode();
- concatenate those "encrypted+encoded" strings and send via url;
第 2 页:
- take the var out from $_GET;
- decode the var via rawurldecode();
- decrypt the decoded var via openssl_decrypt();
现在可以正常工作了~再次感谢~