首页 编程设计PHP正文

PHP常用加密解密功能函数收集

云水 PHP 2020-07-28 17:07:59 1491 0 PHPphp

方法1:(尚未测试)

<?phpfunction passport_encrypt($txt, $key = 'www.h2sheji.com') { 
  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(passport_key($tmp, $key))); } function passport_decrypt($txt, $key = 'www.h2sheji.com') { 
  $txt = passport_key(base64_decode(urldecode($txt)), $key); 
  $tmp = ''; 
  for($i = 0;$i < strlen($txt); $i++) { 
  $md5 = $txt[$i]; 
  $tmp .= $txt[++$i] ^ $md5; 
  } 
  return $tmp; } 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
$txt = "1";
$key = "testkey";
$encrypt = passport_encrypt($txt,$key);
$decrypt = passport_decrypt($encrypt,$key);
echo $encrypt."<br>";
echo $decrypt."<br>";
?>

方法2:简单的对称加密

header("content-type:text/html;charset=utf-8");
/**
*@param $string目标字符串
*@param $key 加密key
*@return string 
*/
function encryption($string="",$key="cxphp"){
//str_split把字符串分割到数组中
$strArr = str_split(base64_encode($string));
$strcount = count($strArr);
foreach(str_split($key) as $key=>$value){
$key < $strcount && $strArr[$key].= $value;
return str_replace(array('=','+','1'), array('O0O0O', 'o000o', 'oo00o'), join($strArr));
}
}
/**
*@param $string 解析字符串
*@param $key 揭秘到key
*@return string
*/
function deciphering($string='',$skey='cxphp'){
$strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
$strCount = count($strArr);
foreach (str_split($skey) as $key => $value)
$key <= $strCount && @$strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
return base64_decode(join($strArr));
}
$a = encryption('这是我的一条测试数据');
var_dump($a);
$a = '6cLo000oZ5piv5oiR55qE5LiA5p2h5rWL6Ko000oV5pWw5o2u';
$b = deciphering($a);
var_dump($b);

方法3:

/*********************************************************************
函数名称:encrypt
函数作用:加密解密字符串
使用方法:
加密 :encrypt('str','E','qingdou');
解密 :encrypt('被加密过的字符串','D','qingdou');
参数说明:
$string   :需要加密解密的字符串
$operation:判断是加密还是解密:E:加密   D:解密
$key  :加密的钥匙(密匙);
*********************************************************************/
function encrypt($string,$operation,$key='')
{
$src  = array("/","+","=");
$dist = array("_a","_b","_c");
if($operation=='D'){$string  = str_replace($dist,$src,$string);}
$key=md5($key);
$key_length=strlen($key);
$string=$operation=='D'?base64_decode($string):substr(md5($string.$key),0,8).$string;
$string_length=strlen($string);
$rndkey=$box=array();
$result='';
for($i=0;$i<=255;$i++)
{
$rndkey[$i]=ord($key[$i%$key_length]);
$box[$i]=$i;
}
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=='D')
{
if(substr($result,0,8)==substr(md5(substr($result,8).$key),0,8))
{
return substr($result,8);
}
else
{
return'';
}
}
else
{
$rdate  = str_replace('=','',base64_encode($result));
$rdate  = str_replace($src,$dist,$rdate);
return $rdate;
}
}

用法:

echo '加密1:'.encrypt('待加密内容', 'E', 'H2sheji氢设计');
echo '解密1:'.encrypt('密文待解密', 'D', 'H2sheji氢设计');


版权声明

1.本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行测试。
2.本站资源仅供学习和交流使用,版权归资源原作者所有,请在下载后24小时之内自觉删除。
3.若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,与本站无关。
4.若内容涉及侵权或违法信息,请联系本站管理员进行下架处理,邮箱ganice520@163.com(本站不支持其他投诉反馈渠道,谢谢合作)

本文链接:http://apod.cc/index.php/post/439.html

发表评论

评论列表(0人评论 , 1491人围观)
☹还没有评论,来说两句吧...