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