I have to do encrypt data on server running php script send them to another server running Java using AES algorithm. I chose to use phpseclib and the javax.crypto package respectively for the php and the Java side.
Unfortunately a have padding exception. Here is my code:
On php side:
$aes = new Crypt_AES('MODE_ECB');
$key = "secretkeyfor1234secretke";
$aes->setKey($key);
$plaintext = 'Good evening this is just a test';
$cryptedmessage = base64_encode($aes->encrypt($plaintext));
On java side: clefAES contain the key and data contain the ciphered text. I send the key using RSA encryption (not reported here).
data = EFjTatx2VkAZR3ScS0UadQr8M6zEkIz/kAX0Cl+XH2FNNHVbeJsEd2b+zWlEkvR6;//( it is base64 encoded )
Cipher aescipher = Cipher.getInstance("AES/ECB/NoPadding", "SunJCE");
Key keySpec = new SecretKeySpec(clefAES, "AES");
byte[] encryptedTextByte = decoder.decodeBuffer(data);
aescipher.init(Cipher.DECRYPT_MODE, keySpec);
byte[] decryptedByte = aescipher.doFinal(encryptedTextByte);
String decryptedText = new String(decryptedByte);
this is the result gave by decryptedText :
Good evening thicx��#3mg�? i�#�Ԁ���O�q
If someone know why I am getting this result and how to solve this it would be really helpful. Also is phpseclib compatible with javax.crypto package for aes encryption? Is it the best way to do what I want to achieve?
EFjTatx2VkAZR3ScS0UadQr8M6zEkIz/kAX0Cl+XH2FNNHVbeJsEd2b+zWlEkvR6is not base64-encoded AES/ECB/NoPadding-encrypted. The base64-encoded cipher text I get from Java code isGYQGpH6fRpHBP0ACElbcp9i8pjKf4G7wiPLDL7TM1Ec=.