我正在尝试将对象序列化为内存流,然后加密此流,然后将其写入文件。无法弄清楚什么是错的,'解密'后内存流是空的。
public static async Task SerializeToFileEncrypt<T>(T o, StorageFile file)
{
DataContractSerializer dsc = new DataContractSerializer(typeof(T));
MemoryStream memoryStream = new MemoryStream();
dsc.WriteObject(memoryStream, o);
DataProtectionProvider provider = new DataProtectionProvider("Local=User");
var raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using(var filestream = raStream.GetOutputStreamAt(0))
{
await provider.ProtectStreamAsync(memoryStream.AsInputStream(), filestream);
await filestream.FlushAsync();
}
}
public static async Task<T> DeserializeFromFileDecrypt<T>(StorageFile file)
{
DataContractSerializer dsc = new DataContractSerializer(typeof(T));
MemoryStream memoryStream = new MemoryStream();
DataProtectionProvider provider = new DataProtectionProvider();
await provider.UnprotectStreamAsync((await file.OpenStreamForReadAsync()).AsInputStream(), memoryStream.AsOutputStream());
return (T) dsc.ReadObject(memoryStream);
}
答案 0 :(得分:3)
完成写入后,您需要移至MemoryStream
的开头。否则没有什么可以读的,因为你已经定位在最后。
这应该有效:
public static async Task SerializeToFileEncrypt<T>(T o, StorageFile file)
{
DataContractSerializer dsc = new DataContractSerializer(typeof(T));
MemoryStream memoryStream = new MemoryStream();
dsc.WriteObject(memoryStream, o);
memoryStream.Seek(0, SeekOrigin.Begin); // move to the beginning of the stream
DataProtectionProvider provider = new DataProtectionProvider("Local=User");
var raStream = await file.OpenAsync(FileAccessMode.ReadWrite);
using(var filestream = raStream.GetOutputStreamAt(0))
{
await provider.ProtectStreamAsync(memoryStream.AsInputStream(), filestream);
await filestream.FlushAsync();
}
}
public static async Task<T> DeserializeFromFileDecrypt<T>(StorageFile file)
{
DataContractSerializer dsc = new DataContractSerializer(typeof(T));
MemoryStream memoryStream = new MemoryStream();
DataProtectionProvider provider = new DataProtectionProvider();
await provider.UnprotectStreamAsync((await file.OpenStreamForReadAsync()).AsInputStream(), memoryStream.AsOutputStream());
memoryStream.Seek(0, SeekOrigin.Begin); // move to the beginning of the stream
return (T) dsc.ReadObject(memoryStream);
}
答案 1 :(得分:0)
对最后发布的答案的一个补充,您需要将raStream包装在using语句中,因为它是一个IDisposable对象。否则,如果某些代码稍后再次尝试打开该文件,则会因访问被拒绝而失败,因为该文件仍由随机访问流打开。
public static async Task SerializeToFileEncrypt<T>(T o, StorageFile file)
{
DataContractSerializer dsc = new DataContractSerializer(typeof(T));
MemoryStream memoryStream = new MemoryStream();
dsc.WriteObject(memoryStream, o);
memoryStream.Seek(0, SeekOrigin.Begin); // move to the beginning of the stream
DataProtectionProvider provider = new DataProtectionProvider("Local=User");
using (var raStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
using (var filestream = raStream.GetOutputStreamAt(0))
{
await provider.ProtectStreamAsync(memoryStream.AsInputStream(), filestream);
await filestream.FlushAsync();
}
}
}