在 Java 和 PHP 之间加密/解密字符串
我使用 AES 加密来加密和解密服务器端的 php 和 Android 应用程序(作为客户端)之间的字符串。
PHP 中的加密字符串是:
HaxRKnMxT24kCJWUXaVvqDHahzurJQK+sYA4lIHql/U=
在 Java 中是:
HaxRKnMxT24kCJWUXaVvqD/KMEkJTPTXEcCsHIYGX9TGtCNOHQcJyUURPk8qlgf3
我在 PHP 脚本中使用 phpseclib 来进行加密。
我在这里缺少什么?
相关的Java代码在这里
SecretKeySpec skeySpec = new SecretKeySpec(pad16(pass), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] out = c.doFinal( input )
和PHP代码在这里:
$aes = new Crypt_AES();
$aes->setKey('password');
$encrypted_encoded_text = base64_encode($aes->encrypt($plaintext));
I'm using AES encryption to encrypt and decrypt string between php on the server side and Android app (as client).
The encrypted string in PHP is:
HaxRKnMxT24kCJWUXaVvqDHahzurJQK+sYA4lIHql/U=
and in Java it's:
HaxRKnMxT24kCJWUXaVvqD/KMEkJTPTXEcCsHIYGX9TGtCNOHQcJyUURPk8qlgf3
I'm making use of phpseclib in the PHP script to do the encryption.
What am I missing here?
The relevant Java code here
SecretKeySpec skeySpec = new SecretKeySpec(pad16(pass), "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] out = c.doFinal( input )
And the PHP code here:
$aes = new Crypt_AES();
$aes->setKey('password');
$encrypted_encoded_text = base64_encode($aes->encrypt($plaintext));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使加密/解密能够跨不同语言工作,几乎不需要相同的东西。
填充方案
可能还有更多因素....
您确定所有这些在两种语言中都是相同的吗?如果是,那么您的加密/解密应该可以完美地工作,除非实现中存在错误(这种情况非常罕见但有可能)。
For the encryption/decryption to work across different languages, there are few things that needs to be the same.
Padding scheme
and probably some more factors too....
Are you sure all those are the same across both the languages? If yes, then your encryption/decryption should work flawlessly unless there is a bug in the implementation (which is very very rare but possible).