AES/ECB/NoPadding Decryption not working with PHP OpenSSL

75 Views Asked by At

Encrypted Value : 673a487f841c4c0b405cb3fee05b3385

Encryption Key : Y4IC99u0pqLKIrpr

Decryption Value : 96895018020

We tried with below Java Code & it is working.

public class oomaHeDecryption {

private static final String CipherMode = "AES/ECB/NoPadding";

public static byte[] decrypt(byte[] content, String password) {
    try {
        SecretKeySpec key = createKey(password);
        Cipher cipher = Cipher.getInstance(CipherMode);
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] result = cipher.doFinal(content);
        return result;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

public static String decrypt(String content, String password) {
    byte[] data = null;
    try {
        data = hex2byte(content);
    } catch (Exception e) {
        e.printStackTrace();
    }
    data = decrypt(data, password);
    if (data == null)
        return null;
    String result = null;
    try {
        result = new String(data, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    result = (result.length() > 11) ? result.substring(0, 11) : result;
    return result;
}

public static SecretKeySpec createKey(String password) {
    byte[] data = null;
    if (password == null) {
        password = "";
    }
    StringBuffer sb = new StringBuffer(16);
    sb.append(password);
    while (sb.length() < 16) {
        sb.append("0");
    }
    if (sb.length() > 16) {
        sb.setLength(16);
    }
    try {
        data = sb.toString().getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return new SecretKeySpec(data, "AES");
}

private static byte[] hex2byte(String inputString) {
    if (inputString == null || inputString.length() < 2) {
        return new byte[0];
    }
    inputString = inputString.toLowerCase();
    int l = inputString.length() / 2;
    byte[] result = new byte[l];
    for (int i = 0; i < l; ++i) {
        String tmp = inputString.substring(2 * i, 2 * i + 2);
        result[i] = (byte) (Integer.parseInt(tmp, 16) & 0xFF);
    }
    return result;
}

@SuppressWarnings("null")
public static <ASCII> String asciiToHex(String asciiValue) {
    char[] chars = ((String) asciiValue).toCharArray();
    StringBuffer hex = new StringBuffer();
    for (int i = 0; i < chars.length; i++) {
        hex.append(Integer.toHexString((int) chars[i]));
    }
    return hex.toString();
}

}

You can see online decryption as a reference enter image description here

In PHP, we have code as below but it gives some garbage value.

$decrypted = openssl_decrypt('673a487f841c4c0b405cb3fee05b3385', 'AES-128-ECB', 'Y4IC99u0pqLKIrpr', OPENSSL_ZERO_PADDING);
var_dump($decrypted);

So can you help us where we are doing wrong in PHP-OPENSSL?

1

There are 1 best solutions below

0
shingo On BEST ANSWER
$encrypted = hex2bin('673a487f841c4c0b405cb3fee05b3385');
$decrypted = openssl_decrypt($encrypted, 'AES-128-ECB', 'Y4IC99u0pqLKIrpr',
                             OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$decrypted = rtrim($decrypted, "\0");
  • hex2bin: Decodes a hexadecimally encoded binary string.

  • OPENSSL_RAW_DATA: $data can be as the description says raw or base64. If no $option is set, data will be assumed to be base64 encoded. If parameter OPENSSL_RAW_DATA is set, it will be understood as raw data, i.e. bytes.

  • OPENSSL_ZERO_PADDING: Unlike its literal meaning, in PHP this option simply means no padding is performed.

  • rtrim: Strip null bytes from the end of a string.