通过Cryptography命名空间下的DES加密,将Object序列化为二进制后DES加密、解密。
1using System;
2using System.Security.Cryptography;
3using System.IO;
4using System.Data;
5using System.Runtime.Serialization.Formatters.Binary;
6using System.Text;
7using System.Windows.Forms;
8
9namespace BLL.GenericCls
10{
11 /// <summary>
12 /// 序列化类 的摘要说明。
13 /// </summary>
14 public class SerializationHelper
15 {
16
17 //密钥
18 //获取或设置对称算法的机密密钥。机密密钥既用于加密,也用于解密。为了保证对称算法的安全,必须只有发送方和接收方知道该机密密钥。
19 //有效密钥大小是由特定对称算法实现指定的,密钥大小在 LegalKeySizes 中列出。
20 private static byte[] DESKey = new byte[] {11, 23, 93, 102, 72, 41, 18, 12};
21
22 //获取或设置对称算法的初始化向量
23 private static byte[] DESIV = new byte[] {75, 158, 46, 97, 78, 57, 17, 36};
24
25 /// <summary>
26 /// 将数据序列化为Bin文件
27 /// </summary>
28 /// <param name="data">object类型的数据</param>
29 /// <param name="filePath">路径</param>
30 public static void Serialize(object data, string filePath)
31 {
32 try
33 {
34 DESCryptoServiceProvider objDes=new DESCryptoServiceProvider();//des加密
35 FileStream fout=new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.Write);
36 CryptoStream objcry=new CryptoStream(fout,objDes.CreateEncryptor(DESKey,DESIV),CryptoStreamMode.Write);
37
38 BinaryFormatter formatter=new BinaryFormatter();
39 formatter.Serialize(objcry,data);
40
41 objcry.Close();
42 fout.Close();
43
44 }
45 catch(Exception ex)
46 {
47 MessageBox.Show(ex.Message,"序列化",MessageBoxButtons.OK,MessageBoxIcon.Error);
48 }
49 }
50
51
52 /// <summary>
53 /// 反序列化
54 /// </summary>
55 /// <param name="filePath">路径</param>
56 public static object Deserialize( string filePath)
57 {
58 object data=new object();
59 try
60 {
61 DESCryptoServiceProvider objdes=new DESCryptoServiceProvider();
62 FileStream fs=new FileStream(filePath,FileMode.Open,FileAccess.Read);
63
64 CryptoStream objcry=new CryptoStream(fs,objdes.CreateDecryptor(DESKey,DESIV),CryptoStreamMode.Read);
65
66 BinaryFormatter bf =new BinaryFormatter();
67 data = (object)bf.Deserialize(objcry);
68
69 fs.Close();
70
71 }
72 catch(Exception ex)
73 {
74 MessageBox.Show(ex.Message,"反序列化",MessageBoxButtons.OK,MessageBoxIcon.Error);
75 }
76 return data;
77
78 }
79 }
80}
81
2using System.Security.Cryptography;
3using System.IO;
4using System.Data;
5using System.Runtime.Serialization.Formatters.Binary;
6using System.Text;
7using System.Windows.Forms;
8
9namespace BLL.GenericCls
10{
11 /// <summary>
12 /// 序列化类 的摘要说明。
13 /// </summary>
14 public class SerializationHelper
15 {
16
17 //密钥
18 //获取或设置对称算法的机密密钥。机密密钥既用于加密,也用于解密。为了保证对称算法的安全,必须只有发送方和接收方知道该机密密钥。
19 //有效密钥大小是由特定对称算法实现指定的,密钥大小在 LegalKeySizes 中列出。
20 private static byte[] DESKey = new byte[] {11, 23, 93, 102, 72, 41, 18, 12};
21
22 //获取或设置对称算法的初始化向量
23 private static byte[] DESIV = new byte[] {75, 158, 46, 97, 78, 57, 17, 36};
24
25 /// <summary>
26 /// 将数据序列化为Bin文件
27 /// </summary>
28 /// <param name="data">object类型的数据</param>
29 /// <param name="filePath">路径</param>
30 public static void Serialize(object data, string filePath)
31 {
32 try
33 {
34 DESCryptoServiceProvider objDes=new DESCryptoServiceProvider();//des加密
35 FileStream fout=new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.Write);
36 CryptoStream objcry=new CryptoStream(fout,objDes.CreateEncryptor(DESKey,DESIV),CryptoStreamMode.Write);
37
38 BinaryFormatter formatter=new BinaryFormatter();
39 formatter.Serialize(objcry,data);
40
41 objcry.Close();
42 fout.Close();
43
44 }
45 catch(Exception ex)
46 {
47 MessageBox.Show(ex.Message,"序列化",MessageBoxButtons.OK,MessageBoxIcon.Error);
48 }
49 }
50
51
52 /// <summary>
53 /// 反序列化
54 /// </summary>
55 /// <param name="filePath">路径</param>
56 public static object Deserialize( string filePath)
57 {
58 object data=new object();
59 try
60 {
61 DESCryptoServiceProvider objdes=new DESCryptoServiceProvider();
62 FileStream fs=new FileStream(filePath,FileMode.Open,FileAccess.Read);
63
64 CryptoStream objcry=new CryptoStream(fs,objdes.CreateDecryptor(DESKey,DESIV),CryptoStreamMode.Read);
65
66 BinaryFormatter bf =new BinaryFormatter();
67 data = (object)bf.Deserialize(objcry);
68
69 fs.Close();
70
71 }
72 catch(Exception ex)
73 {
74 MessageBox.Show(ex.Message,"反序列化",MessageBoxButtons.OK,MessageBoxIcon.Error);
75 }
76 return data;
77
78 }
79 }
80}
81