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