PHP實作的加密解密處理類

PHPz
發布: 2021-05-31 16:22:22
轉載
3258 人瀏覽過

本篇文章為大家介紹php實作加密解密處理類別的方法,結合實例形式分析了php加密解密類別的定義與使用技巧。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有幫助。

PHP實作的加密解密處理類

php實現的加密解密處理類

<?php
class SysCrypt {
 private $crypt_key;
 // 构造函数
 public function __construct($crypt_key) {
  $this -> crypt_key = $crypt_key;
 }
 public function php_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 base64_encode(self::__key($tmp,$this -> crypt_key));
 }
 public function php_decrypt($txt) {
  $txt = self::__key(base64_decode($txt),$this -> crypt_key);
  $tmp = '';
  for($i = 0;$i < strlen($txt); $i++) {
   $md5 = $txt[$i];
   $tmp .= $txt[++$i] ^ $md5;
  }
  return $tmp;
 }
 private function __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;
 }
 public function __destruct() {
  $this -> crypt_key = null;
 }
 }
 //执行代码
 $sc = new SysCrypt('phpwms');
 $text = 'abc10000008910000000990099hh'; //需要加密的数据
 print($sc -> php_encrypt($text)); //加密
 print('<br>');
 print($sc -> php_decrypt($sc -> php_encrypt($text))); //解密
?>
登入後複製

推薦學習:《php影片教學

相關標籤:
來源:jb51.net
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!