【发布时间】:2012-08-01 15:58:33
【问题描述】:
加密字符串时没有错误,但是, 当我尝试解密字符串时出现错误,它表示 Input.Length 无效。有什么想法吗?
public class Crypt
{
public string Encrypt(string Key, string Input)
{
ICryptoTransform crypted = tran(Key).CreateEncryptor();
UTF8Encoding utf8 = new UTF8Encoding();
return utf8.GetString(crypted.TransformFinalBlock(utf8.GetBytes(Input), 0, Input.Length));
}
public string Decrypt(string Key, string Input)
{
ICryptoTransform crypted = tran(Key).CreateDecryptor();
UTF8Encoding utf8 = new UTF8Encoding();
return utf8.GetString(crypted.TransformFinalBlock(utf8.GetBytes(Input), 0, Input.Length));
}
private TripleDESCryptoServiceProvider tran(string Key)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
UTF8Encoding utf8 = new UTF8Encoding();
TripleDESCryptoServiceProvider tDES = new TripleDESCryptoServiceProvider();
tDES.Key = md5.ComputeHash(utf8.GetBytes(Key));
tDES.Mode = CipherMode.ECB;
tDES.Padding = PaddingMode.PKCS7;
return tDES;
}
}
【问题讨论】:
-
不需要
new UTF8Encoding(),Encoding.UTF8已经可用了。
标签: c# encryption 3des tripledes