从PHP手册中提取的一个加密/解密函数
作者:admin 日期:2007-06-02
//encrypt.php
<?
function encrypt($key, $plain_text) {
$plain_text = trim($plain_text);
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$c_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT, $iv);
return trim(chop(base64_encode($c_t)));
}
function decrypt($key, $c_t) {
$c_t = trim(chop(base64_decode($c_t)));
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$p_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT, $iv);
return trim(chop($p_t));
}
?>
function encrypt($key, $plain_text) {
$plain_text = trim($plain_text);
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$c_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $plain_text, MCRYPT_ENCRYPT, $iv);
return trim(chop(base64_encode($c_t)));
}
function decrypt($key, $c_t) {
$c_t = trim(chop(base64_decode($c_t)));
$iv = substr(md5($key), 0,mcrypt_get_iv_size (MCRYPT_CAST_256,MCRYPT_MODE_CFB));
$p_t = mcrypt_cfb (MCRYPT_CAST_256, $key, $c_t, MCRYPT_DECRYPT, $iv);
return trim(chop($p_t));
}
?>
调用代码:
//test.php
<?php
include("encrypt.php");
//设置密匙为"cf",待加密的字符串为"root"
$encryted_str=encrypt("cf","root");
echo "加密后的字符串为:$encryted_str<br>";
//解密
$decrypt_str=decrypt("cf",$encryted_str);
echo "解密后的字符串为:$decrypt_str<br>";
?>
include("encrypt.php");
//设置密匙为"cf",待加密的字符串为"root"
$encryted_str=encrypt("cf","root");
echo "加密后的字符串为:$encryted_str<br>";
//解密
$decrypt_str=decrypt("cf",$encryted_str);
echo "解密后的字符串为:$decrypt_str<br>";
?>
运行结果:
加密后的字符串为:vPATwA==
解密后的字符串为:root
评论: 0 | 引用: 0 | 查看次数: 3088
发表评论
广告位