中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

DES加密解密的PHP類(lèi)

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
<?php
    class authCode {
        public $ttl;//到期時(shí)間 時(shí)間格式:20120101(年月日)
        public $key_1;//密鑰1
        public $key_2;//密鑰2
        public $td;
        public $ks;//密鑰的長(zhǎng)度
        public $iv;//初始向量
        public $salt;//鹽值(某個(gè)特定的字符串)
        public $encode;//加密后的信息
        public $return_array = array(); // 返回帶有MAC地址的字串?dāng)?shù)組
        public $mac_addr;//mac地址
        public $filepath;//保存密文的文件路徑
        public function __construct(){
            //獲取物理地址
            $this->mac_addr=$this->getmac(PHP_OS);
            $this->filepath="./licence.txt";
            $this->ttl="20120619";//到期時(shí)間
            $this->salt="~!@#$";//鹽值,用以提高密文的安全性
//            echo "<pre>".print_r(mcrypt_list_algorithms ())."</pre>";
//            echo "<pre>".print_r(mcrypt_list_modes())."</pre>";
        }
        /**
         * 對(duì)明文信息進(jìn)行加密
         * @param $key 密鑰
         */
        public function encode($key) {
            $this->td = mcrypt_module_open(MCRYPT_DES,'','ecb',''); //使用MCRYPT_DES算法,ecb模式
            $size=mcrypt_enc_get_iv_size($this->td);//設(shè)置初始向量的大小
            $this->iv = mcrypt_create_iv($size, MCRYPT_RAND);//創(chuàng)建初始向量
            $this->ks = mcrypt_enc_get_key_size($this->td);//返回所支持的最大的密鑰長(zhǎng)度(以字節(jié)計(jì)算)
            $this->key_1 = substr(md5(md5($key).$this->salt),0,$this->ks);
            mcrypt_generic_init($this->td, $this->key_1, $this->iv); //初始處理
            //要保存到明文
            $con=$this->mac_addr.$this->ttl;
            //加密
            $this->encode = mcrypt_generic($this->td, $con);  
            //結(jié)束處理
            mcrypt_generic_deinit($this->td);
            //將密文保存到文件中
            $this->savetofile();
        }
        /**
         * 對(duì)密文進(jìn)行解密
         * @param $key 密鑰
         */
        public function decode($key) {
            try {
                if (!file_exists($this->filepath)){
                    throw new Exception("授權(quán)文件不存在");
                }else{//如果授權(quán)文件存在的話(huà),則讀取授權(quán)文件中的密文
                    $fp=fopen($this->filepath,'r');
                    $secret=fread($fp,filesize($this->filepath));
                    $this->key_2 = substr(md5(md5($key).$this->salt),0,$this->ks);
                    //初始解密處理
                    mcrypt_generic_init($this->td, $this->key_2, $this->iv);
                    //解密
                    $decrypted = mdecrypt_generic($this->td, $secret);
                    //解密后,可能會(huì)有后續(xù)的\0,需去掉  
                    $decrypted=trim($decrypted) . "\n";  
                    //結(jié)束
                    mcrypt_generic_deinit($this->td);  
                    mcrypt_module_close($this->td);
                    return $decrypted;       
                }
            }catch (Exception $e){
                echo $e->getMessage();
            }
        }
        /**
         * 將密文保存到文件中
         */
        public function savetofile(){
            try {
                $fp=fopen($this->filepath,'w+');
                if (!$fp){
                    throw new Exception("文件操作失敗");
                }
                fwrite($fp,$this->encode);
                fclose($fp);
            }catch (Exception $e){
                echo $e->getMessage();
            }
        }
        /**
         * 取得服務(wù)器的MAC地址
         */
        public function getmac($os_type){
             switch ( strtolower($os_type) ){
                      case "linux":
                                $this->forLinux();
                                break;
                      case "solaris":
                                break;
                      case "unix":
                                 break;
                       case "aix":
                                 break;
                       default:
                               $this->forWindows();
                               break;
              }
              $temp_array = array();
              foreach( $this->return_array as $value ){
                        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 )){
                            $mac_addr = $temp_array[0];
                            break;
                       }
              }
              unset($temp_array);
              return $mac_addr;
         }
         /**
          * windows服務(wù)器下執(zhí)行ipconfig命令
          */
         public function forWindows(){
              @exec("ipconfig /all", $this->return_array);
              if ( $this->return_array )
                       return $this->return_array;
              else{
                       $ipconfig = $_SERVER["WINDIR"]."\system32\ipconfig.exe";
                       if ( is_file($ipconfig) )
                          @exec($ipconfig." /all", $this->return_array);
                       else
                          @exec($_SERVER["WINDIR"]."\system\ipconfig.exe /all", $this->return_array);
                       return $this->return_array;
              }
         }
         /**
          * Linux服務(wù)器下執(zhí)行ifconfig命令
          */
         public function forLinux(){
              @exec("ifconfig -a", $this->return_array);
              return $this->return_array;
         }
    }
    $code=new authCode();
    //加密
    $code->encode("~!@#$%^");
    //解密
    echo $code->decode("~!@#$%^");
?>

標(biāo)簽: linux 安全 服務(wù)器

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:iOS開(kāi)發(fā)textField點(diǎn)擊背景空白隱藏收起鍵盤(pán)的N種方法

下一篇:iOS tableView表視圖設(shè)置背景圖片