mcrypt.* 和 mdecrypt.* 使用 libmcrypt 提供了对称的加密和解密。这两组过滤器都支持 mcrypt 扩展库中相同的算法,格式为 mcrypt.ciphername,其中 ciphername 是密码的名字,将被传递给 mcrypt_module_open()。有以下五个过滤器参数可用:
Example#1 用 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);?>
Example#2 读取加密的文件
<?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;?>