1
我使用以下代码来加密用密钥的字符串,使用3-DES算法:这个加密邮件为什么被损坏?
private bool Encode(string input, out string output, byte[] k, bool isDOS7)
{
try
{
if (k.Length != 16)
{
throw new Exception("Wrong key size exception");
}
int length = input.Length % 8;
if (length != 0)
{
length = 8 - length;
for (int i = 0; i < length; i++)
{
input += " ";
}
}
TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.Zeros;
des.Key = k;
ICryptoTransform ic = des.CreateEncryptor();
byte[] bytePlainText = Encoding.Default.GetBytes(input);
MemoryStream ms = new MemoryStream();
CryptoStream cStream = new CryptoStream(ms,
ic,
CryptoStreamMode.Write);
cStream.Write(bytePlainText, 0, bytePlainText.Length);
cStream.FlushFinalBlock();
byte[] cipherTextBytes = ms.ToArray();
cStream.Close();
ms.Close();
output = Encoding.Default.GetString(cipherTextBytes);
}
catch (ArgumentException e)
{
output = e.Message;
//Log.Instance.WriteToEvent("Problem encoding, terminalID= "+objTerminalSecurity.TerminalID+" ,Error" + output, "Security", EventLogEntryType.Error);
return false;
}
return true;
}
我将输出发送参数作为结束到WCF HTTP结合web服务,并且我注意到实际编码的字符串看起来不同,它看起来像\ t和\ n,但是字符大致相同。
这是怎么回事,为什么服务器得到不同的编码字符串?
把这个放在stackoverflow上可能会更好,也可能值得把你输入的问题输出。 – Peanut 2012-06-18 15:28:10
我觉得1.你应该问[so],因为这是一个编码问题,而不是安全设计问题; 2.问题出在你没有发布的代码中。我建议你再问一次[这样],这次提供完整的代码,可以让其他人重现问题。 – Gilles 2012-06-18 21:26:14