Send and receive encrypted data between PHP and JAVA. part 1
Part1. Using a secret key to encrypt and decrypt data
In this example we use AES-128 (rijndael-128) and CBC method, so we use a secret key and an initial vector key (iv key) to encrypt and decrypt data.
Encryption in JAVA
// The keys String secretKey = "1234567890abcde"; String ivKey = "fedcba9876543210"; // Create key specs SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivKey.getBytes()); // the text that we want to encrypt String plaintext = "this is the text that we want to encrypt in java"; // pad the string String padded = padString16(plaintext); // Instance the chipper Cipher cipherEc = Cipher.getInstance("AES/CBC/NoPadding"); // init the chipper with key specs cipherEc.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); // Encrypt the text byte[] encrypted = cipherEc.doFinal(padded.getBytes()); // Encode the text with base 64 BASE64Encoder encoder = new BASE64Encoder(); System.out.println(encoder.encode(encrypted));
The output of script above is:
VD9X9nF6uENC+H8TKazjztadnp6wO7kqUBvpH7tQPrWYGLaB8ltR99U2VZ/MGtIeE5yhNr2aYE3hM6Mgm+jhRg==
The pad string method
// pad string needed because the string lenght must be a multiplication of 16
private String padString16(String text) {
int mod = 16 - (text.length() % 16);
String empty = "";
for (int i = 0 ; i < mod ; i++){
empty = empty.concat(" ");
}
return text.concat(empty);
}
Decryption in PHP
// The keys String secretKey = "1234567890abcde"; String ivKey = "fedcba9876543210"; $cryptedText = "VD9X9nF6uENC+H8TKazjztadnp6wO7kqUBvpH7tQPrWYGLaB8ltR99U2VZ/MGtIeE5yhNr2aYE3hM6Mgm+jhRg=="; // Decode the encrypted text $text = base64_decode($cryptedText); // open and init the mcrypt module $td = mcrypt_module_open("rijndael-128", "", "cbc", $ivKey); mcrypt_generic_init($td, $secretKey, $ivKey); // decrypt the text $decrypted = mdecrypt_generic($td, $text); // deinit and close the mcrypt module mcrypt_generic_deinit($td); mcrypt_module_close($td); echo $decrypted;
the output of script above is “this is the text that we want to encrypt in java”
Encryption in PHP
// The keys String secretKey = "1234567890abcde"; String ivKey = "fedcba9876543210"; $plaintext = "this is the text that we want to encrypt in php"; // Open and init the mcrypt library with the keys $td = mcrypt_module_open("rijndael-128", "", "cbc", $ivKey); mcrypt_generic_init($td, $secretKey, $ivKey); // encrypt the text $crypt_text = mcrypt_generic($td, $plaintext); // deinit and close mcrypt module mcrypt_generic_deinit($td); mcrypt_module_close($td); // encode with base 64 echo base64_encode($crypt_text);
The output of above script is
VD9X9nF6uENC+H8TKazjztadnp6wO7kqUBvpH7tQPrU/WKCMgKV6cPwoP+75ju5u
Decryption in JAVA
// The keys String secretKey = "1234567890abcde"; String ivKey = "fedcba9876543210"; // Create key specs SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivKey.getBytes()); // Instance the chipper Cipher cipherDc = Cipher.getInstance("AES/CBC/NoPadding"); // init the chipper with key specs cipherDc.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); // insert the encrypted string String message = "VD9X9nF6uENC+H8TKazjztadnp6wO7kqUBvpH7tQPrU/WKCMgKV6cPwoP+75ju5u"; // Decode the encrypted string with base 64 BASE64Decoder decoder = new BASE64Decoder(); byte[] decodedMessage = decoder.decodeBuffer(message); byte[] outText = cipherDc.doFinal(decodedMessage); System.out.println(new String(outText).trim());
the output of script above is “this is the text that we want to encrypt in php“












