用PHP中的openssl_decrypt解密des

时间:2018-01-18 06:16:41

标签: encryption openssl cryptography php-5.6

尝试使用以下代码解密单个des加密数据:

$keyValue ='0123456789abcdef'; //hex value
$encryptedOrderId = '88C10F0B8C084E5F'; //hex value
$binaryEncrypted = hex2bin($encryptedOrderId);
$decodeValueByOnlineTool = '2020202039353538';  // this is hex
$opensslDecrypt = openssl_decrypt(  $encryptedOrderId  ,'DES-ECB', $keyValue, OPENSSL_RAW_DATA , '' );
var_dump($opensslDecrypt);

输出为false。我不知道我在做什么错。

我工具的输出如下: enter image description here

1 个答案:

答案 0 :(得分:1)

您的输入为十六进制。 openssl_decrypt期待二进制。在传递给hex2bin之前,请在每个输入上使用openssl_decrypt

openssl_decrypt(hex2bin($encryptedOrderId), 'DES-ECB', hex2bin($keyValue), ...

请记住将结果转换回十六进制以获得所需的结果。确保您已根据屏幕截图设置OPENSSL_ZERO_PADDING

编辑:我使用的确切代码......

$keyValue ='0123456789abcdef'; //hex value
$encryptedOrderId = '88C10F0B8C084E5F'; //hex value
$binaryEncrypted = hex2bin($encryptedOrderId);
$decodeValueByOnlineTool = '2020202039353538';  // this is hex
$opensslDecrypt = openssl_decrypt( $binaryEncrypted  ,'des-ecb', hex2bin($keyValue), OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING , '');
var_dump($opensslDecrypt);