110,538
社区成员
发帖
与我相关
我的任务
分享
<?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;
}
}
/*
$key = "111111";
$message = "2222222";
$d = new TripleDES;
$ht = $d->Encrypt($key, $message);
$htd = $d->Decrypt($key, $ht);
echo $ht."------------------>".$htd."<hr />Standard->key:111111 str:2222222 output:C7375081D6C37A00";
*/
?>