Decrypting 3des from hex data with a hex key

霸气de小男生 提交于 2019-12-12 07:04:29

问题


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:


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.



来源:https://stackoverflow.com/questions/41272754/decrypting-3des-from-hex-data-with-a-hex-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!