Unity DES AES加密数据 序列化和反序列化 通用方法

using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
 
public class SerializeTest : MonoBehaviour
{
    //keyVal输入32位,ivVal为16位
    static string keyVal = "qsedcvfrgthjuiklopoiuytrfgbvcdew";
    static string ivVal = "124578963axswedr";
 
    void Start()
    {
        List serList = new List();
        string path = @"Test.xml";
 
        //赋值
        for (int i = 0; i < 5; i++)
        {
            serList.Add(new Information("名字" + i, 20 + i));
        }
 
        GeneralSerialize(serList, path, keyVal, ivVal);
        //XMLSerialize(serList, path);
        //List serTest = XMLDeserialize>(path);
        List serTest = GeneralDeserialize>(path, keyVal, ivVal);
 
        //输出返回的值
        foreach (var temp in serTest)
        {
            Debug.Log(temp.name);
            Debug.Log(temp.age);
        }
    }
 
    ///   
    /// 加密序列化  
    ///   
    /// 要序列化的对象  
    /// 保存路径  
    /// 密钥值  
    /// 加密辅助向量 
    ///   
 
    void GeneralSerialize(T obj, string path, string key, string iv)
    {
        if (string.IsNullOrEmpty(path)) return;
        //32位
        key = "qwertyuiopasdfghqwertyuiopasdfgh";
        //16位
        iv = "qwertyuiopasdfgh";
         
        using (var fileStream = File.Create(path))
        {
            //AES加密
            Rijndael aes = Rijndael.Create();
            aes.Key = Encoding.UTF8.GetBytes(ivVal.Length >= 32 ? ivVal.Substring(0, 32) : key);
            aes.IV = Encoding.UTF8.GetBytes(ivVal.Length >= 16 ? ivVal.Substring(0, 16) : iv);
 
            //DES加密
            //DESCryptoServiceProvider des = new DESCryptoServiceProvider()
            //{
            //    Key = Encoding.ASCII.GetBytes(keyVal.Length > 8 ? keyVal.Substring(0, 8) : key.Substring(0, 8)),
            //    IV = Encoding.ASCII.GetBytes(ivVal.Length > 8 ? ivVal.Substring(0, 8) : iv.Substring(0, 8))
            //};
 
            //加密流,如果是AES加密aes.CreateEncryptor(),DES加密des.CreateEncryptor()
            CryptoStream crStream = new CryptoStream(fileStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
            XmlSerializer xs = new XmlSerializer(typeof(T));
            xs.Serialize(crStream, obj);
            aes.Clear();
            crStream.Close();
            fileStream.Close();
        }
    }
 
    ///   
    /// 加密反序列化  
    ///   
    /// 路径  
    /// 密钥值  
    /// 加密辅助向量 
    ///   
 
    T GeneralDeserialize(string path, string key, string iv)
    {
        if (string.IsNullOrEmpty(path)) return default(T);
        //32位
        key = "qwertyuiopasdfghqwertyuiopasdfgh";
        //16位
        iv = "qwertyuiopasdfgh";
         
        using (var fileStream = File.OpenRead(path))
        {
            //AES加密
            Rijndael aes = Rijndael.Create();
            aes.Key = Encoding.UTF8.GetBytes(ivVal.Length >= 32 ? ivVal.Substring(0, 32) : key);
            aes.IV = Encoding.UTF8.GetBytes(ivVal.Length >= 16 ? ivVal.Substring(0, 16) : iv);
 
            //DES加密
            //DESCryptoServiceProvider des = new DESCryptoServiceProvider()
            //{
            //    Key = Encoding.ASCII.GetBytes(keyVal.Length > 8 ? keyVal.Substring(0, 8) : key.Substring(0, 8)),
            //    IV = Encoding.ASCII.GetBytes(ivVal.Length > 8 ? ivVal.Substring(0, 8) : iv.Substring(0, 8))
            //};
 
            //加密流,如果是AES加密aes.CreateEncryptor(),DES加密des.CreateEncryptor()
            CryptoStream crStream = new CryptoStream(fileStream, aes.CreateDecryptor(), CryptoStreamMode.Read);
            XmlSerializer xs = new XmlSerializer(typeof(T));
            T retObj = (T)xs.Deserialize(crStream);
            aes.Clear();
            crStream.Close();
            fileStream.Close();
            return retObj;
        }
    }
 
 
    //未加密序列化
    void XMLSerialize(T obj, string path)
    {
        if (string.IsNullOrEmpty(path)) return;
 
        XmlSerializer xs = new XmlSerializer(typeof(T));
        Stream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
        xs.Serialize(fs, obj);
        fs.Flush();
        fs.Close();
        fs.Dispose();
    }
 
    //未加密反序列化
    T XMLDeserialize(string path)
    {
        if (string.IsNullOrEmpty(path)) return default(T);
 
        XmlSerializer xs = new XmlSerializer(typeof(T));
        Stream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
        T serTest = (T)xs.Deserialize(fs);
        fs.Flush();
        fs.Close();
        fs.Dispose();
        return serTest;
    }
}
 
[XmlType("人员信息")]
public class Information
{
    [XmlAttribute("名字")]
    public string name;
 
    [XmlAttribute("年龄")]
    public int age;
    public Information(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
 
    //必须要有
    public Information() { }
}

你可能感兴趣的:(Unity DES AES加密数据 序列化和反序列化 通用方法)