Home > php教程 > php手册 > body text

PHP加密解密字符串函数

WBOY
Release: 2016-06-13 11:36:34
Original
1040 people have browsed it

有时候我们不想让,一个网页地址给人看到,简单的方法就是将它加密成一串字符,函数如下:

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)));
}

$key可以随使设个字符串,$plain_text为想要加密的字符串。与之对应的解密函数:

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));
}

$plain_text为想要解密的字符串;

注意事项:如果用url将加密的字符串作为参数传递,要使用URLEncode()函数将其加密,否则用decrypt($key, $c_t)函数解密获得的参数将得不到原来的字符串。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!