Using DataContractSerializer and DataProtectionPro

2019-02-25 18:15发布

I'm trying to serialize an object to a memory stream and then encrypt this stream and then write it to a file. Can't figure out what's wrong, the memory stream is empty after 'decryption'.

    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);
    }

2条回答
欢心
2楼-- · 2019-02-25 18:37

One addition to the last posted answer, you need to wrap the raStream in a using statement because it is an IDisposable object. Otherwise, if some code later tries to open the file again, it will fail with access denied as the file is still open by the random access stream.

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();
                }
            }
        }
查看更多
Ridiculous、
3楼-- · 2019-02-25 19:00

You need to move to the beginning of MemoryStream once you're done writing to it. Otherwise there's nothing to read from it since you're already positioned at the end.

This should work:

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);
}
查看更多
登录 后发表回答