DES加密解密的PHP類
<?php class authCode { public $ttl;//到期時間 時間格式:20120101(年月日) public $key_1;//密鑰1 public $key_2;//密鑰2 public $td; public $ks;//密鑰的長度 public $iv;//初始向量 public $salt;//鹽值(某個特定的字符串) public $encode;//加密后的信息 public $return_array = array(); // 返回帶有MAC地址的字串數組 public $mac_addr;//mac地址 public $filepath;//保存密文的文件路徑 public function __construct(){ //獲取物理地址 $this->mac_addr=$this->getmac(PHP_OS); $this->filepath="./licence.txt"; $this->ttl="20120619";//到期時間 $this->salt="~!@#$";//鹽值,用以提高密文的安全性 // echo "<pre>".print_r(mcrypt_list_algorithms ())."</pre>"; // echo "<pre>".print_r(mcrypt_list_modes())."</pre>"; } /*** 對明文信息進行加密 * @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);//設置初始向量的大小 $this->iv = mcrypt_create_iv($size, MCRYPT_RAND);//創建初始向量 $this->ks = mcrypt_enc_get_key_size($this->td);//返回所支持的最大的密鑰長度(以字節計算) $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); //結束處理 mcrypt_generic_deinit($this->td); //將密文保存到文件中 $this->savetofile(); } /** * 對密文進行解密 * @param $key 密鑰 */ public function decode($key) { try { if (!file_exists($this->filepath)){ throw new Exception("授權文件不存在"); }else{//如果授權文件存在的話,則讀取授權文件中的密文 $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); //解密后,可能會有后續的\0,需去掉 $decrypted=trim($decrypted) . "\n"; //結束 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(); } } /** * 取得服務器的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服務器下執行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服務器下執行ifconfig命令 */ public function forLinux(){ @exec("ifconfig -a", $this->return_array); return $this->return_array; } } $code=new authCode(); //加密 $code->encode("~!@#$%^"); //解密 echo $code->decode("~!@#$%^");
?></pre>
本文由用戶 jspet 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!