[PHP] DES加密解密 →→→→→进入此内容的聊天室

来自 , 2020-10-02, 写在 PHP, 查看 136 次.
URL http://www.code666.cn/view/353de269
  1. <?php
  2.     class authCode {
  3.         public $ttl;//到期时间 时间格式:20120101(年月日)
  4.         public $key_1;//密钥1
  5.         public $key_2;//密钥2
  6.         public $td;
  7.         public $ks;//密钥的长度
  8.         public $iv;//初始向量
  9.         public $salt;//盐值(某个特定的字符串)
  10.         public $encode;//加密后的信息
  11.         public $return_array = array(); // 返回带有MAC地址的字串数组
  12.         public $mac_addr;//mac地址
  13.         public $filepath;//保存密文的文件路径
  14.         public function __construct(){
  15.             //获取物理地址
  16.             $this->mac_addr=$this->getmac(PHP_OS);
  17.             $this->filepath="./licence.txt";
  18.             $this->ttl="20120619";//到期时间
  19.             $this->salt="~!@#$";//盐值,用以提高密文的安全性
  20. //            echo "<pre>".print_r(mcrypt_list_algorithms ())."</pre>";
  21. //            echo "<pre>".print_r(mcrypt_list_modes())."</pre>";
  22.         }
  23.         /**
  24.          * 对明文信息进行加密
  25.          * @param $key 密钥
  26.          */
  27.         public function encode($key) {
  28.             $this->td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); //使用MCRYPT_DES算法,ecb模式
  29.             $size=mcrypt_enc_get_iv_size($this->td);//设置初始向量的大小
  30.             $this->iv = mcrypt_create_iv($size, MCRYPT_RAND);//创建初始向量
  31.             $this->ks = mcrypt_enc_get_key_size($this->td);//返回所支持的最大的密钥长度(以字节计算)
  32.             $this->key_1 = substr(md5(md5($key).$this->salt),0,$this->ks);
  33.             mcrypt_generic_init($this->td, $this->key_1, $this->iv); //初始处理
  34.             //要保存到明文
  35.             $con=$this->mac_addr.$this->ttl;
  36.             //加密
  37.             $this->encode = mcrypt_generic($this->td, $con);  
  38.             //结束处理
  39.             mcrypt_generic_deinit($this->td);
  40.             //将密文保存到文件中
  41.             $this->savetofile();
  42.         }
  43.         /**
  44.          * 对密文进行解密
  45.          * @param $key 密钥
  46.          */
  47.         public function decode($key) {
  48.             try {
  49.                 if (!file_exists($this->filepath)){
  50.                     throw new Exception("授权文件不存在");
  51.                 }else{//如果授权文件存在的话,则读取授权文件中的密文
  52.                     $fp=fopen($this->filepath,'r');
  53.                     $secret=fread($fp,filesize($this->filepath));
  54.                     $this->key_2 = substr(md5(md5($key).$this->salt),0,$this->ks);
  55.                     //初始解密处理
  56.                     mcrypt_generic_init($this->td, $this->key_2, $this->iv);
  57.                     //解密
  58.                     $decrypted = mdecrypt_generic($this->td, $secret);
  59.                     //解密后,可能会有后续的\0,需去掉  
  60.                     $decrypted=trim($decrypted) . "\n";  
  61.                     //结束
  62.                     mcrypt_generic_deinit($this->td);  
  63.                     mcrypt_module_close($this->td);
  64.                     return $decrypted;        
  65.                 }
  66.             }catch (Exception $e){
  67.                 echo $e->getMessage();
  68.             }
  69.         }
  70.         /**
  71.          * 将密文保存到文件中
  72.          */
  73.         public function savetofile(){
  74.             try {
  75.                 $fp=fopen($this->filepath,'w+');
  76.                 if (!$fp){
  77.                     throw new Exception("文件操作失败");
  78.                 }
  79.                 fwrite($fp,$this->encode);
  80.                 fclose($fp);
  81.             }catch (Exception $e){
  82.                 echo $e->getMessage();
  83.             }
  84.         }
  85.         /**
  86.          * 取得服务器的MAC地址
  87.          */
  88.         public function getmac($os_type){
  89.              switch ( strtolower($os_type) ){
  90.                       case "linux":
  91.                                 $this->forLinux();
  92.                                 break;
  93.                       case "solaris":
  94.                                 break;
  95.                       case "unix":
  96.                                  break;
  97.                        case "aix":
  98.                                  break;
  99.                        default:
  100.                                $this->forWindows();
  101.                                break;
  102.               }
  103.               $temp_array = array();
  104.               foreach( $this->return_array as $value ){
  105.                         if (preg_match("/[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f][:-]"."[0-9a-f][0-9a-f]/i",$value,$temp_array )){
  106.                             $mac_addr = $temp_array[0];
  107.                             break;
  108.                        }
  109.               }
  110.               unset($temp_array);
  111.               return $mac_addr;
  112.          }
  113.          /**
  114.           * windows服务器下执行ipconfig命令
  115.           */
  116.          public function forWindows(){
  117.               @exec("ipconfig /all", $this->return_array);
  118.               if ( $this->return_array )
  119.                        return $this->return_array;
  120.               else{
  121.                        $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
  122.                        if ( is_file($ipconfig) )
  123.                           @exec($ipconfig." /all", $this->return_array);
  124.                        else
  125.                           @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
  126.                        return $this->return_array;
  127.               }
  128.          }
  129.          /**
  130.           * Linux服务器下执行ifconfig命令
  131.           */
  132.          public function forLinux(){
  133.               @exec("ifconfig -a", $this->return_array);
  134.               return $this->return_array;
  135.          }
  136.     }
  137.     $code=new authCode();
  138.     //加密
  139.     $code->encode("~!@#$%^");
  140.     //解密
  141.     echo $code->decode("~!@#$%^");
  142. ?>

回复 "DES加密解密"

这儿你可以回复上面这条便签

captcha