Decrypting 3des from hex data with a hex key

1.1k views Asked by At

I am trying to use the mycrypt php library to decrypt the following:

Key: aaaaaaaabbbbbbbbccccccccdddddddd

Data: b5057bbc04b842a96144a0f617f2820e

Expected Result: Test123123

The data is encrypted with 3DES with the mode ECB. The code I'm currently working with decrypts the hex value to "e2119b734b5050e3" which translates to "â›sKPPã". I have tried using open ssl which is returning "False".

The code is as follows:

(PHP Version 5.3.3)

$key = 'aaaaaaaabbbbbbbbccccccccdddddddd';
$key = pack('H*',$key);

// DATA
$data = 'b5057bbc04b842a96144a0f617f2820e';
$data = pack('H'.strlen($key),$data);

// DECRYPT MCRYPT
$data = rtrim(mcrypt_decrypt(MCRYPT_3DES, $key, $data, MCRYPT_MODE_ECB), "\0");
$decryptedHex = unpack('H*',$data);

// DECRYPT OPEN SSL (RETURNS FALSE)
$result = openssl_decrypt($data,'des-ede3', $key);

// ECHO
echo $decryptedHex[1];
1

There are 1 answers

1
pvg On BEST ANSWER

The problem here is that there is too much missing information - the exact variant of 3DES, the padding info. With a little fiddling with encrypting options, rather than decrypting one can try to generate the ciphertext to find the correct options. They turn out to be

openssl_encrypt($ptext,'des-ede', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING)

Where $ptext is "Test123123\0\0\0\0\0\0"

The ciphertext can similarly be decrypted via

$result = openssl_decrypt($data,'des-ede', $key, OPENSSL_RAW_DATA | OPENSSL_ZERO_PADDING);

You will need to upgrade to a more recent and supported (PHP 5.3 was released in 2009 and is no longer supported as of 2015) version of PHP.