ホームページ  >  記事  >  バックエンド開発  >  PHP暗号化クラス

PHP暗号化クラス

不言
不言オリジナル
2018-05-04 09:14:531263ブラウズ

この記事の主な内容は、特定の参考値を持つ PHP 暗号化クラスを紹介することです。必要な友達はそれを参照してください

<?php
namespace app\index\controller;class Crypt{
    private $key = "";  
    public function __construct($key = null)    
    {
            if (!is_null($key)) {
                        $this->key = $key;        
                        }    
     }    
     //加密函数    
     public function passport_encrypt($txt)
     {
             srand((double)microtime() * 1000000);        
             $encrypt_key = md5(rand(0, 32000));        
             $ctr = 0;        
             $tmp = '';        
             for ($i = 0; $i < strlen($txt); $i++) {
                         $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;            
                         $tmp .= $encrypt_key[$ctr] . ($txt[$i] ^ $encrypt_key[$ctr++]);        
                         }        
                         return urlencode(base64_encode($this->passport_key($tmp, $this->key)));    
        }    
        //解密函数    
        public function passport_decrypt($txt)    
        {        
        $txt = $this->passport_key(base64_decode(urldecode($txt)), $this->key);        
        $tmp = '';        
        for ($i = 0; $i < strlen($txt); $i++) {
                    $md5 = $txt[$i];            
                    $tmp .= $txt[++$i] ^ $md5;        
                    }        
                    return $tmp;    
       }    
       private function passport_key($txt, $encrypt_key)    
       {        
       $encrypt_key = md5($encrypt_key);        
       $ctr = 0;        
       $tmp = '';        
       for ($i = 0; $i < strlen($txt); $i++) {
                   $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;            
                   $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];        
                   }        
                   return $tmp;    
                   }
           }


以上がPHP暗号化クラスの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。