使用的DataContractSerializer和DataProtectionProvider序列化和加密的对象 [英] Using DataContractSerializer and DataProtectionProvider to serialize and encrypt an object

查看:110
本文介绍了使用的DataContractSerializer和DataProtectionProvider序列化和加密的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图序列化对象到内存流,然后加密该流,然后将其写入文件。 。无法弄清楚什么是错的,内存流是经过解密空

 公共静态异步任务SerializeToFileEncrypt< T> (T 0,StorageFile文件)
{
DataContractSerializer的DSC =新的DataContractSerializer(typeof运算(T));
的MemoryStream MemoryStream的=新的MemoryStream();
dsc.WriteObject(MemoryStream的,O);
DataProtectionProvider提供商=新DataProtectionProvider(本地=用户);
VAR raStream =等待file.OpenAsync(FileAccessMode.ReadWrite);
使用(VAR FILESTREAM = raStream.GetOutputStreamAt(0))
{
等待provider.ProtectStreamAsync(memoryStream.AsInputStream(),文件流);
等待filestream.FlushAsync();
}
}

公共静态异步任务< T> DeserializeFromFileDecrypt< T>(StorageFile文件)
{
DataContractSerializer的DSC =新的DataContractSerializer(typeof运算(T));
的MemoryStream MemoryStream的=新的MemoryStream();
DataProtectionProvider提供商=新DataProtectionProvider();
等待provider.UnprotectStreamAsync((等待file.OpenStreamForReadAsync())AsInputStream(),memoryStream.AsOutputStream());
回报率(T)dsc.ReadObject(MemoryStream的);
}


解决方案

您需要移动到的MemoryStream 的开始,一旦你写完它。否则没有什么从中读取数据,因为你已经在位于端部



这应该工作:

 公共静态异步任务SerializeToFileEncrypt< T>(T 0,StorageFile文件)
{
DataContractSerializer的DSC =新的DataContractSerializer(typeof运算(T));
的MemoryStream MemoryStream的=新的MemoryStream();
dsc.WriteObject(MemoryStream的,O);
memoryStream.Seek(0,SeekOrigin.Begin); //移动到流
DataProtectionProvider提供商=新DataProtectionProvider(本地=用户)的开始;
VAR raStream =等待file.OpenAsync(FileAccessMode.ReadWrite);
使用(VAR FILESTREAM = raStream.GetOutputStreamAt(0))
{
等待provider.ProtectStreamAsync(memoryStream.AsInputStream(),文件流);
等待filestream.FlushAsync();
}
}

公共静态异步任务< T> DeserializeFromFileDecrypt< T>(StorageFile文件)
{
DataContractSerializer的DSC =新的DataContractSerializer(typeof运算(T));
的MemoryStream MemoryStream的=新的MemoryStream();
DataProtectionProvider提供商=新DataProtectionProvider();
等待provider.UnprotectStreamAsync((等待file.OpenStreamForReadAsync())AsInputStream(),memoryStream.AsOutputStream());
memoryStream.Seek(0,SeekOrigin.Begin); //移动到流
返回(T)的dsc.ReadObject(MemoryStream的)的开始;
}


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

解决方案

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

这篇关于使用的DataContractSerializer和DataProtectionProvider序列化和加密的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆