Heim  >  Artikel  >  php教程  >  php 可逆加密的方法

php 可逆加密的方法

PHP中文网
PHP中文网Original
2016-05-25 17:11:221148Durchsuche

这篇文章主要介绍了php实现可逆加密的方法,借鉴了discuz的加密原理实现通过密钥进行可逆加密的功能,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了php实现可逆加密的方法。分享给大家供大家参考。具体如下:

这里介绍的可以逆转加密类,没有密钥很难破解。

PHP代码如下:

<?php
class encryptCalss
{
var $key=12;
function encode($txt){
for($i=0;$i<strlen($txt);$i++){
$txt[$i]=chr(ord($txt[$i])+$this->key);
}
return $txt=urlencode(base64_encode(urlencode($txt)));
}
function decode($txt){
$txt=urldecode(base64_decode($txt));
for($i=0;$i<strlen($txt);$i++){
$txt[$i]=chr(ord($txt[$i])-$this->key);
}
return $txt;
}
}
?>

discuz加密解密:

<?php
/**
 * 
 * @param string $string 原文或者密文
 * @param string $operation 操作(ENCODE | DECODE), 默认为 DECODE
 * @param string $key 密钥
 * @param int $expiry 密文有效期, 加密时候有效, 单位 秒,0 为永久有效
 * @return string 处理后的 原文或者 经过 base64_encode 处理后的密文
 * @example 
 *  $a = authcode(&#39;abc&#39;, &#39;ENCODE&#39;, &#39;key&#39;);
 *  $b = authcode($a, &#39;DECODE&#39;, &#39;key&#39;); // $b(abc)
 * 
 *  $a = authcode(&#39;abc&#39;, &#39;ENCODE&#39;, &#39;key&#39;, 3600);
 *  $b = authcode(&#39;abc&#39;, &#39;DECODE&#39;, &#39;key&#39;); // 在一个小时内,$b(abc),否则 $b 为空
 */
function authcode($string,$operation=&#39;DECODE&#39;,$key=&#39;&#39;,$expiry=0){
  $ckey_length=4;
  $key=md5($key ? $key:"kalvin.cn");
  $keya=md5(substr($key,0,16));
  $keyb=md5(substr($key,16,16));
  $keyc=$ckey_length ? ($operation==&#39;DECODE&#39; ? substr($string,0,$ckey_length):substr(md5(microtime()),
  -$ckey_length)):&#39;&#39;;
  $cryptkey=$keya.md5($keya.$keyc);
  $key_length=strlen($cryptkey);
  $string=$operation==&#39;DECODE&#39; ? base64_decode(substr($string,$ckey_length)):sprintf(&#39;%010d&#39;,$expiry ? 
  $expiry+time():0).substr(md5($string.$keyb),0,16).$string;
  $string_length=strlen($string);
  $result=&#39;&#39;;
  $box=range(0,255);
  $rndkey=array();
  for($i=0;$i<=255;$i++){
    $rndkey[$i]=ord($cryptkey[$i%$key_length]);
  }
  for($j=$i=0;$i<256;$i++){
    $j=($j+$box[$i]+$rndkey[$i])%256;
    $tmp=$box[$i];
    $box[$i]=$box[$j];
    $box[$j]=$tmp;
  }
  for($a=$j=$i=0;$i<$string_length;$i++){
    $a=($a+1)%256;
    $j=($j+$box[$a])%256;
    $tmp=$box[$a];
    $box[$a]=$box[$j];
    $box[$j]=$tmp;
    $result.=chr(ord($string[$i]) ^ ($box[($box[$a]+$box[$j])%256]));
  }
  if($operation==&#39;DECODE&#39;){
    if((substr($result,0,10)==0||substr($result,0,10)-time()>0)
    &&substr($result,10,16)==substr(md5(substr($result,26).$keyb),0,16)){
      returnsubstr($result,26);
    }else{
      return&#39;&#39;;
    }
  }else{
    return $keyc.str_replace(&#39;=&#39;,&#39;&#39;,base64_encode($result));
  }
}
?>

 以上就是php 可逆加密的方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn