PHP写的加密函数,支持私人密钥(详细介绍)


Posted in PHP onJune 09, 2013

在开发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">// 整理:https://3water.com </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 上一篇,下一篇文章实现代码与原理说明
May 09 PHP
php中JSON的使用与转换
Jan 14 PHP
laravel容器延迟加载以及auth扩展详解
Mar 02 PHP
使用PHP similar text计算两个字符串相似度
Nov 06 PHP
yii2.0实现创建简单widgets示例
Jul 18 PHP
PHP  Yii清理缓存的实现方法
Nov 10 PHP
PHP数据分析引擎计算余弦相似度算法示例
Aug 08 PHP
Laravel学习教程之路由模块
Aug 18 PHP
PHP中实现中文字串截取无乱码的解决方法
May 29 PHP
PHP PDOStatement::getColumnMeta讲解
Feb 01 PHP
PHP进阶学习之反射基本概念与用法分析
Jun 18 PHP
用php定义一个数组最简单的方法
Oct 04 PHP
PHP版 汉字转码的实现详解
Jun 09 #PHP
php批量上传的实现代码
Jun 09 #PHP
PHP转换IP地址到真实地址的方法详解
Jun 09 #PHP
linux环境apache多端口配置虚拟主机的方法深入介绍
Jun 09 #PHP
探讨PHP删除文件夹的三种方法
Jun 09 #PHP
如何用php获取文件名后缀
Jun 09 #PHP
深入php多态的实现详解
Jun 09 #PHP
You might like
php 将bmp图片转为jpg等其他任意格式的图片
2009/06/21 PHP
解决cPanel无法安装php5.2.17
2014/06/22 PHP
php打造智能化的柱状图程序,用于报表等
2015/06/19 PHP
tp5实现微信小程序多图片上传到服务器功能
2018/07/16 PHP
跨域表单提交状态的变相判断代码
2009/11/12 Javascript
关于div自适应高度/左右高度自适应一致的js代码
2013/03/22 Javascript
JavaScript前端图片加载管理器imagepool使用详解
2014/12/29 Javascript
javascript图片滑动效果实现
2021/01/28 Javascript
jQuery的文档处理程序详解
2016/05/10 Javascript
浅谈javascript中的constructor
2016/06/08 Javascript
js+css3实现旋转效果
2017/01/20 Javascript
vue.js指令和组件详细介绍及实例
2017/04/06 Javascript
ES6学习教程之模板字符串详解
2017/10/09 Javascript
使用js实现将后台传入的json数据放在前台显示
2018/08/06 Javascript
js实现图片上传到服务器和回显
2020/01/19 Javascript
基于canvasJS在PHP中制作动态图表
2020/05/30 Javascript
[44:51]2018DOTA2亚洲邀请赛 4.4 淘汰赛 VP vs Liquid 第二场
2018/04/05 DOTA
Python使用chardet判断字符编码
2015/05/09 Python
使用相同的Apache实例来运行Django和Media文件
2015/07/22 Python
python数据清洗系列之字符串处理详解
2017/02/12 Python
使用Python的package机制如何简化utils包设计详解
2017/12/11 Python
详谈Python3 操作系统与路径 模块(os / os.path / pathlib)
2018/04/26 Python
Python把对应格式的csv文件转换成字典类型存储脚本的方法
2019/02/12 Python
Python使用pandas和xlsxwriter读写xlsx文件的方法示例
2019/04/09 Python
通过PYTHON来实现图像分割详解
2019/06/26 Python
python下载库的步骤方法
2019/10/12 Python
使用Python刷淘宝喵币(低阶入门版)
2019/10/30 Python
Gloeilampgoedkoop荷兰:在线购买灯泡
2019/02/16 全球购物
Tomcat Mysql datasource数据源配置
2015/12/28 面试题
如果NULL和0作为空指针常数是等价的,那我到底该用哪一个
2014/09/16 面试题
销售经理工作职责范文
2013/12/03 职场文书
大一期末自我鉴定
2013/12/13 职场文书
视光学毕业生自荐书范文
2014/02/13 职场文书
个人剖析材料范文
2014/09/30 职场文书
Vue如何清空对象
2022/03/03 Vue.js
Python中time标准库的使用教程
2022/04/13 Python