三个php加密解密算法

三个功能强大的php加密解密函数

  1 //加密函数
  2 function lock_url($txt,$key='www.fyunw.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.fyunw.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 }
 39 
 40 
 41 
 42 <?php
 43 function passport_encrypt($txt, $key = 'www.fyunw.com') 
 44 { 
 45     srand((double)microtime() * 1000000); 
 46     $encrypt_key = md5(rand(0, 32000)); 
 47     $ctr = 0; 
 48     $tmp = ''; 
 49     for($i = 0;$i < strlen($txt); $i++) { 
 50     $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
 51     $tmp .= $encrypt_key[$ctr].($txt[$i] ^ $encrypt_key[$ctr++]); 
 52     } 
 53     return urlencode(base64_encode(passport_key($tmp, $key))); 
 54 } 
 55 
 56 function passport_decrypt($txt, $key = 'www.fyunw.com') 
 57 { 
 58     $txt = passport_key(base64_decode(urldecode($txt)), $key); 
 59     $tmp = ''; 
 60     for($i = 0;$i < strlen($txt); $i++) { 
 61     $md5 = $txt[$i]; 
 62     $tmp .= $txt[++$i] ^ $md5; 
 63     } 
 64     return $tmp; 
 65 } 
 66 
 67 function passport_key($txt, $encrypt_key) 
 68 { 
 69     $encrypt_key = md5($encrypt_key); 
 70     $ctr = 0; 
 71     $tmp = ''; 
 72     for($i = 0; $i < strlen($txt); $i++) { 
 73     $ctr = $ctr == strlen($encrypt_key) ? 0 : $ctr; 
 74     $tmp .= $txt[$i] ^ $encrypt_key[$ctr++]; 
 75     } 
 76     return $tmp; 
 77 } 
 78 ?>
 79  
 80 <?php 
 81 
 82 $txt = "1"; 
 83 $key = "testkey"; 
 84 $encrypt = passport_encrypt($txt,$key); 
 85 $decrypt = passport_decrypt($encrypt,$key); 
 86 
 87 echo $encrypt."<br>"; 
 88 echo $decrypt."<br>"; 
 89 ?> 
 90 
 91 //改进第一改加密之后的算法
 92 //加密函数
 93 function lock_url($txt,$key='www.fyunw.com')
 94 {
 95     $txt = $txt.$key;
 96     $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
 97     $nh = rand(0,64);
 98     $ch = $chars[$nh];
 99     $mdKey = md5($key.$ch);
100     $mdKey = substr($mdKey,$nh%8, $nh%8+7);
101     $txt = base64_encode($txt);
102     $tmp = '';
103     $i=0;$j=0;$k = 0;
104     for ($i=0; $i<strlen($txt); $i++) {
105         $k = $k == strlen($mdKey) ? 0 : $k;
106         $j = ($nh+strpos($chars,$txt[$i])+ord($mdKey[$k++]))%64;
107         $tmp .= $chars[$j];
108     }
109     return urlencode(base64_encode($ch.$tmp));
110 }
111 //解密函数
112 function unlock_url($txt,$key='www.fyunw.com')
113 {
114     $txt = base64_decode(urldecode($txt));
115     $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
116     $ch = $txt[0];
117     $nh = strpos($chars,$ch);
118     $mdKey = md5($key.$ch);
119     $mdKey = substr($mdKey,$nh%8, $nh%8+7);
120     $txt = substr($txt,1);
121     $tmp = '';
122     $i=0;$j=0; $k = 0;
123     for ($i=0; $i<strlen($txt); $i++) {
124         $k = $k == strlen($mdKey) ? 0 : $k;
125         $j = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]);
126         while ($j<0) $j+=64;
127         $tmp .= $chars[$j];
128     }
129     return trim(base64_decode($tmp),$key);
130 }

作者:zqifa

出处:https://www.l1mn.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z_qifa

此处弱弱求打赏~~万一有好心人

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值