问题
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.
- Encryption Algorithm (duh!)
- Key (duh, again!)
- Key Size
- Mode of Operation (ECB, CBC, CTR)
- Initialization Vector (If CBC, no need for ECB)
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