反序列化 c#

标签: C#
发布时间: 2013/12/4 20:34:37
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 WPF 应用程序中包含此类: *

{[Serializable]
public class Parametres
{
    private string admin_login;
    private string admin_pwd;
    private string server;
    private string db;
    private string user;
    private string pwd;}

我将此函数与对象序列:

  public static void Serialize_Parametres(string filename, Parametres obj)
    {
        using (FileStream fs = File.Open(filename, FileMode.OpenOrCreate))
        {
            using (CryptoStream cs = new CryptoStream(fs, key.CreateEncryptor(), CryptoStreamMode.Write))
            {
                XmlSerializer xmlser = new XmlSerializer(typeof(Parametres));
                xmlser.Serialize(cs, obj);
            }
        }
    }

它很好的作品和它生成文件.txt,但当我试着反序列化此文件并得到对象参数应用连此函数:

 public static Parametres DeSerialize_Parametres(string filename)
        {
            using (FileStream fs = File.Open(filename, FileMode.Open))
            {

              using (CryptoStream cs = new CryptoStream(fs, key.CreateDecryptor(), CryptoStreamMode.Read))

                {

                    XmlSerializer xmlser = new XmlSerializer(typeof(Parametres));
                    return (Parametres)xmlser.Deserialize(cs);

                }
            }


        }

我有此错误 Length of the data to decrypt is invalid 行中return (Parametres)xmlser.Deserialize(cs);

正是此错误的原因是什么?如何解决该问题?

解决方法 1:

使用此技术要序列化一个对象时,你要做两个部分。长度加密的流必须将存储作为最后流和其由你来执行此操作的一部分。但是,你应该进更多 resuable 窗体分手这。

例如,第一次 serialze 图你想为字节流 ;然后 Encrypte 字节流的形式 ;然后将它保存到一个文件。

下面是一个示例如何对序列化到文件中使用 AES:

    public class ObjectXmlSerializer
    {
        //---------------------------------------------------------------------
        public override Byte[] Serialize(Object obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                new XmlSerializer(obj.GetType()).Serialize(ms, obj);
                return ms.ToArray();
            }
        }
        //---------------------------------------------------------------------
        public override T Deserialize<T>(Byte[] bObj)
        {
            using (MemoryStream ms = new MemoryStream(bObj))
            {
                return (T)new XmlSerializer(typeof(T)).Deserialize(ms);
            }
        }
        //---------------------------------------------------------------------
        public override T Deserialize<T>(Stream iostream)
        {
            return (T)new XmlSerializer(typeof(T)).Deserialize(iostream);
        }
    }

/ / 下一步

public static class CryptoSerivces
{
        //---------------------------------------------------------------------
        public static Byte[] AesEncrypt(Byte[] src, Byte[] key, Byte[] IV)
        {
            using (RijndaelManaged myRijndael = new RijndaelManaged())
            {
                try
                {
                    myRijndael.Mode = CipherMode.CBC;
                    myRijndael.Key = key;
                    myRijndael.IV = IV;
                    myRijndael.Padding = PaddingMode.PKCS7;

                    using (ICryptoTransform encryptor = myRijndael.CreateEncryptor())
                    using (MemoryStream msEncrypt = new MemoryStream())
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        csEncrypt.Write(src, 0, src.Length);
                        csEncrypt.FlushFinalBlock();

                        return msEncrypt.ToArray();
                    }
                }
                finally
                {
                    myRijndael.Clear();
                }
            }
        }
        //---------------------------------------------------------------------
        public static Byte[] AesDecrypt(Byte[] src, Byte[] key, Byte[] IV)
       {
        using (RijndaelManaged myRijndael = new RijndaelManaged())
        {
            try
            {
                myRijndael.Mode = CipherMode.CBC;
                myRijndael.Key = key;
                myRijndael.IV = IV;
                myRijndael.Padding = PaddingMode.PKCS7;

                using (ICryptoTransform decryptor = myRijndael.CreateDecryptor())
                using (MemoryStream msDecrypt = new MemoryStream())
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write))
                {
                    csDecrypt.Write(src, 0, src.Length);
                    csDecrypt.FlushFinalBlock();

                    return msDecrypt.ToArray();
                }
            }
            finally
            {
                myRijndael.Clear();
            }
        }
    }
}

/ / 将所有碎片拼凑

void SaveToFile(String fileName, Parametres obj)
{
   ObjectXmlSerializer oxs = new ObjectXmlSerializer();
   Byte[] bObj = oxs.Serialize(obj);
   Byte[] bEncObj = CryptoSerivces.AesEncrypt(bObj, SomeKey, SomeIV);

   using (FileStream fs = File.Open(filename, FileMode.OpenOrCreate))
   {
      fs.Write(bEncObj, 0, bEncObj.Length);
   }
}

/ / 我就会离开你读。

官方微信
官方QQ群
31647020