动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 如何利Mcrypt扩展库进行加密和解密

作者来源: 
阅读 数 209 人次 , 2006-3-29 4:13:00 


综述:mcrypt 2.4.7是一个功能强大的加密算法扩展库,它包括有22种算法,其中就包括下面的几种算法:

blowfish rc2 safer-sk64 xtea
cast-256 rc4 safer-sk128
des rc4-iv serpent
enigma rijndael-128 threeway
gost rijndael-192 tripledes
loki97 rijndael-256 twofish
panamasaferplus wake

如何安装mcrypt?

在标准的php软件包中不包括mcrypt,因此需要下载它,下载的地址为:ftp://argeas.cs-net.gr/pub/unix/mcrypt/ 。下载后,按照下面的方法进行编译,并把它扩充在php中:

下载mcrypt软件包。

gunzipmcrypt-x.x.x.tar.gz
tar -xvfmcrypt-x.x.x.tar
./configure --disable-posix-threads
make
make install
cd to your php directory.
./configure -with-mcrypt=[dir] [--other-configuration-directives]
make
make install

根据你的要求和php安装时与服务器作适当的修改。

如何使用mcrypt扩展库对数据进行加密?

首先,我们将介绍如何使用mcrypt扩展库对数据进行加密,然后再介绍如何使用它进行解密。下面的代码对这一过程进行了演示,首先是对数据进行加密,然后在浏览器上显示加密后的数据,并将加密后的数据还原为原来的字符串,将它显示在浏览器上。

使用mcrypt对数据进行加、解密

<?php
// designate string to be encrypted
$string = "applied cryptography, by bruce schneier, is
a wonderful cryptography reference.";

// encryption/decryption key
$key = "four score and twenty years ago";

// encryption algorithm
$cipher_alg = mcrypt_rijndael_128;

// create the initialization vector for added security.
$iv = mcrypt_create_iv(mcrypt_get_iv_size($cipher_alg,
mcrypt_mode_ecb), mcrypt_rand);

// output original string
print "original string: $string
";

// encrypt $string
$encrypted_string = mcrypt_encrypt($cipher_alg, $key,
$string, mcrypt_mode_cbc, $iv);

// convert to hexadecimal and output to browser
print "encrypted string: ".bin2hex($encrypted_string)."

";
$decrypted_string = mcrypt_decrypt($cipher_alg, $key,
$encrypted_string, mcrypt_mode_cbc, $iv);

print "decrypted string: $decrypted_string";
?>


执行上面的脚本将会产生下面的输出:

original string: applied cryptography, by bruce schneier, is a wonderful cryptography reference.

encrypted string: 02a7c58b1ebd22a9523468694b091e60411cc4dea8652bb8072 34fa06bbfb20e71ecf525f29df58e28f3d9bf541f7ebcecf62b c89fde4d8e7ba1e6cc9ea24850478c11742f5cfa1d23fe22fe8 bfbab5e

decrypted string: applied cryptography, by bruce schneier, is a wonderful cryptography reference.


上面的代码中两个最典型的函数是mcrypt_encrypt()和 mcrypt_decrypt(),它们的用途是显而易见的。我们使用了"电报密码本"模式,mcrypt提供了几种加密方式,由于每种加密方式都有可以影响密码安全的特定字符,因此对每种模式都需要了解。对那些没有接触过密码系统的读者来说,可能对mcrypt_create_iv()函数更有兴趣,我们会提到它创建的初始化向量(hence, iv),这一向量可以使每条信息彼此独立。 尽管不是所有的模式都需要这一初始化变量,但如果在要求的模式中没有提供这一变量,php就会给出警告信息。

 本文Tagsmd5  加密  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP中实现大图自动缩成小图(及GD库的安装)
· 下一篇:模板--如何在PHP中使用它们
· 一个广告轮播系统的例子(内含文件上传的方法)
· 能把汉字转化为拼音的一个函数
· 使用PHP4.2.0及以后版本的注意事项
· 计算MySQL中的datetime字段与当前时间差一例
· 在PHP中得到多选的下拉菜单的各项值


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077