php的一个简单加密解密代码

yipeiwu_com6年前PHP代码库

复制代码 代码如下:

class SysCrypt{
 private $crypt_key='//www.jb51.net';//密钥
 public function __construct($crypt_key){
  $this->crypt_key=$crypt_key;
 }
 public function 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 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('//www.jb51.net');
$text='yhm.1234@163.com';
$test1=$sc->encrypt($text);
echo '原文:',$text;
echo '<br />';
echo '密文:',$test1;
echo "<br/>";
echo '解密:',$sc->decrypt($test1);

输出结果类似:

原文:yhm.1234@163.com
密文:BS8DbFU6AioCNFFlVGZQMgRGBDUOYlEzBXoAZgo0Bjk=
解密:yhm.1234@163.com

相关文章

PHP 定界符 使用技巧

如果用传统的输出方法——按字符串输出的话,肯定要有大量的转义符来对字符串中的引号等特殊字符进行转义,以免出现语法错误。如果是一两处还可以容忍,但是要是一个完整的html文本或者是一个20...

PHP中date()日期函数有关参数整理

在页面的最前页加上 date_default_timezone_set(PRC); /*把时间调到北京时间,php5默认为格林威治标准时间*/ date () a: "am"或是"pm"...

php生成唯一数字id的方法汇总

关于生成唯一数字ID的问题,是不是需要使用rand生成一个随机数,然后去数据库查询是否有这个数呢?感觉这样的话有点费时间,有没有其他方法呢? 当然不是,其实有两种方法可以解决。 1....

深入php self与$this的详解

先谈parent与self:复制代码 代码如下:<?php/* * Created by YinYiNiao */ class A{ &nb...

PHP 用数组降低程序的时间复杂度

PHP 用数组降低程序的时间复杂度

而随着设备硬件配置的不断提升,对中小型应用程序来说,对算法的空间复杂度的要求也宽松了不少。不过,在当今 Web2.0 时代,对应用程序的时间复杂度却有了更高的要求。 什么是算法的时间复杂...