简体   繁体   English

加密/解密Java和PHP之间的字符串

[英]Encrypt/Decrypt string between Java and PHP

I'm using AES encryption to encrypt and decrypt string between php on the server side and Android app (as client). 我正在使用AES加密来加密和解密服务器端的php和Android应用程序(作为客户端)之间的字符串。

The encrypted string in PHP is: PHP中的加密字符串是:

HaxRKnMxT24kCJWUXaVvqDHahzurJQK+sYA4lIHql/U=

and in Java it's: 在Java中它是:

HaxRKnMxT24kCJWUXaVvqD/KMEkJTPTXEcCsHIYGX9TGtCNOHQcJyUURPk8qlgf3

I'm making use of phpseclib in the PHP script to do the encryption. 我正在PHP脚本中使用phpseclib来进行加密。

What am I missing here? 我在这里错过了什么?

The relevant Java code here 这里有相关的Java代码

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: 这里的PHP代码:

$aes = new Crypt_AES();
$aes->setKey('password');
$encrypted_encoded_text =  base64_encode($aes->encrypt($plaintext));

For the encryption/decryption to work across different languages, there are few things that needs to be the same. 为了使加密/解密能够跨不同语言工作,很少有东西需要相同。

  1. Encryption Algorithm (duh!) 加密算法(呃!)
  2. Key (duh, again!) 钥匙(呃,再次!)
  3. Key Size 密钥大小
  4. Mode of Operation (ECB, CBC, CTR) 运作模式(ECB,CBC,CTR)
  5. Initialization Vector (If CBC, no need for ECB) 初始化向量(如果是CBC,则不需要ECB)
  6. 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). 如果是,那么你的加密/解密应该完美无缺,除非在实现中存在错误(这是非常罕见但可能的)。

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

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