PHP使用DES进行加密和解密

释放双眼,带上耳机,听听看~!

DES是一种标准的数据加密算法,关于这个算法的详细介绍可以参考wiki和百度百科:

wiki百科 百度百科

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

在配置文件中将这个扩展打开还不能够在windows环境下使用

需要将PHP文件夹下的 libmcrypt.dll 拷贝到系统的 system32 目录下,这是通过phpinfo可以查看到mcrypt表示这个模块可以正常试用了。

 下面是PHP中使用DES加密解密的一个例子:

   

PHP使用DES进行加密和解密
//
$input – stuff to decrypt
PHP使用DES进行加密和解密    //$key – the secret key to use
PHP使用DES进行加密和解密
PHP使用DES进行加密和解密    
function
 do_mencrypt(
$input
,
 
$key
)
PHP使用DES进行加密和解密    {
PHP使用DES进行加密和解密        
$input
 

 
str_replace
(
""
n
"

""

$input
);
PHP使用DES进行加密和解密        
$input
 = str_replace(
""
t
"
,
 
""
,
 
$input
);
PHP使用DES进行加密和解密        
$input
 

 
str_replace
(
""
r
"

""

$input
);
PHP使用DES进行加密和解密        
$key
 = substr(md5(
$key
), 0, 24);
PHP使用DES进行加密和解密        
$td
 = mcrypt_module_open('tripledes', '', 'ecb', '');
PHP使用DES进行加密和解密        
$iv
 = mcrypt_create_iv(mcrypt_enc_get_iv_size(
$td
), MCRYPT_RAND);
PHP使用DES进行加密和解密        mcrypt_generic_init(
$td

$key

$iv
);
PHP使用DES进行加密和解密        
$encrypted_data
 = mcrypt_generic(
$td

$input
);
PHP使用DES进行加密和解密        mcrypt_generic_deinit(
$td
);
PHP使用DES进行加密和解密        mcrypt_module_close(
$td
);
PHP使用DES进行加密和解密        return trim(chop(base64_encode(
$encrypted_data
)));
PHP使用DES进行加密和解密    }
PHP使用DES进行加密和解密    
PHP使用DES进行加密和解密    //
$input
 – stuff to decrypt
PHP使用DES进行加密和解密    //
$key
 – the secret key to use
PHP使用DES进行加密和解密    
PHP使用DES进行加密和解密    function do_mdecrypt(
$input

$key
)
PHP使用DES进行加密和解密    {
PHP使用DES进行加密和解密        
$input
 = str_replace(
""
n
"
,
 
""
,
 
$input
);
PHP使用DES进行加密和解密        
$input
 

 
str_replace
(
""
t
"

""

$input
);
PHP使用DES进行加密和解密        
$input
 = str_replace(
""
r
"
,
 
""
,
 
$input
);
PHP使用DES进行加密和解密        
$input
 

 
trim
(
chop
(
base64_decode
(
$input
)));
PHP使用DES进行加密和解密        
$td
 

 mcrypt_module_open(
'
tripledes
'
,
 
''
,
 
'
ecb
'
,
 
''
);
PHP使用DES进行加密和解密        
$key
 

 
substr
(
md5
(
$key
)
,
 
0
,
 
24
);
PHP使用DES进行加密和解密        
$iv
 

 mcrypt_create_iv(mcrypt_enc_get_iv_size(
$td
)
,
 MCRYPT_RAND);
PHP使用DES进行加密和解密        mcrypt_generic_init(
$td
,
 
$key
,
 
$iv
);
PHP使用DES进行加密和解密        
$decrypted_data
 

 mdecrypt_generic(
$td
,
 
$input
);
PHP使用DES进行加密和解密        mcrypt_generic_deinit(
$td
);
PHP使用DES进行加密和解密        mcrypt_module_close(
$td
);
PHP使用DES进行加密和解密        
return
 
trim
(
chop
(
$decrypted_data
));

PHP使用DES进行加密和解密    }   

 

 

给TA打赏
共{{data.count}}人
人已打赏
安全技术

Ubuntu下安装Mysql 以及mysql-query-browser

2022-1-11 12:36:11

气候事件

长江大桥:台风来袭限速不封桥

2011-8-5 8:27:33

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索