zoukankan      html  css  js  c++  java
  • php 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,"");
    if($_data[0]!=md5($decrypted)){
    return false;
    }
    return $decrypted;
    }

    }

  • 相关阅读:
    申请奖励加分
    寒假学习01
    加分项及建议
    12月30日总结
    12月17日 期末总结
    12月31日总结
    12月15日总结
    12月28日总结
    01月03日总结
    01月05日总结
  • 原文地址:https://www.cnblogs.com/lijiageng/p/5953377.html
Copyright © 2011-2022 走看看