ホームページ >バックエンド開発 >PHPチュートリアル >php实现的三个常用加密解密功能函数示例

php实现的三个常用加密解密功能函数示例

PHPz
PHPz転載
2016-05-16 20:16:48979ブラウズ

这篇文章主要介绍了php实现的三个常用加密解密功能函数,涉及php针对字符串的遍历、截取、编码转换等相关操作技巧,需要的朋友可以参考下

本文实例讲述了php实现的三个常用加密解密功能函数。分享给大家供大家参考,具体如下:

算法一:

//加密函数
function lock_url($txt,$key='www.jb51.net')
{
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  $nh = rand(0,64);
  $ch = $chars[$nh];
  $mdKey = md5($key.$ch);
  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  $txt = base64_encode($txt);
  $tmp = '';
  $i=0;$j=0;$k = 0;
  for ($i=0; $i<strlen($txt); $i++) {
    $k = $k == strlen($mdKey) ? 0 : $k;
    $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
    $tmp .= $chars[$j];
  }
  return urlencode($ch.$tmp);
}
//解密函数
function unlock_url($txt,$key=&#39;www.jb51.net&#39;)
{
  $txt = urldecode($txt);
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  $ch = $txt[0];
  $nh = strpos($chars,$ch);
  $mdKey = md5($key.$ch);
  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  $txt = substr($txt,1);
  $tmp = &#39;&#39;;
  $i=0;$j=0; $k = 0;
  for ($i=0; $i<strlen($txt); $i++) {
    $k = $k == strlen($mdKey) ? 0 : $k;
    $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
    while ($j<0) $j+=64;
    $tmp .= $chars[$j];
  }
  return base64_decode($tmp);
}

用法:

$str="脚本之家";
$pwd = lock_url($str);
echo "加密之后:".$pwd."<br/>";
echo "解密还原:".unlock_url($pwd);

运行结果:

算法二:

<?php
function passport_encrypt($txt, $key = &#39;www.jb51.net&#39;) 
{ 
  srand((double)microtime() * 1000000); 
  $encrypt_key = md5(rand(0, 32000)); 
  $ctr = 0; 
  $tmp = &#39;&#39;; 
  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 = &#39;www.jb51.net&#39;) 
{ 
  $txt = passport_key(base64_decode(urldecode($txt)), $key); 
  $tmp = &#39;&#39;; 
  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 = &#39;&#39;; 
  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>";
?>

运行结果:

算法三(改进第一个加密之后的算法)

//加密函数
function lock_url($txt,$key=&#39;www.jb51.net&#39;)
{
  $txt = $txt.$key;
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  $nh = rand(0,64);
  $ch = $chars[$nh];
  $mdKey = md5($key.$ch);
  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  $txt = base64_encode($txt);
  $tmp = &#39;&#39;;
  $i=0;$j=0;$k = 0;
  for ($i=0; $i<strlen($txt); $i++) {
    $k = $k == strlen($mdKey) ? 0 : $k;
    $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
    $tmp .= $chars[$j];
  }
  return urlencode(base64_encode($ch.$tmp));
}
//解密函数
function unlock_url($txt,$key=&#39;www.jb51.net&#39;)
{
  $txt = base64_decode(urldecode($txt));
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  $ch = $txt[0];
  $nh = strpos($chars,$ch);
  $mdKey = md5($key.$ch);
  $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  $txt = substr($txt,1);
  $tmp = &#39;&#39;;
  $i=0;$j=0; $k = 0;
  for ($i=0; $i<strlen($txt); $i++) {
    $k = $k == strlen($mdKey) ? 0 : $k;
    $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
    while ($j<0) $j+=64;
    $tmp .= $chars[$j];
  }
  return trim(base64_decode($tmp),$key);
}

用法:

$str="脚本之家";
$pwd = lock_url($str);
echo "加密之后:".$pwd."<br/>";
echo "解密还原:".unlock_url($pwd);

运行结果:

更多相关教程请访问 php编程从入门到精通全套视频教程

声明:
この記事はjb51.netで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。