当前位置:Gxlcms > PHP教程 > php实现的三个常用加密解密功能函数示例

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

时间:2021-07-01 10:21:17 帮助过:49人阅读

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

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

算法一:

  1. //加密函数
  2. function lock_url($txt,$key='www.gxlcms.com')
  3. {
  4. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  5. $nh = rand(0,64);
  6. $ch = $chars[$nh];
  7. $mdKey = md5($key.$ch);
  8. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  9. $txt = base64_encode($txt);
  10. $tmp = '';
  11. $i=0;$j=0;$k = 0;
  12. for ($i=0; $i<strlen($txt); $i++) {
  13. $k = $k == strlen($mdKey) ? 0 : $k;
  14. $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
  15. $tmp .= $chars[$j];
  16. }
  17. return urlencode($ch.$tmp);
  18. }
  19. //解密函数
  20. function unlock_url($txt,$key='www.gxlcms.com')
  21. {
  22. $txt = urldecode($txt);
  23. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  24. $ch = $txt[0];
  25. $nh = strpos($chars,$ch);
  26. $mdKey = md5($key.$ch);
  27. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  28. $txt = substr($txt,1);
  29. $tmp = '';
  30. $i=0;$j=0; $k = 0;
  31. for ($i=0; $i<strlen($txt); $i++) {
  32. $k = $k == strlen($mdKey) ? 0 : $k;
  33. $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
  34. while ($j<0) $j+=64;
  35. $tmp .= $chars[$j];
  36. }
  37. return base64_decode($tmp);
  38. }

用法:

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

运行结果:

算法二:

  1. <?php
  2. function passport_encrypt($txt, $key = 'www.gxlcms.com')
  3. {
  4. srand((double)microtime() * 1000000);
  5. $encrypt_key = md5(rand(0, 32000));
  6. $ctr = 0;
  7. $tmp = '';
  8. for($i = 0;$i < strlen($txt); $i++) {
  9. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  10. $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
  11. }
  12. return urlencode(base64_encode(passport_key($tmp, $key)));
  13. }
  14. function passport_decrypt($txt, $key = 'www.gxlcms.com')
  15. {
  16. $txt = passport_key(base64_decode(urldecode($txt)), $key);
  17. $tmp = '';
  18. for($i = 0;$i < strlen($txt); $i++) {
  19. $md5 = $txt[$i];
  20. $tmp .= $txt[++$i] ^ $md5;
  21. }
  22. return $tmp;
  23. }
  24. function passport_key($txt, $encrypt_key)
  25. {
  26. $encrypt_key = md5($encrypt_key);
  27. $ctr = 0;
  28. $tmp = '';
  29. for($i = 0; $i < strlen($txt); $i++) {
  30. $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  31. $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  32. }
  33. return $tmp;
  34. }
  35. ?>

用法:

  1. <?php
  2. $txt = "1";
  3. $key = "testkey";
  4. $encrypt = passport_encrypt($txt,$key);
  5. $decrypt = passport_decrypt($encrypt,$key);
  6. echo $encrypt."<br>";
  7. echo $decrypt."<br>";
  8. ?>

运行结果:

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

  1. //加密函数
  2. function lock_url($txt,$key='www.gxlcms.com')
  3. {
  4. $txt = $txt.$key;
  5. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  6. $nh = rand(0,64);
  7. $ch = $chars[$nh];
  8. $mdKey = md5($key.$ch);
  9. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  10. $txt = base64_encode($txt);
  11. $tmp = '';
  12. $i=0;$j=0;$k = 0;
  13. for ($i=0; $i<strlen($txt); $i++) {
  14. $k = $k == strlen($mdKey) ? 0 : $k;
  15. $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
  16. $tmp .= $chars[$j];
  17. }
  18. return urlencode(base64_encode($ch.$tmp));
  19. }
  20. //解密函数
  21. function unlock_url($txt,$key='www.gxlcms.com')
  22. {
  23. $txt = base64_decode(urldecode($txt));
  24. $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
  25. $ch = $txt[0];
  26. $nh = strpos($chars,$ch);
  27. $mdKey = md5($key.$ch);
  28. $mdKey = substr($mdKey,$nh%8, $nh%8+7);
  29. $txt = substr($txt,1);
  30. $tmp = '';
  31. $i=0;$j=0; $k = 0;
  32. for ($i=0; $i<strlen($txt); $i++) {
  33. $k = $k == strlen($mdKey) ? 0 : $k;
  34. $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
  35. while ($j<0) $j+=64;
  36. $tmp .= $chars[$j];
  37. }
  38. return trim(base64_decode($tmp),$key);
  39. }

用法:

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

运行结果:

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

人气教程排行