从二进制文件反序列化加密对象时,c#异常

时间:2018-09-20 03:09:25

标签: c# encryption serialization deserialization

我有以下代码可以对二进制文件中的解密对象进行序列化和反序列化。编写解密的对象效果很好。但是,当尝试从文件反序列化解密的对象时,在第一个对象成功反序列化之后,该操作将失败。

我希望当我没有想法时,有人可以指出我的错误。

将加密对象写入二进制文件效果很好



    public List ReadAllObjectsFromFile(string pPath)
                {
                    List objList = null;
                    T obj = default(T);
                    using (FileStream stream = new FileStream(pPath, FileMode.Open, FileAccess.Read))
                    {
                        while (true)
                        {
                            if (stream.Length>m_ReaderPosition)
                            {
                                stream.Seek(m_ReaderPosition, SeekOrigin.Begin);
                                if (IsEncrypted)
                                {
                                    using (Stream cryptoStream = new CryptoStream(stream, m_Decryptor, CryptoStreamMode.Read))
                                    {
                                        if (objList == null) objList = new List();
                                        obj = (T)m_Formatter.Deserialize(cryptoStream);
                                    }
                                }
                                else
                                {
                                    if (objList == null) objList = new List();
                                    obj = (T)m_Formatter.Deserialize(stream);
                                }
                                m_ReaderPosition = stream.Position;
                            }

                            if ((IsReadToEnd = object.Equals(obj, default(T)))) break;
                            else
                            {
                                objList.Add(obj);
                                obj = default(T);
                            }
                        }
                    }
                    return objList;
                }

尝试从二进制文件中读取加密对象并将其反序列化为解密对象,这是它尝试反序列化的第二个对象的异常

  

“ System.Runtime.Serialization.SerializationException:'输入   流不是有效的二进制格式。起始内容(以字节为单位)   是:83-AD-D4-BB-F9-7A-4E-34-C2-E7-4F-0C-4F-51-F2-1E-EC ..

该方法成功地反序列化了第一个对象。 这行代码将触发第二个对象的异常。

  

obj =(T)m_Formatter.Deserialize(cryptoStream);



    public List ReadAllObjectsFromFileEnc(string pPath)
            {
                List objList = null;
                T obj = default(T);
                using (FileStream stream = new FileStream(pPath, FileMode.Open, FileAccess.Read))
                {
                    using (Stream cryptoStream = new CryptoStream(stream, m_Decryptor, CryptoStreamMode.Read))
                    {
                        while (true)
                        {
                            if (stream.Length>m_ReaderPosition)
                            {
                                stream.Seek(m_ReaderPosition, SeekOrigin.Begin);
                                if (objList == null) objList = new List();
                                obj = (T)m_Formatter.Deserialize(cryptoStream);
                                m_ReaderPosition = stream.Position;
                            }

                            if ((IsReadToEnd = object.ReferenceEquals(obj, default(T)))) break;
                            else
                            {
                                objList.Add(obj);
                                obj = default(T);
                            }
                        }
                    }
                    return objList;
                }
            }

主要



    static void Main(string[] args)
            {
                List objList = new List();
                objList.Add(
                    new Bear()
                    {
                        Name = "John",
                        Age = 35,
                        Birth = new DateTime(1977, 02, 02),
                        Females = new KeyValuePair("Dove", "Mumu")
                    });
                objList.Add(
                    new Bear()
                    {
                        Name = "Max",
                        Age = 40,
                        Birth = new DateTime(1977, 08, 02),
                        Females = new KeyValuePair("Gr", "Mumu")
                    });
                objList.Add(
                    new Bear()
                    {
                        Name = "Mika",
                        Age = 21,
                        Birth = new DateTime(1990, 02, 02),
                        Females = new KeyValuePair("Dr", "Mumu")
                    });
                objList.Add(
                    new Bear()
                    {
                        Name = "Miles",
                        Age = 90,
                        Birth = new DateTime(1901, 02, 02),
                        Females = new KeyValuePair("SE", "Mumu")
                    });

                BinarySerializer binser = new BinarySerializer(@"E:\Temp\myFile.bin", 10000, true, "My Encryption is here");
                foreach(Bear t in objList)
                    binser.WriteObjectToFile(binser.FileDetails.FullName, t);

                objList = null;

                objList = binser.ReadAllObjectsFromFileEnc(binser.FileDetails.FullName);
            }

这是写入文件



    public void WriteObjectToFile(string pPath, object pObject)
            {
                using (FileStream stream = new FileStream(pPath, FileMode.Append, FileAccess.Write))
                {
                    if (IsEncrypted)
                    {
                        using (Stream cryptoStream = new CryptoStream(stream, m_Encryptor, CryptoStreamMode.Write))
                        {
                            // 3. write to the cryptoStream
                            m_Formatter.Serialize(cryptoStream, pObject);
                        }
                    }
                    else
                        m_Formatter.Serialize(stream, pObject);
                }
            }

0 个答案:

没有答案