John Davidson

Convert AES/CBC/PKCS5Padding from Java to PHP

0 comments
Message:


I must match an encrypted JSON object from Java to PHP but I failed to do so. Tried almost all solutions on stackoverflow, still no luck.


DOC says field must be obtained through AES encryption of the JSON representation of all the fields the merchant wants to send. Encryption algorithm must be AES/CBC/PKCS5Padding and must use as encrypting key the one provided. The initialization vector to be used for data encryption must be 16 bytes length equal to 0. Encrypted byte array must encoded to base64.


{"addrMatch":"N"} should convert to q8zzcOYHKggpSFXdmdZdObe2R/1EWnFgco2FJzjhtJQ=


I have the following code:


import java.security.InvalidAlgorithmParameterException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

public class Utility {
public static String encode3DSdata(String APISecretMerchant, String JSONobject) throws Throwable {

// Initialization vector
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
// AES Key from the API merchant key
byte[] key = APISecretMerchant.substring(0, 16).getBytes();
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");

byte[] toEncrypt = JSONobject.getBytes("UTF-8");
// Encrypt
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(toEncrypt);
// Convert to base64
return DatatypeConverter.printBase64Binary(encrypted);
}
}

Here is my code on PHP without luck:


        function pkcs5pad($text,$blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text.str_repeat(chr($pad), $pad);
}

function encrypt($data, $key)
{
$json = iconv('utf-8', 'utf-8//IGNORE', json_encode($data,JSON_UNESCAPED_SLASHES));
$padded=pkcs5pad($json,16);
$encrypted = openssl_encrypt($padded, 'AES-128-CBC', $key, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, self::iv);
return base64_encode($encrypted);
}

const iv = '\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0'; // also tried as "0000000000000000"
$key='c-MCjRx4X-pD-Lux';
$data=['addrMatch'=>'N'];
echo encrypt($data,$key);

I produce : Bpo0neCnY+dFrdISLYcefU8sqhX/4HSr5Io+zUe4sro=


Any help would be appreciated.



My Answer:
Here is an example of how you can encrypt a JSON object using AES/CBC/PKCS5Padding in PHP:

php

// JSON object to encrypt
$jsonObject = '{"field1": "value1", "field2": "value2"}';

// Encryption key provided
$encryptionKey = 'your_encryption_key_here';

// Initialization vector
$iv = str_repeat("\0", 16);

// Encrypt the JSON object
$encrypted = openssl_encrypt($jsonObject, 'AES-256-CBC', $encryptionKey, OPENSSL_RAW_DATA, $iv);

// Encode the encrypted data to base64
$encryptedBase64 = base64_encode($encrypted);

echo $encryptedBase64;

?>


Make sure to replace `'your_encryption_key_here'` with the actual encryption key provided. This code snippet uses the `openssl_encrypt` function in PHP to encrypt the JSON object using AES-256-CBC with the provided encryption key and initialization vector. The encrypted data is then encoded to base64 before being outputted.

Rate this post

5 of 5 based on 5592 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us