为什么这段代码不起作用?可能我做错了,但我找不到。
当我运行下面的代码时,我看到消息的唯一部分被正确解密。据我了解,cryptoStreamReader.ReadToEnd()
由于某种原因没有读取整个文件。
实际上我已经通过使用XmlTextReader和StringReader来加密/解密而不是MemoryStream来解决了这个问题。但我想知道该代码有什么问题。任何人都可以帮我找到它吗?提前谢谢!
-
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Security.Cryptography;
using System.Collections.Generic;
namespace Tests
{
internal static class Program
{
private static void Main()
{
#if GENERATE_DATA
var list = new List<int>();
for (int i = 0; i <= 5 * 1024; i++)
list.Add(i);
using (var fileStream = new FileStream("data.xml", FileMode.Create))
{
var serializer = new DataContractSerializer(list.GetType());
serializer.WriteObject(fileStream, list);
}
#endif
var originalData = GetData<List<int>>();
SerializeAndEncrypt(originalData);
var restoredData = DecryptAndDeserialize<List<int>>();
Console.ReadKey();
}
private static T GetData<T>()
{
using (var fileStream = new FileStream("data.xml", FileMode.Open))
{
var serializer = new DataContractSerializer(typeof(T));
return (T)serializer.ReadObject(fileStream);
}
}
private const string ENCRYPTED_DATA_FILE_NAME = "data.enc";
// 32 bytes
private static readonly byte[] KEY = new byte[]
{
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 0,
1, 2
};
// 16 bytes
private static readonly byte[] INITIALIZATION_VECTOR = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6 };
private static string _toEncrypt;
private static string _decrypted;
private static void SerializeAndEncrypt<T>(T data)
{
var memoryStream = new MemoryStream();
var serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(memoryStream, data);
memoryStream.Position = 0L;
var memoryStreamReader = new StreamReader(memoryStream);
var fileStream = File.Open(ENCRYPTED_DATA_FILE_NAME, FileMode.Create);
var aes = Aes.Create();
var cryptoTransform = aes.CreateEncryptor(KEY, INITIALIZATION_VECTOR);
var cryptoStream = new CryptoStream(fileStream, cryptoTransform, CryptoStreamMode.Write);
var cryptoStreamWriter = new StreamWriter(cryptoStream);
_toEncrypt = memoryStreamReader.ReadToEnd();
cryptoStreamWriter.Write(_toEncrypt);
cryptoStream.Close();
fileStream.Close();
memoryStream.Close();
}
private static T DecryptAndDeserialize<T>()
{
var fileStream = File.Open(ENCRYPTED_DATA_FILE_NAME, FileMode.Open);
var aes = Aes.Create();
var cryptoTransform = aes.CreateDecryptor(KEY, INITIALIZATION_VECTOR);
var cryptoStream = new CryptoStream(fileStream, cryptoTransform, CryptoStreamMode.Read);
var cryptoStreamReader = new StreamReader(cryptoStream);
var memoryStream = new MemoryStream();
var memoryStreamWriter = new StreamWriter(memoryStream);
// The following line is shorter than the original (_toEncrypt). Why? :(
_decrypted = cryptoStreamReader.ReadToEnd();
memoryStreamWriter.Write(_decrypted);
memoryStream.Position = 0L;
var serializer = new DataContractSerializer(typeof(T));
var result = (T)serializer.ReadObject(memoryStream);
memoryStream.Close();
cryptoStream.Close();
fileStream.Close();
return result;
}
}
}
-
答案 0 :(得分:1)
您必须刷新StreamWriter
才能获取写入底层的缓冲数据
设备:
private static void SerializeAndEncrypt<T>(T data)
{
...
_toEncrypt = memoryStreamReader.ReadToEnd();
cryptoStreamWriter.Write(_toEncrypt);
// Flush your stream writer. So all buffered
// data get written to the underlying device.
cryptoStreamWriter.Flush();
cryptoStream.Close();
fileStream.Close();
memoryStream.Close();
}
private static T DecryptAndDeserialize<T>()
{
...
_decrypted = cryptoStreamReader.ReadToEnd();
memoryStreamWriter.Write(_decrypted);
// Flush buffered data.
memoryStreamWriter.Flush();
memoryStream.Position = 0L;
var serializer = new DataContractSerializer(typeof(T));
var result = (T)serializer.ReadObject(memoryStream);
memoryStream.Close();
cryptoStream.Close();
fileStream.Close();
return result;
}
希望,这有帮助。