Encrypt/Decrypt string between Java and PHP

非 Y 不嫁゛ 提交于 2019-12-09 04:59:24

问题


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));

回答1:


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)
  5. Initialization Vector (If CBC, no need for 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).



来源:https://stackoverflow.com/questions/8757101/encrypt-decrypt-string-between-java-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!