教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php DES加密解密的代码一例

php DES加密解密的代码一例

发布时间:2015-12-16   编辑:jiaochengji.com
为大家提供一个php DES加密与解密的代码,有需要的朋友,可以参考下。

代码如下:
 

复制代码 代码示例:

<?php
/**
 * php des加密与解密
 * by http://www.jbxue.com
*/
 function do_mencrypt($input, $key)
{
 $input = str_replace(""n", "", $input);
 $input = str_replace(""t", "", $input);
 $input = str_replace(""r", "", $input);
 $key = substr(md5($key), 0, 24);
 $td = mcrypt_module_open('tripledes', '', 'ecb', '');
 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 mcrypt_generic_init($td, $key, $iv);
 $encrypted_data = mcrypt_generic($td, $input);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);
 return trim(chop(base64_encode($encrypted_data)));
}

//$input - stuff to decrypt
//$key - the secret key to use
function do_mdecrypt($input, $key)
{
 $input = str_replace(""n", "", $input);
 $input = str_replace(""t", "", $input);
 $input = str_replace(""r", "", $input);
 $input = trim(chop(base64_decode($input)));
 $td = mcrypt_module_open('tripledes', '', 'ecb', '');
 $key = substr(md5($key), 0, 24);
 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
 mcrypt_generic_init($td, $key, $iv);
 $decrypted_data = mdecrypt_generic($td, $input);
 mcrypt_generic_deinit($td);
 mcrypt_module_close($td);
 return trim(chop($decrypted_data));
}
?>

您可能感兴趣的文章:
关于des加密与解密实现方法(php net两个版本)
php使用3des加密的代码(兼容.net)

您可能感兴趣的文章:
php base64加密解密的实现代码
php中DES加密与解密的实例代码
PHP DES加解密方法代码
php中加密解密DES的正确使用姿势
php DES加密解密的代码一例
c#实现加密与解密的实例代码
密码学之对称加密
PHP中DES加解密的代码示例
C# 生成注册码的代码一例
关于des加密与解密实现方法(php net两个版本)

[关闭]
~ ~