Purushotam Sangroula
Purushotam Sangroula

Reputation: 485

Decrypt des with openssl_decrypt in PHP

Trying to decrypt a single des encrypted data using this code:

$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);

The output is false. I do not know what wrong I'm doing.

The output from my tool is as follows: enter image description here

Upvotes: 0

Views: 2569

Answers (1)

Luke Joshua Park
Luke Joshua Park

Reputation: 9806

Your inputs are in hex. openssl_decrypt expects binary. Use hex2bin on each input before passing to openssl_decrypt.

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

Remember to convert the result back into hex to get the result you want. Make sure you've set OPENSSL_ZERO_PADDING as per your screenshot.

EDIT: The exact code I used...

$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);

Upvotes: 3

Related Questions