3

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));
2
  • There is an issue with padding while doing encryption/decryption between Java-PHP. See this link - logikdev.com/2010/11/01/encrypt-with-php-decrypt-with-java Commented Jan 6, 2012 at 11:30
  • @ManishSharma: Oh thanks for mentioning Padding! :) Commented Jan 6, 2012 at 11:38

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.