PHP 中的 Java 新 Base64(-1)?
我正在尝试在 php 中匹配 java base64 代码。但得到的结果不一致。
Java base64 编码
encMessage = URLEncoder.encode(new Base64(-1).encodeToString(encrypted),"UTF8");
Java 解码
message = URLDecoder.decode(message,"utf8");
上面的代码 java 编码代码返回我必须在 php 中解码和解密的字符串
PHP base64 解码
$message = utf8_decode(urldecode($encrypted));
$message = base64_decode($message);
PHP 编码
$encMessage = base64_encode($encrypted);
$encMessage = utf8_encode(urlencode($encMessage));
结果: java:
KO%2F%2B%2Bzbp5z8oCdvZn62jb72kseT%2Bem8hYUZY0IuB9zo%3D
php:
KO%2F%2B%2Bzbp5z8oCdvZn62jb3CVVVXsV%2Bws2kDOmKK%2BPEc%3D
I m trying to match java base64 code in php. But getting inconsistent result.
Java base64 encode
encMessage = URLEncoder.encode(new Base64(-1).encodeToString(encrypted),"UTF8");
Java decode
message = URLDecoder.decode(message,"utf8");
Above code java encode code return the string which i have to decode and decrypt in php
PHP base64 decode
$message = utf8_decode(urldecode($encrypted));
$message = base64_decode($message);
PHP encode
$encMessage = base64_encode($encrypted);
$encMessage = utf8_encode(urlencode($encMessage));
Results:
java:
KO%2F%2B%2Bzbp5z8oCdvZn62jb72kseT%2Bem8hYUZY0IuB9zo%3D
php:
KO%2F%2B%2Bzbp5z8oCdvZn62jb3CVVVXsV%2Bws2kDOmKK%2BPEc%3D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在 CSharp 和 Java 之间遇到了这个问题,发现 URL 编码是罪魁祸首。我在工作中所做的基本上是使用新生成的公钥重新加密数据,直到获得不需要 URL 编码的公钥为止。这不是一个很好的解决方案,但它确实有效,平均需要 2 次尝试才能正确,但我已经看到最多需要 15 次尝试才能完成,无论哪种方式,我们仍然在谈论毫秒,而且它工作可靠。
青年MMV
I had this problem between CSharp and Java, and found that URL encoding things was the culprit. What I did in my work around was basically re-encrypt the data with a newly generated public key until I got one that didn't need URL encoding. Not a great solution, but it works, it averages 2 tries to get it right, but I've seen it taking up to 15 tries to do it, either way we're still talking milliseconds, and it works reliably.
YMMV