Home > Backend Development > PHP Tutorial > php结合md5实现的加密解密方法_PHP

php结合md5实现的加密解密方法_PHP

WBOY
Release: 2016-05-28 11:48:56
Original
1201 people have browsed it

本文实例讲述了php结合md5实现的加密解密方法。分享给大家供大家参考,具体如下:

最近在整理代码发现了一个不错的东西,结合md5的加解密算法。网上关于php结合md5的加密,解密算法比较少的,其实php手册里面就有,改一改就行了。在此贴一下,用这算法要加载一个php模块mcrypt,不然用不了。

//加密
function string2secret($str)
{
 $key = "123";
 $td = mcrypt_module_open(MCRYPT_DES,'','ecb','');
 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 $ks = mcrypt_enc_get_key_size($td);
 $key = substr(md5($key), 0, $ks);
 mcrypt_generic_init($td, $key, $iv);
 $secret = mcrypt_generic($td, $str);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);
 return $secret;
}
//解密
function secret2string($sec)
{
 $key = "123";
 $td = mcrypt_module_open(MCRYPT_DES,'','ecb','');
 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 $ks = mcrypt_enc_get_key_size($td);
 $key = substr(md5($key), 0, $ks);
 mcrypt_generic_init($td, $key, $iv);
 $string = mdecrypt_generic($td, $sec);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);
 return trim($string);
}
echo secret2string(string2secret("11111111111111111")); //显示结果是11111111111111111
echo string2secret("11111111111111111"); //显示乱码

Copy after login

php经常用的加解密函数,base64_encode,base64_decode。

更多关于PHP加密解密相关内容感兴趣的读者可查看本站专题:《php加密方法总结》

希望本文所述对大家PHP程序设计有所帮助。

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template