嘿,我对加密和解密很新,甚至是c#语言,说实话。基本上,我有一个tcp聊天服务器,可以“保存”日志并加密文本文件。这是我加密的方式(基于MSDN示例):
public static void EncryptFile(string strInputFileName, string strOutputFileName, string strKey)
{
FileStream fsIn = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read);
FileStream fsOut = new FileStream(strOutputFileName, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
ICryptoTransform desencrypt = des.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsOut, desencrypt, CryptoStreamMode.Write);
byte[] byteArrayInput = new byte[fsIn.Length - 1];
fsIn.Read(byteArrayInput, 0, byteArrayInput.Length);
cryptostream.Write(byteArrayInput, 0, byteArrayInput.Length);
fsIn.Close();
fsOut.Close();
}
方法成功完全加密文件。这是我的解密方法:
public static void DecryptFile(string strInputFileName, string strOutputFileName, string strKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(strKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(strKey);
byte[] te = new byte[1024];
FileStream fsRead = new FileStream(strInputFileName, FileMode.Open, FileAccess.Read);
ICryptoTransform desdecrypt = des.CreateDecryptor();
CryptoStream cryptostream = new CryptoStream(fsRead, desdecrypt, CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(strOutputFileName);
fsDecrypted.Write(new StreamReader(cryptostream).ReadToEnd());//This is where the "Bad Data" occurs.
fsDecrypted.Flush();
fsDecrypted.Close();
fsRead.Close();
}
当我检查cryptostream对象时,它说它抛出了异常,“Stream不支持搜索”。
非常感谢任何帮助!
答案 0 :(得分:4)
下面:
cryptostream.Write(byteArrayInput, 0, byteArrayInput.Length);
fsIn.Close();
fsOut.Close();
您正在关闭fsOut
,而不关闭cryptostream
。这意味着加密流没有机会刷新任何最终块等。
此外:
using
语句而不是手动调用Close或Dispose Read
一次,并假设它将读取所有数据 - 您不会检查返回值。 (你也是出于某种原因删除了输入文件的最后一个字节......为什么?)通常,你应该循环,读入缓冲区,然后写出你读取的多少字节,直到Read方法返回0如果您使用的是.NET 4,Stream.CopyTo
是您的朋友。答案 1 :(得分:2)
objCryptStream.CopyTo(stream);
为我工作,完整的代码是
public static string DecryptString(string encriptedText, string key)
{
try
{
//Convert the text into bytest
byte[] ecriptedBytes = System.Convert.FromBase64String(encriptedText);
// Create a memory stream to the passed buffer
MemoryStream objMemStream = new MemoryStream(ecriptedBytes);
//Set the legal keys and initialization verctors
objCrptoService.Key = GetLegalsecretKey(key);
objCrptoService.IV = GetLegalIV();
// Create a CryptoStream using the memory stream and the cryptographic service provider version
// of the Data Encryption stanadard algorithm key
CryptoStream objCryptStream = new CryptoStream(objMemStream, objCrptoService.CreateDecryptor(), CryptoStreamMode.Read);
// Create a StreamReader for reading the stream.
//StreamReader objstreamReader = new StreamReader(objCryptStream);
MemoryStream stream = new MemoryStream();
objCryptStream.CopyTo(stream);
stream.Position = 0;
StreamReader R = new StreamReader(stream);
string outputText = R.ReadToEnd();
// Close the streams.
R.Close();
objCryptStream.Close();
objMemStream.Close();
return outputText;
}
catch (Exception exc)
{
return "";
}
}
答案 2 :(得分:0)
解决我的问题的是在创建文件时在cryptostream上调用FlushFinalBlock
CryptoStream cryptostream = new CryptoStream(memoryStream, this._encryptionKeyHelper.Encryptor(), CryptoStreamMode.Write);
xmlser.Serialize(cryptostream, builderObject);
cryptostream.FlushFinalBlock();