|
mcrypt.* 和 mdecrypt.*
使用 libmcrypt 提供了對稱的加密和解密。這兩組過濾器都支援
mcrypt 增加庫中相同的算法,格式為
mcrypt.ciphername,其中 ciphername
是密碼的名字,將被傳遞給 mcrypt_module_open()。有以下五個過濾器參數可用:
表格 N-1. mcrypt 過濾器參數 參數 | 是否必須 | 預設值 | 取值舉例 |
---|
mode | 可選 | cbc | cbc, cfb, ecb, nofb, ofb, stream | algorithms_dir | 可選 | ini_get('mcrypt.algorithms_dir') | algorithms 模組的目錄 | modes_dir | 可選 | ini_get('mcrypt.modes_dir') | modes 模組的目錄 | iv | 必須 | N/A | 典型為 8,16 或 32 位元組的二進位資料。根據密碼而定 | key | 必須 | N/A | 典型為 8,16 或 32 位元組的二進位資料。根據密碼而定 |
例子 N-10. 用 3DES 將檔案加密輸出
<?php $passphrase = 'My secret';
/* Turn a human readable passphrase * into a reproducable iv/key pair */ $iv = substr(md5('iv'.$passphrase, true), 0, 8); $key = substr(md5('pass1'.$passphrase, true) . md5('pass2'.$passphrase, true), 0, 24); $opts = array('iv'=>$iv, 'key'=>$key);
$fp = fopen('secert-file.enc', 'wb'); stream_filter_append($fp, 'mcrypt.tripledes', STREAM_FILTER_WRITE, $opts); fwrite($fp, 'Secret secret secret data'); fclose($fp); ?>
|
|
例子 N-11. 讀取加密的檔案
<?php $passphrase = 'My secret';
/* Turn a human readable passphrase * into a reproducable iv/key pair */ $iv = substr(md5('iv'.$passphrase, true), 0, 8); $key = substr(md5('pass1'.$passphrase, true) . md5('pass2'.$passphrase, true), 0, 24); $opts = array('iv'=>$iv, 'key'=>$key);
$fp = fopen('secert-file.enc', 'rb'); stream_filter_append($fp, 'mdecrypt.tripledes', STREAM_FILTER_WRITE, $opts); $data = rtrim(stream_get_contents($fp)); fclose($fp);
echo $data; ?>
|
|
| |