Search code examples
phpencryptionencryption-symmetric

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


Solution

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