C#3des加密,如何知道何时失败?
我使用以下代码加密/解密c#和php之间的字符串:C#3des加密,如何知道何时失败?,c#,.net,visual-studio-2010,encryption,C#,.net,Visual Studio 2010,Encryption,我使用以下代码加密/解密c#和php之间的字符串: 我可以将其与解密的字符串bu进行比较,即使.net lib来自另一种语言,所有计算机上的“错误”文本是否都相同?解密失败可能意味着很多事情 解密引擎TransformFinalBlock()会引发异常,因为您提供了无效的密钥或IV 您提供了有效但不正确的IV-这可以解决,因为您知道它们的正确值以及如何保护它们 您提供的密钥正确,但密码文本错误(或被篡改) 1是算法故障,可以处理 对于2和3,不幸的是,如果不将解密文本与原始明文进行比较,就很难知
我可以将其与解密的字符串bu进行比较,即使.net lib来自另一种语言,所有计算机上的“错误”文本是否都相同?解密失败可能意味着很多事情
块密码总是在明文中添加填充,而不考虑加密前用于获取下一个完整块大小的链接技术。解密应该删除填充,但您可能希望字符串以一个或多个null结尾。谨防这一点,并考虑保持数据的长度。如果你在2011使用三重DES,我会说它在你的代码运行之前就已经失败了。
class encryption
{
public string SimpleTripleDes(string Data)
{
byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
byte[] iv = Encoding.ASCII.GetBytes("password");
byte[] data = Encoding.ASCII.GetBytes(Data);
byte[] enc = new byte[0];
TripleDES tdes = TripleDES.Create();
tdes.IV = iv;
tdes.Key = key;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
ICryptoTransform ict = tdes.CreateEncryptor();
enc = ict.TransformFinalBlock(data, 0, data.Length);
return ByteArrayToString(enc);
}
public string SimpleTripleDesDecrypt(string Data)
{
byte[] key = Encoding.ASCII.GetBytes("passwordDR0wSS@P6660juht");
byte[] iv = Encoding.ASCII.GetBytes("password");
byte[] data = StringToByteArray(Data);
byte[] enc = new byte[0];
TripleDES tdes = TripleDES.Create();
tdes.IV = iv;
tdes.Key = key;
tdes.Mode = CipherMode.CBC;
tdes.Padding = PaddingMode.Zeros;
ICryptoTransform ict = tdes.CreateDecryptor();
enc = ict.TransformFinalBlock(data, 0, data.Length);
return Encoding.ASCII.GetString(enc);
}
public static string ByteArrayToString(byte[] ba)
{
string hex = BitConverter.ToString(ba);
return hex.Replace("-", "");
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
}
Could not find any recognizable digits