[C#] 纯文本查看 复制代码
[/color][/size][/b]
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;
namespace XML_Serialize
{
class Program
{
//keyVal输入32位,ivVal为16位
static string keyVal = "qsedcvfrgthjuiklopoiuytrfgbvcdew";
static string ivVal = "124578963axswedr";
static void Main(string[] args)
{
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)
{
Console.WriteLine(temp.name);
Console.WriteLine(temp.age);
}
Console.ReadKey();
}
/// <summary>
/// 加密序列化
/// </summary>
/// <param name="obj">要序列化的对象</param>
/// <param name="path">保存路径</param>
/// <param name="key">密钥值</param>
/// <param name="iv">加密辅助向量</param>
/// <returns></returns>
static 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>
static 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;
}
}
//未加密序列化
static 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();
}
//未加密反序列化
static 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()
{
}
}
[b][size=4][color=#ff0000]