关闭
_cuclife.com
当前位置:cuclife.com > 电脑技术 > PHP >

PhP 的3DES加密解密源码

3DES加密解密!TripleDES.class.php


Class TripleDES{ 
public static function Encrypt($key,$text){ 
$cipher = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, ''); 
srand((double)microtime() * 1000000); 
$size=mcrypt_enc_get_iv_size($cipher); 
$iv = mcrypt_create_iv($size, MCRYPT_RANDOM); 
if(mcrypt_generic_init($cipher, $key, $iv) != -1){ 
$cipherText = mcrypt_generic($cipher,$text); 
mcrypt_generic_deinit($cipher); 
return bin2hex($cipherText); 



public static function Decrypt($key,$encryptedText){ 
$cipherText=self::Hex2bin($encryptedText); 
$cipher = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_ECB, ''); 
srand((double)microtime() * 1000000); 
$size=mcrypt_enc_get_iv_size($cipher); 
$iv = mcrypt_create_iv($size, MCRYPT_RAND); 
if (mcrypt_generic_init($cipher, $key, $iv) != -1){ 
$decrypted_data = mdecrypt_generic($cipher,$cipherText); 
mcrypt_generic_deinit($cipher); 
return $decrypted_data; 


public static function Hex2bin($h){ 
if (!is_string($h)) return null; 
$r=''; 
for ($a=0; $a<strlen($h); $a+=2) { 
$r.=chr(hexdec($h{$a}.$h{($a+1)}));  

return $r; 


?> 

文章来源:互联网 本站编辑:admin
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
趣闻*视频
相关文章