PHP技术论坛

搜索
查看: 1536|回复: 0
打印 上一主题 下一主题

PHP各种加密解密函数

[复制链接]

83

主题

88

帖子

411

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
411
跳转到指定楼层
楼主
发表于 2016-8-15 10:15:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

普通hash函数如md5、sha1、base64等都是不可逆函数。虽然我们利用php可以利用这些函数写出可逆函数来。但是跨语言时这类可逆函数非常难搞定。所以这时尽量使用AES DES RC4 Rabbit TripleDes这些方法。


包含超时的加密解密函数
  1. /**
  2.      * 加密
  3.      * @param string $string         要加密或解密的字符串
  4.      * @param string $operation 加密 ''  解密 DECODE
  5.      * @param string $key                密钥,加密解密时保持一致
  6.      * @param int    $expiry        有效时长,单位:秒
  7.      * @return string
  8.      */
  9.      function encrypt_code($string, $expiry = 0, $key = 'abc12345') {
  10.                 $ckey_length = 7;
  11.                 $key = md5($key ? $key : UC_KEY); //加密解密时这个是不变的
  12.                 $keya = md5(substr($key, 0, 16)); //加密解密时这个是不变的
  13.                 $keyb = md5(substr($key, 16, 16)); //加密解密时这个是不变的
  14.                 $keyc = $ckey_length ?  substr(md5(microtime()), -$ckey_length) : '';
  15.                 $cryptkey = $keya . md5($keya . $keyc); //64
  16.                 $key_length = strlen($cryptkey); //64

  17.                 $string =sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
  18.                 $string_length = strlen($string);

  19.                 $result = '';
  20.                 $box = range(0, 255);

  21.                 $rndkey = array();
  22.                 for ($i = 0; $i <= 255; $i++) { //字母表 64位后重复 数列 范围为48~122
  23.                         $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  24.                 }

  25.                 for ($j = $i = 0; $i < 256; $i++) { //这里是一个打乱算法
  26.                         $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  27.                         $tmp = $box[$i];
  28.                         $box[$i] = $box[$j];
  29.                         $box[$j] = $tmp;
  30.                 }
  31.                 for ($a = $j = $i = 0; $i < $string_length; $i++) {
  32.                         $result .= chr(ord($string[$i]) ^ ($box[$i]));
  33.                
  34.                 }
  35.              $str =  $keyc . str_replace('=', '', urlsafe_b64encode($result));       
  36.                  //  $str =htmlentities($str, ENT_QUOTES, "UTF-8"); // curl 访问出错
  37.                    return $str ;
  38.         }
  39.         
  40.         
  41.     /**
  42.      * 解密
  43.      * @param string $string         要加密或解密的字符串
  44.      * @param string $operation 加密 ''  解密 DECODE
  45.      * @param string $key                密钥,加密解密时保持一致
  46.      * @param int    $expiry        有效时长,单位:秒
  47.      * @return string
  48.      */
  49.       function encrypt_decode($string, $expiry = 0,$key = 'abc12345') {  
  50.                              //  $string = html_entity_decode($string, ENT_QUOTES, "UTF-8") ; //curl 访问出错
  51.                 $ckey_length = 7;
  52.                 $key = md5($key ? $key : UC_KEY); //加密解密时这个是不变的
  53.                 $keya = md5(substr($key, 0, 16)); //加密解密时这个是不变的
  54.                 $keyb = md5(substr($key, 16, 16)); //加密解密时这个是不变的
  55.               
  56.                 $keyc = $ckey_length ?  substr($string, 0, $ckey_length)   : '';

  57.                 $cryptkey = $keya . md5($keya . $keyc); //64
  58.                 $key_length = strlen($cryptkey); //64
  59.                 $string = urlsafe_b64decode(substr($string, $ckey_length)) ;
  60.                 $string_length = strlen($string);
  61.                 $result = '';
  62.                 $box = range(0, 255);

  63.                 $rndkey = array();
  64.                 for ($i = 0; $i <= 255; $i++) { //字母表 64位后重复 数列 范围为48~122
  65.                         $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  66.                 }
  67.                 for ($j = $i = 0; $i < 256; $i++) { //这里是一个打乱算法
  68.                         $j = ($j + $box[$i] + $rndkey[$i]) % 256;

  69.                         $tmp = $box[$i];
  70.                         $box[$i] = $box[$j];
  71.                         $box[$j] = $tmp;
  72.                 }
  73.                 for ($a = $j = $i = 0; $i < $string_length; $i++) {
  74.                         $result .= chr(ord($string[$i]) ^ ($box[$i]));
  75.                 }
  76.            if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
  77.                         return substr($result, 26);
  78.                 } else {
  79.                         return false;
  80.                 }
  81.                
  82.         }
复制代码
最简单的往往是最好用的。

URL加密解密函数

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

  1. <?php
  2. function passport_encrypt($txt, $key = 'www.zhuoyuexiazai.com') {
  3.         srand((double)microtime() * 1000000);
  4.         $encrypt_key = md5(rand(0, 32000));
  5.         $ctr = 0;
  6.         $tmp = '';
  7.         for($i = 0;$i < strlen($txt); $i++) {
  8.         $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  9.         $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]);
  10.         }
  11.         return urlencode(base64_encode(passport_key($tmp, $key)));
  12. }

  13. function passport_decrypt($txt, $key = 'www.zhuoyuexiazai.com') {
  14.         $txt = passport_key(base64_decode(urldecode($txt)), $key);
  15.         $tmp = '';
  16.         for($i = 0;$i < strlen($txt); $i++) {
  17.         $md5 = $txt[$i];
  18.         $tmp .= $txt[++$i] ^ $md5;
  19.         }
  20.         return $tmp;
  21. }

  22. function passport_key($txt, $encrypt_key) {
  23.         $encrypt_key = md5($encrypt_key);
  24.         $ctr = 0;
  25.         $tmp = '';
  26.         for($i = 0; $i < strlen($txt); $i++) {
  27.         $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr;
  28.         $tmp .= $txt[$i] ^ $encrypt_key[$ctr++];
  29.         }
  30.         return $tmp;
  31. }
  32. ?>
复制代码
测试方法:


  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. ?>
复制代码
SHA1的可逆加密解密函数:

  1. <?php
  2. $string = "Helloworld";
  3. echo $str1 = dencrypt($string, true, "www.miaohr.com");
  4. echo $str2 = dencrypt($str1, false, "www.miaohr.com");

  5. function dencrypt($string, $isEncrypt = true, $key = KEY_SPACE) {
  6. if (!isset($string{0}) || !isset($key{0})) {
  7. return false;
  8. }

  9. $dynKey = $isEncrypt ? hash('sha1', microtime(true)) : substr($string, 0, 40);
  10. $fixedKey = hash('sha1', $key);

  11. $dynKeyPart1 = substr($dynKey, 0, 20);
  12. $dynKeyPart2 = substr($dynKey, 20);
  13. $fixedKeyPart1 = substr($fixedKey, 0, 20);
  14. $fixedKeyPart2 = substr($fixedKey, 20);
  15. $key = hash('sha1', $dynKeyPart1 . $fixedKeyPart1 . $dynKeyPart2 . $fixedKeyPart2);

  16. $string = $isEncrypt ? $fixedKeyPart1 . $string . $dynKeyPart2 : (isset($string{339}) ? gzuncompress(base64_decode(substr($string, 40))) : base64_decode(substr($string, 40)));

  17. $n = 0;
  18. $result = '';
  19. $len = strlen($string);

  20. for ($n = 0; $n < $len; $n++) {
  21. $result .= chr(ord($string{$n}) ^ ord($key{$n % 40}));
  22. }
  23. return $isEncrypt ? $dynKey . str_replace('=', '', base64_encode($n > 299 ? gzcompress($result) : $result)) : substr($result, 20, -20);
  24. }

  25. ?>
复制代码
DES的加密解密函数:

  1. <?php

  2. $input ='http://mlaan2.home.xs4all.nl/ispack/isetup-5.5.3.exe';
  3. /**
  4. *加密函数
  5. *$input 要被加密的字符串
  6. *$key 密钥
  7. */

  8. $key = randomkeys(8);//生成随机密匙
  9. function do_mencrypt($input, $key)
  10. {
  11.         $input = base64_encode(trim($input));
  12.         //$key = substr(md5($key), 0, 4);
  13.         $td = mcrypt_module_open('des', '', 'ecb', '');
  14.         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  15.         mcrypt_generic_init($td, $key, $iv);
  16.         $encrypted_data = mcrypt_generic($td, $input);
  17.         mcrypt_generic_deinit($td);
  18.         mcrypt_module_close($td);
  19.         return trim(base64_encode($encrypted_data));
  20. }
  21. print_r(do_mencrypt($input, $key));
  22. echo "<br/>";
  23. /**
  24. *解密函数
  25. *$input 要被解密的字符串
  26. *$key 密钥
  27. */
  28. $input1 = do_mencrypt($input, $key);
  29. function do_mdecrypt($input1, $key)
  30. {
  31.         $input1 = base64_decode(trim($input1));
  32.         $td = mcrypt_module_open('des', '', 'ecb', '');
  33.         //$key = substr(md5($key), 0, 4);
  34.         $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
  35.         mcrypt_generic_init($td, $key, $iv);
  36.         $decrypted_data = mdecrypt_generic($td, $input1);
  37.         mcrypt_generic_deinit($td);
  38.         mcrypt_module_close($td);
  39.         return trim(base64_decode($decrypted_data));
  40. }
  41. print_r(do_mdecrypt($input1, $key));
  42.      
  43. #2.rand key: "CWSTOAYD":生成随机密匙,统一用字母或者数字,长度为8.   
  44. function randomkeys($length)
  45. {
  46.    $pattern = '1234567890';
  47.     for($i=0;$i<$length;$i++)
  48.     {
  49.         @$key .= $pattern{rand(0,9)};    //生成php随机数
  50.     }
  51.     return $key;
  52. }
  53. ?>
复制代码



相关帖子

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|PHP  

GMT+8, 2024-4-29 18:05 , Processed in 0.063905 second(s), 29 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表