public class DataSetFormat
{
/// <summary>
/// 序列化DataSet
/// </summary>
/// <param name="dataSet">需要序列化的DataSet</param>
/// <returns>序列化后的byte数组</returns>
public static byte[] DataSetToByte(DataSet dataSet)
{
//通过dataset创建DataSetSurrogate对象
DataSetSurrogate surrogate = new DataSetSurrogate(dataSet);
MemoryStream str = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
//将DataSetSurrogate实例转化成内存流
bf.Serialize(str, surrogate);
return str.ToArray();
}
/// <summary>
/// 反序列化DataSet
/// </summary>
/// <param name="dsByte">Byte数组</param>
/// <returns>DataSet</returns>
public static DataSet ByteToDataSet(byte[] dsByte)
{
//将byte数组转化成内存流
MemoryStream str = new MemoryStream(dsByte);
BinaryFormatter bf = new BinaryFormatter();
//将内存流转化成object对象
object o = bf.Deserialize(str);
//强行转化成DataSetSurrogate实例
DataSetSurrogate surrogate = (DataSetSurrogate)o;
//通过DataSetSurrogate转化成DataSet
return surrogate.ConvertToDataSet();
}
}
/// <summary>
/// 加密类
/// </summary>
public class Encrypt
{
#region 加密参数
/// <summary>
/// DES密钥
/// </summary>
private static string KEY = "PQTadmin";
/// <summary>
/// DES密鈅
/// </summary>
public static string Key
{
set
{
if (KEY != value)
{
KEY = value;
}
}
}
/// <summary>
/// DES初始化向量
/// </summary>
private static string IV = "PQTUnica";
/// <summary>
/// DES初始化向量
/// </summary>
public static string Iv
{
set
{
if (IV != value)
{
IV = value;
}
}
}
#endregion
#region MD5算法
/// <summary>
/// 采用MD5加密方式,不可逆
/// </summary>
/// <param name="buffer">加密字符串</param>
/// <returns>加密后串</returns>
public static string MD5Encrypt(string buffer)
{
// 创建MD对象
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
// 加密字符串
byte[] encryptBytes = md5.ComputeHash(Encoding.ASCII.GetBytes(buffer));
//对加密出来的byte转换成字符串
StringBuilder sb = new StringBuilder();
for (int i = 0; i < encryptBytes.Length; i++)
{
sb.AppendFormat("{0:x2}", encryptBytes[i]);
}
return sb.ToString();
}
#endregion
#region DES算法
/// <summary>
/// 采用DES加密算法
/// </summary>
/// <param name="buffer">被加密串</param>
/// <returns></returns>
public static string DESEncrypt(string buffer)
{
// 定义对称算法类的实例
SymmetricAlgorithm symmetric;
// 加密接口实例
ICryptoTransform crypto;
MemoryStream mstream;
// 加密流
CryptoStream cstream;
byte[] bufferbyte;
// 设置SymmetricAlgorithm参数
symmetric = new DESCryptoServiceProvider();
symmetric.Key = Encoding.UTF8.GetBytes(KEY);
symmetric.IV = Encoding.UTF8.GetBytes(IV);
// 初始化ICryptoTransform接口
crypto = symmetric.CreateEncryptor();
// 将被加密串二进制化
bufferbyte = Encoding.UTF8.GetBytes(buffer);
// 通过ICryptoTransform实例创建加密流对象
mstream = new MemoryStream();
cstream = new CryptoStream(mstream, crypto, CryptoStreamMode.Write);
// 加密数据
cstream.Write(bufferbyte, 0, bufferbyte.Length);
cstream.FlushFinalBlock();
cstream.Close();
// 返回加密后的串
return Convert.ToBase64String(mstream.ToArray());
}
/// <summary>
/// 采用DES解密算法
/// </summary>
/// <param name="buffer">被DES加密的串</param>
/// <returns>解密后的串</returns>
public static string DESDecrypt(string buffer)
{
// 定义对称算法类的实例
SymmetricAlgorithm symmetric;
// 加密接口实例
ICryptoTransform crypto;
MemoryStream mstream;
// 加密流
CryptoStream cstream;
byte[] bufferbyte;
// 设置SymmetricAlgorithm参数
symmetric = new DESCryptoServiceProvider();
symmetric.Key = Encoding.UTF8.GetBytes(KEY);
symmetric.IV = Encoding.UTF8.GetBytes(IV);
// 初始化ICryptoTransform接口
crypto = symmetric.CreateDecryptor();
// 将串转化成byte数组
bufferbyte = Convert.FromBase64String(buffer);
// 通过ICryptoTransform实例创建加密流对象
mstream = new MemoryStream();
cstream = new CryptoStream(mstream, crypto, CryptoStreamMode.Write);
// 加密数据
cstream.Write(bufferbyte, 0, bufferbyte.Length);
cstream.FlushFinalBlock();
cstream.Close();
// 返回解密后的串
return Encoding.UTF8.GetString(mstream.ToArray());
}
#endregion
}