PHP写的加密函数,支持私人密钥(详细介绍)
2015-01-24信息快讯网
本篇文章是对PHP写的加密函数且支持私人密钥进行了详细的分析介绍,需要的朋友参考下
在开发PHP系统时,会员部分往往是一个必不可少的模块,而密码的处理又是不得不面对的问题,PHP 的 Mcrypt 加密库又需要额外设置,很多人都是直接使用md5()函数加密,这个方法的确安全,但是因为md5是不可逆加密,无法还原密码,因此也有一些不便之处,本文介绍加密函数支持私钥,用起来还是不错的.代码如下:
PHP:
<ol><li class="li1"><div class="de1"> </div> </li> <li class="li1"><div class="de1"><span class="kw2"><?php</span> </div> </li> <li class="li1"><div class="de1"><span class="co1">// 说明:PHP 写的加密函数,支持私人密钥 </span> </div> </li> <li class="li1"><div class="de1"><span class="co1">// 整理:http://www.jb51.net </span> </div> </li> <li class="li2"><div class="de2"> </div> </li> <li class="li1"><div class="de1"><span class="kw2">function</span> keyED<span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$encrypt_key</span> <span class="br0">)</span> </div> </li> <li class="li1"><div class="de1"><span class="br0">{</span> </div> </li> <li class="li1"><div class="de1"> <span class="re0">$encrypt_key</span> = <span class="kw3">md5</span> <span class="br0">(</span> <span class="re0">$encrypt_key</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$ctr</span> =<span class="nu0">0</span> ; </div> </li> <li class="li2"><div class="de2"> <span class="re0">$tmp</span> = <span class="st0">""</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="kw1">for</span> <span class="br0">(</span> <span class="re0">$i</span> =<span class="nu0">0</span> ;<span class="re0">$i</span> <strlen<span class="br0">(</span> <span class="re0">$txt</span> <span class="br0">)</span> ;<span class="re0">$i</span> ++<span class="br0">)</span> </div> </li> <li class="li1"><div class="de1"> <span class="br0">{</span> </div> </li> <li class="li1"><div class="de1"> <span class="kw1">if</span> <span class="br0">(</span> <span class="re0">$ctr</span> ==<span class="kw3">strlen</span> <span class="br0">(</span> <span class="re0">$encrypt_key</span> <span class="br0">)</span> <span class="br0">)</span> <span class="re0">$ctr</span> =<span class="nu0">0</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$tmp</span> .= <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$i</span> ,<span class="nu0">1</span> <span class="br0">)</span> ^ <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$encrypt_key</span> ,<span class="re0">$ctr</span> ,<span class="nu0">1</span> <span class="br0">)</span> ; </div> </li> <li class="li2"><div class="de2"> <span class="re0">$ctr</span> ++; </div> </li> <li class="li1"><div class="de1"> <span class="br0">}</span> </div> </li> <li class="li1"><div class="de1"> <span class="kw1">return</span> <span class="re0">$tmp</span> ; </div> </li> <li class="li1"><div class="de1"><span class="br0">}</span> </div> </li> <li class="li1"><div class="de1"> </div> </li> <li class="li2"><div class="de2"><span class="kw2">function</span> encrypt<span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$key</span> <span class="br0">)</span> </div> </li> <li class="li1"><div class="de1"><span class="br0">{</span> </div> </li> <li class="li1"><div class="de1"> <span class="kw3">srand</span> <span class="br0">(</span> <span class="br0">(</span> double<span class="br0">)</span> <span class="kw3">microtime</span> <span class="br0">(</span> <span class="br0">)</span> *<span class="nu0">1000000</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$encrypt_key</span> = <span class="kw3">md5</span> <span class="br0">(</span> <span class="kw3">rand</span> <span class="br0">(</span> <span class="nu0">0</span> ,<span class="nu0">32000</span> <span class="br0">)</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$ctr</span> =<span class="nu0">0</span> ; </div> </li> <li class="li2"><div class="de2"> <span class="re0">$tmp</span> = <span class="st0">""</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="kw1">for</span> <span class="br0">(</span> <span class="re0">$i</span> =<span class="nu0">0</span> ;<span class="re0">$i</span> <strlen<span class="br0">(</span> <span class="re0">$txt</span> <span class="br0">)</span> ;<span class="re0">$i</span> ++<span class="br0">)</span> </div> </li> <li class="li1"><div class="de1"> <span class="br0">{</span> </div> </li> <li class="li1"><div class="de1"> <span class="kw1">if</span> <span class="br0">(</span> <span class="re0">$ctr</span> ==<span class="kw3">strlen</span> <span class="br0">(</span> <span class="re0">$encrypt_key</span> <span class="br0">)</span> <span class="br0">)</span> <span class="re0">$ctr</span> =<span class="nu0">0</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$tmp</span> .= <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$encrypt_key</span> ,<span class="re0">$ctr</span> ,<span class="nu0">1</span> <span class="br0">)</span> . <span class="br0">(</span> <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$i</span> ,<span class="nu0">1</span> <span class="br0">)</span> ^ <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$encrypt_key</span> ,<span class="re0">$ctr</span> ,<span class="nu0">1</span> <span class="br0">)</span> <span class="br0">)</span> ; </div> </li> <li class="li2"><div class="de2"> <span class="re0">$ctr</span> ++; </div> </li> <li class="li1"><div class="de1"> <span class="br0">}</span> </div> </li> <li class="li1"><div class="de1"> <span class="kw1">return</span> keyED<span class="br0">(</span> <span class="re0">$tmp</span> ,<span class="re0">$key</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"><span class="br0">}</span> </div> </li> <li class="li1"><div class="de1"> </div> </li> <li class="li2"><div class="de2"><span class="kw2">function</span> decrypt<span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$key</span> <span class="br0">)</span> </div> </li> <li class="li1"><div class="de1"><span class="br0">{</span> </div> </li> <li class="li1"><div class="de1"> <span class="re0">$txt</span> = keyED<span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$key</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$tmp</span> = <span class="st0">""</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="kw1">for</span> <span class="br0">(</span> <span class="re0">$i</span> =<span class="nu0">0</span> ;<span class="re0">$i</span> <strlen<span class="br0">(</span> <span class="re0">$txt</span> <span class="br0">)</span> ;<span class="re0">$i</span> ++<span class="br0">)</span> </div> </li> <li class="li2"><div class="de2"> <span class="br0">{</span> </div> </li> <li class="li1"><div class="de1"> <span class="re0">$md5</span> = <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$i</span> ,<span class="nu0">1</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$i</span> ++; </div> </li> <li class="li1"><div class="de1"> <span class="re0">$tmp</span> .= <span class="br0">(</span> <span class="kw3">substr</span> <span class="br0">(</span> <span class="re0">$txt</span> ,<span class="re0">$i</span> ,<span class="nu0">1</span> <span class="br0">)</span> ^ <span class="re0">$md5</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> <span class="br0">}</span> </div> </li> <li class="li2"><div class="de2"> <span class="kw1">return</span> <span class="re0">$tmp</span> ; </div> </li> <li class="li1"><div class="de1"><span class="br0">}</span> </div> </li> <li class="li1"><div class="de1"> </div> </li> <li class="li1"><div class="de1"><span class="re0">$key</span> = <span class="st0">"www.yitu.org"</span> ; </div> </li> <li class="li1"><div class="de1"><span class="re0">$string</span> = <span class="st0">"我是加密字符"</span> ; </div> </li> <li class="li2"><div class="de2"> </div> </li> <li class="li1"><div class="de1"><span class="co1">// encrypt $string, and store it in $enc_text </span> </div> </li> <li class="li1"><div class="de1"><span class="re0">$enc_text</span> = encrypt<span class="br0">(</span> <span class="re0">$string</span> ,<span class="re0">$key</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> </div> </li> <li class="li1"><div class="de1"><span class="co1">// decrypt the encrypted text $enc_text, and store it in $dec_text </span> </div> </li> <li class="li2"><div class="de2"><span class="re0">$dec_text</span> = decrypt<span class="br0">(</span> <span class="re0">$enc_text</span> ,<span class="re0">$key</span> <span class="br0">)</span> ; </div> </li> <li class="li1"><div class="de1"> </div> </li> <li class="li1"><div class="de1"><span class="kw3">print</span> <span class="st0">"加密的 text : $enc_text <Br> "</span> ; </div> </li> <li class="li1"><div class="de1"><span class="kw3">print</span> <span class="st0">"解密的 text : $dec_text <Br> "</span> ; </div> </li> <li class="li1"><div class="de1"><span class="kw2">?></span> </div> </li> <li class="li2"><div class="de2"> </div> </li> </ol>
每一次加密后的结果是不一样的,大大加强了密码的安全性.
浅谈PHP变量作用域以及地址引用问题
一个好用的PHP验证码类实例分享
PHP连接SQLServer2005方法及代码
php截取中文字符串不乱码的方法
php输入流php://input使用示例(php发送图片流到服务器)
php使用strtotime和date函数判断日期是否有效代码分享
PHP eval函数使用介绍
php函数指定默认值方法的小例子
PHP函数microtime()用法与说明
php指定函数参数默认值示例代码
PHP加密扩展库Mcrypt安装和实例
php加密解密函数authcode的用法详细解析
PHP加密函数 Javascript/Js 解密函数
php使用异或实现的加密解密实例
php实现MD5加密16位(不要默认的32位)
解决ajax+php中文乱码的方法详解
PHP版 汉字转码的实现详解
php批量上传的实现代码
PHP转换IP地址到真实地址的方法详解
探讨PHP删除文件夹的三种方法
如何用php获取文件名后缀
解析PHP自带的进位制之间的转换函数
PHP rawurlencode与urlencode函数的深入分析
如何利用php array_multisort函数 对数据库结果进行复杂排序
PHP stripos()函数及注意事项的分析
深入php常用函数的使用汇总
PHP使用DES进行加密与解密的方法详解
一个简洁的PHP可逆加密函数(分享)
怎样给PHP源代码加密?PHP二进制加密与解密的解决办法
PHP可逆加密/解密函数分享
php中3des加密代码(完全与.net中的兼容)
php 的加密函数 md5,crypt,base64_encode 等使用介绍