I am encrypting some data in my Android application, which is then sent to a PHP page for decryption and treatment.
The cipher being used is "AES/CBC/PKCS5Padding"
Everything works fine now (after alot of digging around for info). However, the resulting decrypted data has a number of new lines added to the end which do not exist in the original data sent from the application.
I am presuming that this is a side effect of PHP not supporting PKCS5Padding. I am uncomfortable assuming that the end will always have newlines or spaces appended to the string.
If I try to use the code proposed in the mcrypt docs, the encrypted buffer is emptied.
Is there a better workaround for unpadding ?
Edit : code added as per request
PHP
$cipher = mcrypt_module_open(MCRYPT_RIJNDAEL_128, "", MCRYPT_MODE_CBC, "");
if($cipher === false)
{
trigger_error("AES compatible cipher missing", E_USER_WARNING);
exit;
}
$InitResult = mcrypt_generic_init($cipher, $AesPassword, $AesIv);
if($InitResult !== 0)
{
trigger_error("AES cipher init failed", E_USER_WARNING);
exit;
}
// now do the decryption
$DataBlock = mdecrypt_generic($cipher, $EncryptedBlock);
// close down mcrypt
mcrypt_generic_deinit($cipher);
mcrypt_module_close($cipher);
Android/Java :
String strEncrypted = null;
Cipher cipher = null;
IvParameterSpec ivSpec = null;
byte[] btEncrypted = null;
try
{
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
ivSpec = new IvParameterSpec(m_btIV);
cipher.init(Cipher.ENCRYPT_MODE, m_KeySpec, ivSpec);
btEncrypted = cipher.doFinal(strData.getBytes(m_strCharSet));
strEncrypted = Base64.encodeToString(btEncrypted, Base64.NO_PADDING | Base64.NO_WRAP);
}
catch(Exception e)
{
e.printStackTrace();
}
return strEncrypted;
Note that the key and iv are calculated in Android and transmitted in the POST data to the server.
Does this help ?