Search code examples
phpcryptography3des

Cannot find the right 3DES cipher in PHP


I am trying to compute a PHP function to have the 3DES (Triple DES) in ECB Mode. But I am getting the wrong result.

My result : 615EDC0E8EAD5DDE

Expected result : 7B66D9A5010A8035

(the expected result is computed with HSM and confirmed with the website) http://tripledes.online-domain-tools.com/

Here is my PHP function, taking as parameters :

$data = "3200000025381234"

$key = "98137332E06BBA25AEE51CFD150EA8E3"

function tripleDES($data, $key) {
   $key= hex2bin($key);
   $data = hex2bin($data);
   $enc = openssl_encrypt($data, 'des-ede3', $key, OPENSSL_RAW_DATA | 
           OPENSSL_ZERO_PADDING);

   return strtoupper(bin2hex($enc));
}

What am I doing wrong ?


Solution

  • Thanks to the answer of Topaco, I understood my mistake. So I used this github project: https://github.com/gilfether/phpcrypt and corrected my code this way (using my 16-bytes key):

    function triple_DES($data, $key){
       $key = hex2bin($key);
       $data = hex2bin($data);
       $crypt = new PHP_Crypt($key, PHP_Crypt::CIPHER_3DES, PHP_Crypt::MODE_ECB);
       $encrypt = $crypt->encrypt($data);
    
       return strtoupper(bin2hex($encrypt));
    }