网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > PHP技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.php访问查询mysql数据的三种方法.
.php调用存储过程返回结果集,解决.
.屏蔽浏览器缓存另类方法.
.PHP和JAVA的XML-RPC中文问题解决.
.提取HTML标签.
.PHP与SQL注入攻击[一] .
.向普通人加密 用PHP程序保护数据.
.用php来检测proxy.
.PHP集成FCK的函数代码.
.使用php作linux自动执行脚本.
.使用PHPLIB访问多个数据库.
.如何对php程序中的常见漏洞进行攻.
.PHPUnit袖珍指南之自动测试.
.php数据库连接.
.在ZendStudio 中开启PHP 5.2.x 环.
.PHP用代码实现文件下载.
.php的content-type=text/html问题.
.PHP+MySQL应用中使用XOR运算加密.
.NuSOAP与PHPRPC比较.
.详细解析:关于PHP事件驱动问题.

利Mcrypt扩展库进行加密和解

发表日期:2008-12-21


综述: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就会给出警告信息。

上一篇:一个PHP中常用email校验语句的解释 人气:109
下一篇:在PHP中如何使用模板 人气:100
浏览全部PHP的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐