微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

算法一:

rush:PHP;"> //加密函数 function lock_url($txt,$key='www.jb51.cc') { $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函数 function unlock_url($txt,$key='www.jb51.cc') { $txt = urldecode($txt); $chars = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; $ch = $txt[0]; $nh = strpos($chars,$ch); $mdKey = md5($key.$ch); $mdKey = substr($mdKey,$nh%8+7); $txt = substr($txt,1); $tmp = ''; $i=0;$j=0; $k = 0; for ($i=0; $i

用法

rush:PHP;"> $str="编程之家"; $pwd = lock_url($str); echo "加密之后:".$pwd."
"; echo "解密还原:".unlock_url($pwd);

运行结果:

算法二:

rush:PHP;"> PHP function passport_encrypt($txt,$key = 'www.jb51.cc') { 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.cc') { $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; } ?>

用法

rush:PHP;"> PHP $txt = "1"; $key = "testkey"; $encrypt = passport_encrypt($txt,$key); $decrypt = passport_decrypt($encrypt,$key); echo $encrypt."
"; echo $decrypt."
"; ?>

运行结果:

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

rush:PHP;"> //加密函数 function lock_url($txt,$key='www.jb51.cc') { $txt = $txt.$key; $chars = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; $nh = rand(0,$txt[$i])+ord($mdKey[$k++]))%64; $tmp .= $chars[$j]; } return urlencode(base64_encode($ch.$tmp)); } //解密函数 function unlock_url($txt,$key='www.jb51.cc') { $txt = base64_decode(urldecode($txt)); $chars = "ABCDEFGHIJKLMnopQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+"; $ch = $txt[0]; $nh = strpos($chars,$txt[$i])-$nh - ord($mdKey[$k++]); while ($j<0) $j+=64; $tmp .= $chars[$j]; } return trim(base64_decode($tmp),$key); }

用法

rush:PHP;"> $str="编程之家"; $pwd = lock_url($str); echo "加密之后:".$pwd."
"; echo "解密还原:".unlock_url($pwd);

运行结果:

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

文字在线加密解密工具(包含AES、DES、RC4等):

MD5在线加密工具:

在线散列/哈希算法加密工具:

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《PHP正则表达式用法总结》

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐