我有一个PHP函数可以正常解密TripleDes消息。现在我试图将它移植到C#,但我得到了不同的结果。我不确定这是错的。它是CBC,相同的密钥,相同的IV,相同的填充等。
PHP函数(它工作正常):
public static function DecryptInstallationCode($enc)
{
$td = mcrypt_module_open(MCRYPT_3DES, '', MCRYPT_MODE_CBC, '');
$iv = '12345678';
$key = '123456789012345678901234';
$bin = base64_decode($enc);
mcrypt_generic_init($td, $key, $iv);
$str = mdecrypt_generic($td, $bin);
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $str;
}
C#函数(返回不同的结果):
public static string DecryptInstallationCode(string enc)
{
const string iv = "12345678";
const string key = "123456789012345678901234";
var bin = Convert.FromBase64String(enc);
TripleDES tdes = TripleDES.Create();
tdes.IV = Encoding.ASCII.GetBytes(iv);
tdes.KeySize = 192;
tdes.Key = Encoding.ASCII.GetBytes(key);
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
var ict = tdes.CreateEncryptor();
var result = ict.TransformFinalBlock(bin, 0, bin.Length);
var strResult = Encoding.UTF8.GetString(result);
return strResult;
}
答案 0 :(得分:0)