三个php加密解密算法
2016-06-01 17:08
435 查看
三个功能强大的php加密解密函数
//加密函数 function lock_url($txt,$key='www.fyunw.com') { $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.fyunw.com') { $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); } <?php function passport_encrypt($txt, $key = 'www.fyunw.com') { 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.fyunw.com') { $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.fyunw.com') { $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.fyunw.com') { $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); }
相关文章推荐
- php_pdo 事务处理
- 一个经典的PHP加密解密算法
- PHP--封装一个下载图片的方法
- 一组PHP可逆加密解密算法
- php安装composer是报错解决
- ubuntu服务器中安装设置ftp服务器
- Windows下搭建PHP开发环境-WEB服务器
- 关于初学TP的一点小小感悟
- PHP文件上传进度处理方法
- [转]日请求过亿的Web系统PHP7升级实践
- php引用赋值& 和传递赋值
- PHP基础笔记(01)
- PHP Warning: mkdir() [function.mkdir]: Permission denied in解决方法
- PHP菜鸟如何开始学习PHP语言
- PHP邮箱验证示例教程
- crontab执行php的问题
- 本地环境phpStorm10+XDebug配置和断点调试
- php处理emoji表情方法集合
- php缓存机制
- 考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传