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

 2025-01-15  阅读 323  评论 8  点赞 110

摘要:本文实例讲述了php实现的三个常用加密解密功能函数。分享给大家供大家参考,具体如下: 算法一: //加密函数 function lock_url($txt,$key='www.jb51.net') { $chars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789-=+"; $nh = rand(0,64); $ch = $char

本文实例讲述了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='www.jb51.net')
{
  $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 = '';
  $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 = 'www.jb51.net') 
{ 
  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.jb51.net') 
{ 
  $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>";
?>

运行结果:

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


//加密函数
function lock_url($txt,$key='www.jb51.net')
{
  $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 = '';
  $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='www.jb51.net')
{
  $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 = '';
  $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);

运行结果:

ps:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含aes、des、rc4等):
http://tools.jb51.net/password/txt_encode

md5在线加密工具:
http://tools.jb51.net/password/createmd5password

在线散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:
http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode

更多关于php相关内容感兴趣的读者可查看本站专题:《php加密方法总结》、《php编码与转码操作技巧汇总》、《php数学运算技巧总结》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php数据结构与算法教程》、《php程序设计算法总结》及《php正则表达式用法总结》

希望本文所述对大家php程序设计有所帮助。


标签:phpphp教程

评论列表:

  •   tg7187037109
     发布于 3天前回复该评论
  • 又学到了新知识!
  •   henbucuo
     发布于 2天前回复该评论
  • 写的很不错,学到了!
显示更多评论

发表评论:

管理员

承接各种程序开发,外贸网站代运营,外贸网站建设等项目
  • 内容2460
  • 积分67666
  • 金币86666

Copyright © 2024 LS'Blog-保定PHP程序员老宋个人博客 Inc. 保留所有权利。 Powered by LS'blog 3.0.3

页面耗时0.0295秒, 内存占用1.94 MB, 访问数据库27次

冀ICP备19034377号