PKCS5填充 PHP DES算法库


  1. class Bdes extends CBase {
  2.  private $key; //密码
  3.  private $iv; //iv
  4.  private $mcrypt;
  5.  private $blockSize;
  6. <span id="more-201"></span> /**
  7.   *
  8.   * @author 肖江
  9.   * @param $key
  10.   * @param $iv
  11.   * @return unknown_type
  12.   */
  13.  function __construct($key,$iv) {
  14.   $this->key=$this->hexstr($key);
  15.   $this->iv=$this->toKey($iv);
  16.   $encodeType = MCRYPT_TripleDES;      
  17.         $mod = MCRYPT_MODE_CBC;
  18.         ////////////////////////////////////3des ecb/pkcs5 加密/////////////////
  19.         $this->mcrypt = mcrypt_module_open($encodeType, ”, $mod, ”);
  20.         if($this->mcrypt == false)   {
  21.             $this->error(‘加密解密模块打开错误,请安装相关组件’,110011);
  22.         }
  23.  }
  24.     /**
  25.      *
  26.      * @author 肖江
  27.      * @return unknown_type
  28.      */
  29.     function __destruct() {
  30.      mcrypt_generic_deinit($this->mcrypt);
  31.         mcrypt_module_close($this->mcrypt);
  32.     }
  33.  /**
  34.   *
  35.   * @author 肖江
  36.   * @param $data
  37.   * @return unknown_type
  38.   */
  39.     function desEncode($data)  {
  40.      $encodeType = MCRYPT_TripleDES;  
  41.         $ks = mcrypt_enc_get_key_size($this->mcrypt);
  42.         $key = substr($this->key, 0, $ks);
  43.  
  44.         if(mcrypt_generic_init($this->mcrypt, $key, $this->iv) < 0)
  45.         {
  46.             mcrypt_module_close($this->mcrypt);
  47.             $this->error(‘加密解密模块执行错误’,110012);
  48.         }
  49.         $this->blockSize = mcrypt_module_get_algo_block_size ($encodeType);
  50.         $tmpLen = $this->blockSize – strlen($data)% $this->blockSize;
  51.         for($i = 0; $i < $tmpLen; $i++)     {
  52.             $data .= pack("h", $tmpLen);
  53.         }
  54.  
  55.         $encodeStr = mcrypt_generic($this->mcrypt, $data);
  56.         return $this->strhex($encodeStr);
  57.     }
  58.     //DES 解密
  59.     /**
  60.      *
  61.      * @author 肖江
  62.      * @param $data
  63.      * @return unknown_type
  64.      */
  65.     function desDecode($data)
  66.     {
  67.         $ks = mcrypt_enc_get_key_size($this->mcrypt);
  68.         $key = substr($this->key, 0, $ks);
  69.  
  70.         if(mcrypt_generic_init($this->mcrypt, $key, $this->iv) < 0)
  71.         {
  72.             mcrypt_module_close($this->mcrypt);
  73.             $this->error(‘加密解密模块执行错误’,110012);
  74.         }
  75.      $data=$this->hexstr($data);
  76.         $decodeStr = mdecrypt_generic($this->mcrypt, $data);
  77.         //去掉pkcs5填充
  78.         $strLen = strlen($decodeStr);
  79.         $theLastPosChar =  substr($decodeStr, -1);
  80.         $theFill = hexdec(bin2hex($theLastPosChar));
  81.         $decodeStr = substr($decodeStr, 0, $strLen – $theFill);
  82.         return $decodeStr;
  83.     }
  84.     /**
  85.      *
  86.      * @author 肖江
  87.      * @param $var
  88.      * @return unknown_type
  89.      */
  90.  function toKey($var) {
  91.   $re=”;
  92.   foreach($var as $per) {
  93.    $re=$re.chr($per);
  94.   }
  95.   return $re;
  96.  }
  97.  /**
  98.   *
  99.   * @author 肖江
  100.   * @param $string
  101.   * @return unknown_type
  102.   */
  103.   function strHex($string) {
  104.      $hex="";
  105.      for ($i=0;$i<strlen($string);$i++)
  106.          $hex.=(strlen(dechex(ord($string[$i])))<2)? "0".dechex(ord($string[$i])): dechex(ord($string[$i]));
  107.      return $hex;
  108.  }
  109.  /**
  110.   *
  111.   * @author 肖江
  112.   * @param $hex
  113.   * @return unknown_type
  114.   */
  115.  function hexstr($hex)
  116.  {
  117.      $string="";
  118.      for ($i=0;$i<strlen($hex)-1;$i+=2)
  119.          $string.=chr(hexdec($hex[$i].$hex[$i+1]));
  120.      return $string;
  121.  }
  122. }
  1. No comments yet.
(will not be published)

  1. No trackbacks yet.