PHP写的加密函数,支持私人密钥(详细介绍)_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>

每一次加密后的结果是不一样的,大大加强了密码的安全性.

时间: 2024-10-30 08:34:16

PHP写的加密函数,支持私人密钥(详细介绍)_php技巧的相关文章

PHP 匿名函数与注意事项详细介绍_php技巧

PHP 匿名函数与注意事项 PHP5.2 以前:autoload, PDO 和 MySQLi, 类型约束 PHP5.2:JSON 支持 PHP5.3:弃用的功能,匿名函数,新增魔术方法,命名空间,后期静态绑定,Heredoc 和 Nowdoc, const, 三元运算符,Phar PHP5.4:Short Open Tag, 数组简写形式,Traits, 内置 Web 服务器,细节修改 PHP5.5:yield, list() 用于 foreach, 细节修改 PHP5.6: 常量增强,可变函数

解析php curl_setopt 函数的相关应用及介绍_php技巧

一.要想使用curl_setopt 这个函数必须在服务器里边进行编译curl这个组件,怎么安装编译这个组件请具体到google搜索二.curl_setopt的php帮助文档的解释bool curl_setopt (int ch, string option, mixed value)curl_setopt()函数将为一个CURL会话设置选项.option参数是你想要的设置,value是这个选项给定的值.下列选项的值将被作为长整形使用(在option参数中指定): CURLOPT_INFILESI

PHP文件锁函数flock()详细介绍_php技巧

文件操作系统是在网络环境下完成的,可能有多个客户端用户在同一个时刻对服务器上的同一个文件访问.当这种并发访问产生时,很可能会破坏文件中.例如一个用户正在向文件中写入数据,当还没有写完时,其他用户在这一时刻也向这个文件中写数据,就会造成数据写入混乱.还有,当用户没有将数据写完时,其他用户就去获取这个文件中的内容,也会得到残缺的数据. 在PHP中提供了flock()函数,可以对文件使用锁定机制(锁定或释放文件).当一个进程在访问文件时加上锁,其他进程要想对该文件进行访问,则必须等到锁定被释放以后.这

PHP函数spl_autoload_register()用法和__autoload()介绍_php技巧

__autoload()的用法就不再说了,以前已经在我的WEB开发笔记中说过.PHP __autoload函数(自动载入类文件)的使用方法, 原文地址:http://www.jb51.net/article/29625.htm . 说一说,spl_autoload_register()的用法吧,很简单,可以这样理解,就是声明一个自定义__autoload(),你可以是A函数,也可以是B函数,想怎么样就怎么样,函数体的写法,当然要和__autoload()一样就OK 了. 当PHP找不到类文件会调

php无限分类且支持输出树状图的详细介绍_php技巧

复制代码 代码如下: <?php/*** 通用的树型类,可以生成任何树型结构*/class tree{    /**    * 生成树型结构所需要的2维数组    * @var array    */    var $arr = array();     /**    * 生成树型结构所需修饰符号,可以换成图片    * @var array    */    var $icon = array('│','├','└');     /**    * @access private    */  

php 的加密函数 md5,crypt,base64_encode 等使用介绍_php技巧

不可逆的加密函数为:md5().crypt() md5() 用来计算 MD5 哈稀.语法为:string md5(string str); crypt() 将字符串用 UNIX 的标准加密 DES 模块加密.这是单向的加密函数,无法解密.欲比对字符串,将已加密的字符串的头二个字符放在 salt 的参数中,再比对加密后的字符串.语法为:string crypt(string str, string [salt]); 可逆转的加密为:base64_encode().urlencode() 相对应的解

mcrypt启用 加密以及解密过程详细解析_php技巧

Mcrypt扩展库可以实现加密解密功能,就是既能将明文加密,也可以密文还原. 1.PHP加密扩展库Mcrypt安装在标准的PHP安装过程中并没有把Mrcypt安装上,但PHP的主目录下包含了libmcrypt.dll和libmhash.dll文件 (libmhash.dll是Mhash扩展库,这里可以一起装上).首先,将这两个文件复制到系统目录windows\system32下,然后在 PHP.ini文件中按Ctrl+F快捷键跳出查找框,并找到:extension=php-mcrypt.dll和

PHP实现加密的几种方式介绍_php技巧

PHP中的加密方式有如下几种 1. MD5加密 string md5 ( string $str [, bool $raw_output = false ] ) 参数 str  --  原始字符串. raw_output  --  如果可选的 raw_output 被设置为 TRUE,那么 MD5 报文摘要将以16字节长度的原始二进制格式返回. 这是一种不可逆加密,执行如下的代码 $password = '123456'; echo md5($password); 得到结果是e10adc3949

PHP 函数执行效率的小比较_php技巧

就是把原来的数组中的数都"拆"成"单"位的. 下面是自己写的一个函数: 复制代码 代码如下: function splitStrToArray_mine($array) { $new_array = array(); foreach($array as $value) { $value = (string)$value; $len = strlen($value); for($i = 0; $i < $len; $i ++){ array_push($new_