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<Information> serList = new List<Information>();
        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<Information> serTest = XMLDeserialize<List<Information>>(path);
        List<Information> serTest = GeneralDeserialize<List<Information>>(path, keyVal, ivVal);
 
        //输出返回的值
        foreach (var temp in serTest)
        {
            Debug.Log(temp.name);
            Debug.Log(temp.age);
        }
    }
 
    /// <summary>  
    /// 加密序列化  
    /// </summary>  
    /// <param name="obj">要序列化的对象</param>  
    /// <param name="path">保存路径</param>  
    /// <param name="key">密钥值</param>  
    /// <param name="iv">加密辅助向量</param> 
    /// <returns></returns>  
 
    void GeneralSerialize<T>(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();
        }
    }
 
    /// <summary>  
    /// 加密反序列化  
    /// </summary>  
    /// <param name="path">路径</param>  
    /// <param name="key">密钥值</param>  
    /// <param name="iv">加密辅助向量</param> 
    /// <returns></returns>  
 
    T GeneralDeserialize<T>(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>(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<T>(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算法

2017年09月20日 5.04MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭