2

i am try to encrypt following data using the 3des(CBC) using a secretkey and IV spec key in php but i am not getting the same output which i get on this online tool (http://symmetric-ciphers.online-domain-tools.com/)

//input
$data = "Id=120278;timestamp=2009-02-05 08:28:39.195";
$key = "80127ECD5E40BB25DB14354A3795880DF2B459BB08E1EE6D";
$iv = "331BA9C5A7446C98";

//output from online tool. I should get the same result in my php code
$result = "1C80CBCE1713128176499C7A3DFB8779156B31B8DEF2F667A7100F1C3AEFABACB24283CFDF 5D312D A0074897138684BC";

following the PHP code i tried

$string = "Id=120278;timestamp=2009-02-05 08:28:39.195";
$iv = "331BA9C5A7446C98";
$passphrase = "80127ECD5E40BB25DB14354A3795880DF2B459BB08E1EE6D"; 
$encryptedString = encryptString($string, $passphrase, $iv);

function encryptString($unencryptedText, $passphrase, $iv) { 
  $enc = mcrypt_encrypt(MCRYPT_3DES, $passphrase, $unencryptedText, MCRYPT_MODE_CBC, $iv); 
  return base64_encode($enc);
}
Kaii
  • 20,122
  • 3
  • 38
  • 60
user3546274
  • 21
  • 1
  • 2

1 Answers1

1

Try this:

 function encrypt3DES($key,$iv,$text_enc){
       $block = mcrypt_get_block_size('tripledes', 'cbc');
       $pad = $block - (strlen($text_enc) % $block);
       $text_enc .= str_repeat(chr($pad), $pad);
       $text_enc = mcrypt_encrypt(MCRYPT_3DES, $key, $text_enc, MCRYPT_MODE_CBC, $iv);
       $text_enc = base64_encode ($text_enc);
       return $text_enc;
   }
Priyesh
  • 11
  • 1