AES/ECB/NoPadding 解密不适用于 PHP OpenSSL

问题描述 投票:0回答:1

加密值:673a487f841c4c0b405cb3fee05b3385

加密密钥:Y4IC99u0pqLKIrpr

解密值:96895018020

我们尝试使用下面的 Java 代码并且它正在工作。

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();
}

}

可以看在线解密作为参考

在 PHP 中,我们有如下代码,但它给出了一些垃圾值。

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

那么您能帮助我们解决 PHP-OPENSSL 中的错误吗?

php encryption php-openssl
1个回答
0
投票
$encrypted = hex2bin('673a487f841c4c0b405cb3fee05b3385');
$decrypted = openssl_decrypt($encrypted, 'AES-128-ECB', 'Y4IC99u0pqLKIrpr',
                             OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);
$decrypted = rtrim($decrypted, "\0");
  • hex2bin:解码十六进制编码的二进制字符串。

  • OPENSSL_RAW_DATA

    $data
    可以如描述所示为raw或base64。如果未设置
    $option
    ,则数据将被假定为采用 Base64 编码。如果设置了参数 OPENSSL_RAW_DATA,它将被理解为行数据。

  • rtrim:从字符串末尾去除空字节。

© www.soinside.com 2019 - 2024. All rights reserved.