请教各位,能否将如下代码转换到c#

开拓者-lada 2012-12-13 07:34:23
<?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";
*/
?>
...全文
96 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
开拓者-lada 2012-12-13
  • 打赏
  • 举报
回复
因为要匹配转换,所以比较麻烦
开拓者-lada 2012-12-13
  • 打赏
  • 举报
回复
我对这个不是很了解,能帮我写个完整的例子么,谢谢
threenewbee 2012-12-13
  • 打赏
  • 举报
回复
参考 http://hi.baidu.com/11345/item/ae131a3f26dc41627c034b88

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧