时间:2021-07-01 10:21:17 帮助过:9人阅读
这篇文章主要介绍了PHP实现的DES加密解密方法,结合完整实例形式分析了PHP实现DES加密解密的相关技巧,需要的朋友可以参考下,具体如下:
<?php $key = 'very important data'; function jiami($key, $str) { /* Open module, and create IV */ $td = mcrypt_module_open('des', '', 'ecb', ''); //$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); //$td = mcrypt_module_open('des', '', 'cbc', ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); $iv_size = mcrypt_enc_get_iv_size($td); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); /* Initialize encryption handle */ if (mcrypt_generic_init($td, $key, $iv) === -1) { return FALSE; } /* Encrypt data */ $c_t = mcrypt_generic($td, $str); /* Clean up */ mcrypt_generic_deinit($td); mcrypt_module_close($td); return $c_t; } function jiemi($key, $str) { /* Open module, and create IV */ $td = mcrypt_module_open('des', '', 'ecb', ''); //$td = mcrypt_module_open(MCRYPT_DES, '', MCRYPT_MODE_CBC, ''); //$td = mcrypt_module_open('des', '', 'cbc', ''); $key = substr($key, 0, mcrypt_enc_get_key_size($td)); $iv_size = mcrypt_enc_get_iv_size($td); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); /* Initialize encryption handle */ if (mcrypt_generic_init($td, $key, $iv) === -1) { return FALSE; } /* Reinitialize buffers for decryption */ $p_t = mdecrypt_generic($td, $str); /* Clean up */ mcrypt_generic_deinit($td); mcrypt_module_close($td); return trim($p_t); } $str = 'hello world!'; echo 'key:' . $key . '<br>'; echo 'str:' . $str . '<br>'; $jiami = jiami($key, $str); echo '加密:' . $jiami . '<br>'; file_put_contents('jiamiqian.txt', $str); file_put_contents('jiamihou.txt', $jiami); $jiemi = jiemi($key, $jiami); echo '解密:' . $jiemi . '<br>';
以上就是php des加密解密代码的详细内容,更多请关注Gxl网其它相关文章!