PHP實現加強版加密解密類執行個體_php技巧

來源:互聯網
上載者:User

本文執行個體講述了PHP實現加強版加密解密類。分享給大家供大家參考。具體如下:

<?phpclass Ender{  private $enkey;//加密解密用的密鑰  private $rep_char='#';  //替換加密後的base64字串中的=,因為=在有些場合是禁止使用的,  //這裡可以用一個允許的字元作為替換。  //構造參數是密鑰  public function __construct($key=''){    if(!$key){      $this->enkey=$key;    }  }  //設定密鑰http://blog.ddian.cn  public function set_key($key){    $this->enkey=$key;  }  private function keyED($txt,$encrypt_key)   {   $encrypt_key = md5($encrypt_key);   $ctr=0;   $tmp = "";   for ($i=0;$i<strlen($txt);$i++)   {   if ($ctr==strlen($encrypt_key)) $ctr=0;   $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);   $ctr++;   }   return $tmp;  }  //加密字串  public function encrypt($txt,$key='')   {  if(!$key){    $key=$this->enkey;  }  srand((double)microtime()*1000000);   $encrypt_key = md5(rand(0,32000));   $ctr=0;   $tmp = "";   for ($i=0;$i<strlen($txt);$i++)   {   if ($ctr==strlen($encrypt_key)) $ctr=0;   $tmp.= substr($encrypt_key,$ctr,1) .   (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));   $ctr++;   }  $r=base64_encode($this->keyED($tmp,$key));  $r=str_replace('=',$this->rep_char,$r);  return $r;   }  //解密字串  public function decrypt($txt,$key='')   {  $txt=str_replace($this->rep_char,'=',$txt);  $txt=base64_decode($txt);  if(!$key){      $key=$this->enkey;  }  $txt = $this->keyED($txt,$key);   $tmp = "";   for ($i=0;$i<strlen($txt);$i++)   {   $md5 = substr($txt,$i,1);   $i++;   $tmp.= (substr($txt,$i,1) ^ $md5);   }   return $tmp;   }}

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.