C#基于AES加密XML序列化以及反序列化

1.设置AES加密解密参数

        private static RijndaelManaged Setting()
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged
            {
                Key = Encoding.UTF8.GetBytes("qwqwqwqwqwqwqwqw"), 
                IV = Encoding.UTF8.GetBytes("asasasasasasasas"),  
                Mode = CipherMode.CBC,       
                Padding = PaddingMode.PKCS7, 
                BlockSize = 128           
            };
            return rijndaelCipher;
        }

2.序列化

        public static bool WriteAESXmlFile<T>(string filePath, T xmlObject)
        {
            try
            {
                string strXml = string.Empty;
                using (MemoryStream stream = new MemoryStream())
                {
                    XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
                    ns.Add(string.Empty, string.Empty);

                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                    xmlSerializer.Serialize(stream, xmlObject, ns);

                    stream.Position = 0;
                    using (StreamReader sr = new StreamReader(stream))
                    {
                        strXml = sr.ReadToEnd();
                    }
                }

                string the64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(strXml));

                byte[] fileData = Convert.FromBase64String(the64);
                if (fileData.Length == 0)
                    return false;

                RijndaelManaged rijndaelCipher = Setting();

                ICryptoTransform transform = rijndaelCipher.CreateEncryptor(); 
                byte[] cipherBytes = transform.TransformFinalBlock(fileData, 0, fileData.Length);

                string contentStr = Convert.ToBase64String(cipherBytes); 
                                                                         
                File.WriteAllText(filePath, contentStr);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

3.反序列化

        public static T LoadAESXmlFile<T>(string filePath)
        {
            try
            {
                if (filePath == string.Empty)
                    return default;

                byte[] fileData = FileReadBytesToEnd(filePath);
                if (fileData.Length == 0)
                    return default;

                string decryptStr = Encoding.UTF8.GetString(fileData);
                fileData = Convert.FromBase64String(decryptStr);

                RijndaelManaged rijndaelCipher = Setting();

                ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); 
                byte[] cipherBytes = transform.TransformFinalBlock(fileData, 0, fileData.Length);

                string contentStr = Encoding.UTF8.GetString(cipherBytes);
                StringReader sr = new StringReader(contentStr);
                XmlSerializer xmldes = new XmlSerializer(typeof(T));
                var obj = (T)xmldes.Deserialize(sr);
                //XmlSerializer xmldesMy = new XmlSerializer(typeof(MyPersons));
                //MyPersons myPersons = (MyPersons)xmldesMy.Deserialize(sr);
                return (T)obj;
            }
            catch (Exception ex)
            {
                return default;
            }
        }

4.涉及函数

        private static byte[] FileReadBytesToEnd(string fp)
        {
            try
            {
                FileStream fs = new FileStream(fp, FileMode.Open, FileAccess.Read);
                byte[] buf = new byte[fs.Length];
                fs.Read(buf, 0, buf.Length);
                if (fs != null)
                    fs.Close();
                return buf;
            }
            catch (Exception ex)
            {

            }
            return new byte[0];
        }

5.个人思考

整体使用AES的字符串加密方式 

序列化:先将对象转为string,对string进行加密,随后写入文件中

反序列化:读取文件的string,对string进行解密,再通过XmlSerializer转为类对象

以上是个人拙见,有更优解还望大佬赐教