国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

怎樣在Mcrypt擴展庫進行加密和解密

2024-05-04 23:05:37
字體:
來源:轉載
供稿:網友
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就會給出警告信息。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 明光市| 上饶县| 永顺县| 正镶白旗| 宣汉县| 利川市| 湟源县| 青阳县| 永昌县| 尼木县| 普洱| 泾源县| 鄂伦春自治旗| 松江区| 视频| 刚察县| 永川市| 汉川市| 蒙自县| 花垣县| 新和县| 武定县| 晋城| 开江县| 师宗县| 广灵县| 彭州市| 大荔县| 天长市| 通化县| 闸北区| 齐齐哈尔市| 抚松县| 娄烦县| 格尔木市| 江川县| 馆陶县| 防城港市| 浦城县| 时尚| 星座|