本文实例讲述了php des加密算法。分享给大家供大家参考,具体如下:

yii框架的des代码

<?php
/**
 *@see yii csecuritymanager;
 */
class des{
 public static function encrypt($data,$key){
   $module=mcrypt_module_open('des','', mcrypt_mode_cbc,'');
   $key=substr(md5($key),0,mcrypt_enc_get_key_size($module));
   srand();
   $iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($module), mcrypt_rand);
   mcrypt_generic_init($module,$key,$iv);
   $encrypted=$iv.mcrypt_generic($module,$data);
   mcrypt_generic_deinit($module);
   mcrypt_module_close($module);
   return md5($data).'_'.base64_encode($encrypted);
 }
 public static function decrypt($data,$key){  
   $_data = explode('_',$data,2);
   if(count($_data)<2){
    return false;
   }
   $data = base64_decode($_data[1]);   
   $module=mcrypt_module_open('des','', mcrypt_mode_cbc,'');
   $key=substr(md5($key),0,mcrypt_enc_get_key_size($module));
   $ivsize=mcrypt_enc_get_iv_size($module);
   $iv=substr($data,0,$ivsize);
   mcrypt_generic_init($module,$key,$iv);
   $decrypted=mdecrypt_generic($module,substr($data,$ivsize,strlen($data)));
   mcrypt_generic_deinit($module);
   mcrypt_module_close($module);
   $decrypted = rtrim($decrypted,"
<?php
/**
*@see yii csecuritymanager;
*/
class des{
public static function encrypt($data,$key){
$module=mcrypt_module_open('des','', mcrypt_mode_cbc,'');
$key=substr(md5($key),0,mcrypt_enc_get_key_size($module));
srand();
$iv=mcrypt_create_iv(mcrypt_enc_get_iv_size($module), mcrypt_rand);
mcrypt_generic_init($module,$key,$iv);
$encrypted=$iv.mcrypt_generic($module,$data);
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
return md5($data).'_'.base64_encode($encrypted);
}
public static function decrypt($data,$key){  
$_data = explode('_',$data,2);
if(count($_data)<2){
return false;
}
$data = base64_decode($_data[1]);   
$module=mcrypt_module_open('des','', mcrypt_mode_cbc,'');
$key=substr(md5($key),0,mcrypt_enc_get_key_size($module));
$ivsize=mcrypt_enc_get_iv_size($module);
$iv=substr($data,0,$ivsize);
mcrypt_generic_init($module,$key,$iv);
$decrypted=mdecrypt_generic($module,substr($data,$ivsize,strlen($data)));
mcrypt_generic_deinit($module);
mcrypt_module_close($module);
$decrypted = rtrim($decrypted,"\0");    
if($_data[0]!=md5($decrypted)){
return false;
}
return $decrypted;
}
}
"); if($_data[0]!=md5($decrypted)){ return false; } return $decrypted; } }

在网上看到了一篇文章,讲到:

由于php使用mcrypt扩展进行3des加密,填充模式是跟java以及.net是不一样的,java和.net填充模式使用的是pkcs7。

所以php端必须自定义一个函数对加密字符串进行pkcs7模式补位填充。

另外一点就是双方的key注意进行base64编码,最后php端经过3des加密后得到的结果也需要进行base64编码。

以上几点都做好之后,加密结果就一致了。

下面是兼容c#和java的3des加密的算法

<?php
class std3des
{
  private $key = "";
  private $iv = "";
  /**
  * 构造,传递二个已经进行base64_encode的key与iv
  *
  * @param string $key
  * @param string $iv
  */
  function __construct ($key, $iv)
  {
    if (empty($key) || empty($iv)) {
      echo 'key and iv is not valid';
      exit();
    }
    $this->key = $key;
    $this->iv = $iv;
  }
  /**
  *加密
  * @param <type> $value
  * @return <type>
  */
  public function encrypt ($value)
  {
    $td = mcrypt_module_open(mcrypt_3des, '', mcrypt_mode_cbc, '');
    $iv = base64_decode($this->iv);
    $value = $this->paddingpkcs7($value);
    $key = base64_decode($this->key);
    mcrypt_generic_init($td, $key, $iv);
    $ret = base64_encode(mcrypt_generic($td, $value));
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $ret;
  }
  /**
  *解密
  * @param <type> $value
  * @return <type>
  */
  public function decrypt ($value)
  {
    $td = mcrypt_module_open(mcrypt_3des, '', mcrypt_mode_cbc, '');
    $iv = base64_decode($this->iv);
    $key = base64_decode($this->key);
    mcrypt_generic_init($td, $key, $iv);
    $ret = trim(mdecrypt_generic($td, base64_decode($value)));
    $ret = $this->unpaddingpkcs7($ret);
    mcrypt_generic_deinit($td);
    mcrypt_module_close($td);
    return $ret;
  }
  private function paddingpkcs7 ($data)
  {
    $block_size = mcrypt_get_block_size('tripledes', 'cbc');
    $padding_char = $block_size - (strlen($data) % $block_size);
    $data .= str_repeat(chr($padding_char), $padding_char);
    return $data;
  }
  private function unpaddingpkcs7($text)
  {
    $pad = ord($text{strlen($text) - 1});
    if ($pad > strlen($text)) {
      return false;
    }
    if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) {
      return false;
    }
    return substr($text, 0, - 1 * $pad);
  }
}
?>

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

在线des加密/解密工具

md5在线加密工具:
http://tools.jb51.net/password/createmd5password

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具: