将ColdFusion crypto()转换为PHP openssl_encrypt()

时间:2018-06-26 03:21:23

标签: php encryption coldfusion

我现在正在撞墙;我正在尝试使用ColdFusion encrypt()方法(PHP 7)将以下PHP's openssl_encrypt()代码转换为PHP

<cfset key = ToBase64(BinaryDecode("24a5d2b96b9aee2fb515c94fb36da508", "Hex"))>
<cfset encryptedString = Encrypt("Encrypting this string in CF and hopefully PHP too.", key, "AES", "Hex")>

这是我在PHP中的尝试: (我已经更新了代码。谢谢@Ageax的反馈!)

但是结果仍然不同。

$key = base64_encode(hex2bin("24a5d2b96b9aee2fb515c94fb36da508"));
$encrypted = openssl_encrypt(
    "Encrypting this string in CF and hopefully PHP too.",
    'AES-128-ECB',
    $key
);
echo '<p><strong>AES-128-ECB encryption</strong>: '.bin2hex(base64_decode($encrypted)).'</p>';

产生ColdFusion (感谢@Ageax):

Key: JKXSuWua7i+1FclPs22lCA==
Encryption: 1CCA4C862D3D2DC85637AF2F0E532145DEFC135F1268D5DFA991C77ED624BA0228E557BAEB06D96925B4A2968ACF55AC1312F28BCC42CB89EADE6BB76BD698F5

PHP的结果

Key: JKXSuWua7i+1FclPs22lCA==
Encryption: 4e487f1f9ff3ecaa82b5672803d9baecf5907c10bd91dbfdaf630927250c195f7e5a1d3e129da161b01a0a307ed73acf672d39614a91a53f6e1adcd1db6d1632

任何反馈都会很棒。谢谢!

1 个答案:

答案 0 :(得分:2)

我解决了!!!我的错误是将密钥编码为base 64(base64_encode),只需删除密钥就可以了!好极了! :)

错误:

$key = base64_encode(hex2bin("24a5d2b96b9aee2fb515c94fb36da508"));

正确:

$key = hex2bin("24a5d2b96b9aee2fb515c94fb36da508");

感谢大家的反馈!