【问题标题】:PHP code to encrypt using 3DES algorithm using CBC mode and a secretkey and IV spec key使用 CBC 模式和密钥和 IV 规范密钥使用 3DES 算法加密的 PHP 代码
【发布时间】:2014-04-17 21:40:30
【问题描述】:

我尝试使用 3des(CBC) 使用 php 中的密钥和 IV 规范密钥加密以下数据,但我没有得到与我在此在线工具上获得的相同输出 (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";

按照我尝试的 PHP 代码

$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);
}

【问题讨论】:

    标签: php encryption 3des tripledes cbc-mode


    【解决方案1】:

    试试这个:

     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;
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-02
      • 2013-03-13
      • 2013-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-06-09
      • 1970-01-01
      相关资源
      最近更新 更多