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

php中DES加密与解密的实例代码

发布时间:2016-04-03   编辑:jiaochengji.com
本文介绍下,在php编程中使用des进行加密与密码的一例代码,有需要的朋友参考下吧。

php中有一个扩展可以支持DES的加密算法,是:

extension=php_mcrypt.dll
 

在配置文件中将这个扩展打开,还不能够在windows环境下使用。需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下。
然后,通过phpinfo查看到mcrypt,即可使用这个模块了。

PHP中使用DES加密解密的实例代码:
 

复制代码 代码示例:

<?php
/**
 * des 加密与解密
 * edit www.jbxue.com
*/
//$input - stuff to decrypt
//$key - the secret key to use

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

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

关键词: php加密解密   
[关闭]
~ ~