I can't get the same result of encrypted text in JAVA from my php script. This is my php code (client side - my side):
$input = 'my text to be encrypted';
$key = 'my key';
$size = mcrypt_get_block_size(MCRYPT_DES, 'ecb');
$input = pkcs5_pad($input, $size);
$td = mcrypt_module_open(MCRYPT_DES, '', 'ecb', '');
$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
mcrypt_generic_init($td, $key, $iv);
$data = mcrypt_generic($td, $input);
print base64_encode($data);
function pkcs5_pad ($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
Bu taban JAVA kodu (Sunucu tarafı) olduğunu:
SecretKey key = new SecretKeySpec(keyBytes, "DES");
Cipher ecipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new String(Base64Utils.encode(enc));
I know there's a padding problem in php that makes the difference in my result. Can't find why.