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() { }
}
DES AES加密数据
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一、简述DES的不安全性以及DESede算法的低效,催生了这个AES算法(advanced Encryption ...
- Base64 base64是一种基于64个可打印字符来表示二进制数据的表示方法.严格来说它只能算作一种编码方式.B...
- 一、前后端要做到安全的数据交互通常需要了解以下几个技术: 1、对称加密:指加密和解密使用相同密钥的加密算法,速度较...
- 在服务器与终端设备进行HTTP通讯时,常常会被网络抓包、反编译(Android APK反编译工具)等技术得到HTT...
- 题目描述 如题,已知一个数列,你需要进行下面两种操作:1.将某区间每一个数加上x2.将某区间每一个数乘上x3.求出...