使用BinaryFormatter的问题反序列化加密数据 [英] Issue deserializing encrypted data using BinaryFormatter
本文介绍了使用BinaryFormatter的问题反序列化加密数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面是我的代码:
公共静态无效的保存< T>(T toSerialize,串FILESPEC){
的BinaryFormatter格式=新的BinaryFormatter();
DESCryptoServiceProvider DES =新DESCryptoServiceProvider();使用
(的FileStream流= File.Create(FILESPEC)){使用
(CryptoStream的CryptoStream的=新的CryptoStream(流des.CreateEncryptor(键,IV),CryptoStreamMode.Write)){
formatter.Serialize(CryptoStream的,toSerialize);
cryptoStream.FlushFinalBlock();
}
}
}
公共静态T装载< T>(字符串文件规范){
的BinaryFormatter格式=新的BinaryFormatter();
DESCryptoServiceProvider DES =新DESCryptoServiceProvider();使用
(的FileStream流= File.OpenRead(FILESPEC)){使用
(CryptoStream的CryptoStream的=新的CryptoStream(流des.CreateEncryptor(键,IV),CryptoStreamMode.Read)){
回报率(T)formatter.Deserialize(CryptoStream的);
}
}
}
密钥和IV都是静态的字节数组为8的长度我使用用于测试目的哪些。有错误如下:
二进制流178不包含一个有效的BinaryHeader。可能的原因是序列化和反序列化
块引用>
任何帮助深表感谢!
之间无效流或对象版本更改解决方案一小错字:你的
加载
方法应使用des.CreateDecryptor
,就像这样:
<预类=郎-CS prettyprint-覆盖>公共静态T装载< T>(字符串文件规范)
{
的BinaryFormatter格式=新的BinaryFormatter();
DESCryptoServiceProvider DES =新DESCryptoServiceProvider();使用
(的FileStream流= File.OpenRead(FILESPEC))使用
{
(CryptoStream的CryptoStream的=
新的CryptoStream(流des.CreateDecryptor(钥匙,四),
CryptoStreamMode.Read))
{
回报率(T)formatter.Deserialize(CryptoStream的);
}
}
}
Here is my code:
public static void Save<T>(T toSerialize, string fileSpec) { BinaryFormatter formatter = new BinaryFormatter(); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (FileStream stream = File.Create(fileSpec)) { using (CryptoStream cryptoStream = new CryptoStream(stream, des.CreateEncryptor(key, iv), CryptoStreamMode.Write)) { formatter.Serialize(cryptoStream, toSerialize); cryptoStream.FlushFinalBlock(); } } } public static T Load<T>(string fileSpec) { BinaryFormatter formatter = new BinaryFormatter(); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (FileStream stream = File.OpenRead(fileSpec)) { using (CryptoStream cryptoStream = new CryptoStream(stream, des.CreateEncryptor(key, iv), CryptoStreamMode.Read)) { return (T)formatter.Deserialize(cryptoStream); } } }
Key and iv are both static byte arrays with a length of 8 which I'm using for testing purposes. There error is as follows:
Binary stream '178' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization
Any help is much appreciated!
解决方案One small typo: your
Load
method should usedes.CreateDecryptor
, like this:public static T Load<T>(string fileSpec) { BinaryFormatter formatter = new BinaryFormatter(); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); using (FileStream stream = File.OpenRead(fileSpec)) { using (CryptoStream cryptoStream = new CryptoStream(stream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read)) { return (T)formatter.Deserialize(cryptoStream); } } }
这篇关于使用BinaryFormatter的问题反序列化加密数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文