Purushotam Sangroula
Purushotam Sangroula

Reputation: 485

Decrypt a des string in to plain text

I need to decrypt a string encrypted with des algorithm. How can I do in PHP? I have real test cases as follows:

key ='0123456789abcdef'
encryptedValue = '88C10F0B8C084E5F'; //hex value
decodedValue = '2020202039353538';  // this is hex

I've tried

$keyValue ='0123456789abcdef';
$encryptedValue = '88C10F0B8C084E5F'; //hex value
$decodedValue = '2020202039353538';  // this is hex

$decryptedData = mcrypt_decrypt( MCRYPT_DES, $keyValue, $encryptedValue , 'ecb');
var_dump($decryptedData);
var_dump($decodedValue);

Output of decryptedData is null. I checked this solution. Please suggest me a solution.

Update:2017 Jan 18: Many people are suggeting me not use des or mcrypt. I need to decrypt this because my API provider reponds me with this algorithm. And about mcrypt_decrypt function, I did not find an alternative. Now please suggest me more.

I tried according to @duskwuff, I made modifications as.

$decryptedData = mcrypt_decrypt( MCRYPT_DES, $keyValue, hex2bin($encryptedValue) 'ecb');
var_dump(bin2hex($decryptedData));

Output is empty string which is obviously binary representation of bool false

For you convenience I want to share the result of crypto calculator.enter image description here I'm getting this warning as well:Warning: mcrypt_decrypt(): Key of size 16 not supported by this algorithm. Only keys of size 8 supported in /var/www/html/encdec/enc.phtml on line 13

Upvotes: 0

Views: 609

Answers (2)

Purushotam Sangroula
Purushotam Sangroula

Reputation: 485

I solved my issue by using following code:

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

Upvotes: 0

user149341
user149341

Reputation:

The values you're passing into mcrypt_decrypt() look like they're intended to be a representation of hexadecimal data, not passed in directly. Use hex2bin() on the inputs to convert them to binary data, and bin2hex() to convert the output back to the expected representation.

Also, stop using mcrypt. It's old and broken, and has been removed from PHP 7.2.

Upvotes: 2

Related Questions