I have a php code that encrypts a plain text using rijndael algorithm in the EBC mode, and a 17-charachter key length. I'm looking to use an equivalent AES algorithm with Java
This is my php code:
<?php
$key=" 4288f0b8060ca1b ";
$mcryptAlgo= MCRYPT_RIJNDAEL_128 ;
$mcryptMode= MCRYPT_MODE_ECB ;
$data = "text_to_crypt_with_aes" ;
$mcryptedData = mcrypt_encrypt ($mcryptAlgo, $key, $data, $mcryptMode);
$parametres_chiffres = urlencode( base64_encode ($mcryptedData));
echo($parametres_chiffres);
?>
It returns as a result the encrypted message: 4LepwOstJA0R2bg5FrdQXeoxesxmKV4pkf514F3VDqU%3D
However, the following Java code that I've built doesn't return the same message:
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
sb.append("text_to_crypt_with_aes");
String clearText = sb.toString();
StringBuilder sbKey = new StringBuilder(" 4288f0b8060ca1b ");
for (int i = 0; i < 7; i++) {
sbKey.append("\0");
}
try {
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
Key key = new SecretKeySpec(sbKey.toString().getBytes("UTF-8"),
"AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedMessageInBytes = cipher.doFinal(clearText
.getBytes("UTF-8"));
byte[] b64 = Base64.encodeBase64(encryptedMessageInBytes);
String scrambled_text = new String(b64, Charset.forName("US-ASCII"));
System.out.println(scrambled_text);
} catch (Exception e) {
e.printStackTrace();
}
}
It returns actually: 4LepwOstJA0R2bg5FrdQXRutIOZlJi06f0D8NnnIG5Q=
How can I adapt my Java code to return exactly the same as in php ?