結果:
hash2: e86cf511bd5490d46d5cd61738c82c0c
可以發現,盡管二個結果的長度都是32個字符,但明文中一點微小的變化使得結果發生了很大的變化,因此,混編和md5()函數是檢查數據中微小變化的一個很好的工具。
盡管crypt()和md5()各有用處,但二者在功能上都受到一定的限制。在下面的部分中,我們將介紹二個非常有用的被稱作mcrypt和mhash的php擴展,將大大拓展php用戶在加密方面的選擇。
盡管我們在上面的小節中說明了單向加密的重要性,但有時我們可能需要在加密后,再把密碼數據還原成原來的數據,幸運的是,php通過mcrypt擴展庫的形式提供了這種可能性。
mcrypt
mcrypt 2.5.7 unix | win32
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
安裝:
在標準的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的優點不僅僅在于其提供的加密算法較多,還在于它可以對數據進行加/解密處理,此外,它還提供了35種處理數據用的函數。盡管對這些函數進行詳細介紹已經超出了這篇文章的范圍,我還是要就幾個典型的函數作一下簡要的介紹。
首先,我將介紹如何使用mcrypt擴展庫對數據進行加密,然后再介紹如何使用它進行解密。下面的代碼對這一過程進行了演示,首先是對數據進行加密,然后在瀏覽器上顯示加密后的數據,并將加密后的數據還原為原來的字符串,將它顯示在瀏覽器上。
使用mcrypt對數據進行加、解密
<?php
// designate string to be encrypted
$string = "applied cryptography, by bruce schneier, is
a wonderful cryptography reference.";
// encryption/decryption key
$key = "
// 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 <p>";
// 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)."<p>";
$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就會給出警告信息。
mhash擴展庫
http://sourceforge.net/projects/mhash/
0.8.3版的mhash擴展庫支持12種混編算法,仔細檢查mhash v.0.8.3的頭文件mhash.h可以知道,它支持下面的混編算法:
crc32 haval160 md5
crc32b haval192 ripemd160
gost haval224 sha1
haval128 haval256 tiger
安裝
象mcrypt一樣,mhash也沒有包括在php軟件包中,對于非windows用戶而言,下面是安裝過程:
下載mhash擴展庫
gunzipmhash-x.x.x.tar.gz
tar -xvfmhash-x.x.x.tar
./configure
make
make install
cd <php所在的目錄>
./configure -with-mhash=[dir] [--other-configuration-directives]
make
make install
象mcrypt一樣,根據php在互聯網服務器軟件上的安裝方式,可能需要對mhash進行其他的配置。
對于windows用戶而言,http://www.php4win.de中有一個很好的包括mhash擴展庫在內的php軟件包。只要下載并進行解壓縮,然后根據其中的readme.first文檔中的指令進行安裝即可。
使用mhash
對信息進行混編非常簡單,看一下下面的例子:
<?php
$hash_alg = mhash_tiger;
$message = "these are the directions to the secret fort. two steps left, three steps right, and cha chacha.";
$hashed_message = mhash($hash_alg, $message);
print "the hashed message is ". bin2hex($hashed_message);
?>
執行這一段腳本程序將得到下面的輸出結果:
the hashed message is 07a92a4db3a4177f19ec9034ae5400eb60d1a9fbb4ade461
在這里使用bin2hex()函數的目的是方便我們理解$hashed_message的輸出,這是因為混編的結果是二進制格式,為了能夠將它轉化為易于理解的格式,必須將它轉換為十六進制格式。
新聞熱點
疑難解答