在上次DataSet的加密解密一文发布后,有网友提出采用XML序列化的方式速度较慢,建议用二进制序列化。我觉得很有道理,下面是在VS2005种采用二进制序列化方式结合对称加密算法对DataSet进行加密解密的代码:
1
using System;
2
using System.Collections;
3
using System.Security.Cryptography;
4
using System.IO ;
5
using System.Data;
6
using System.Runtime.Serialization.Formatters.Binary;
7
namespace zjz.ClsDoCode
8
{
9
/// <summary>
10
/// ClsEncryption 的摘要说明。
11
/// </summary>
12
public class ClsEncryption
13
{
14
public ClsEncryption()
15
{
16
//
17
// TODO: 在此处添加构造函数逻辑
18
//
19
}
20
//密钥
21
//获取或设置对称算法的机密密钥。机密密钥既用于加密,也用于解密。为了保证对称算法的安全,必须只有发送方和接收方知道该机密密钥。有效密钥大小是由特定对称算法实现指定的,密钥大小在 LegalKeySizes 中列出。
22
private static byte[] DESKey = new byte[] {11, 23, 93, 102, 72, 41, 18, 12};
23
//获取或设置对称算法的初始化向量
24
private static byte[] DESIV = new byte[] {75, 158, 46, 97, 78, 57, 17, 36};
25![](/Images/OutliningIndicators/InBlock.gif)
26
public static void EncryptDataSetToBinary(DataSet objDataSet, string outFilePath)
27
{
28
DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
29
FileStream fout = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
30
//用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
31
CryptoStream objCryptoStream = new CryptoStream(fout, objDES.CreateEncryptor(DESKey, DESIV), CryptoStreamMode.Write);
32
//StreamWriter objStreamWriter = new StreamWriter(objCryptoStream);
33
objDataSet.RemotingFormat = SerializationFormat.Binary;
34
BinaryFormatter bf = new BinaryFormatter();
35
bf.Serialize(objCryptoStream, objDataSet);
36
objCryptoStream.Close();
37
fout.Close();
38![](/Images/OutliningIndicators/InBlock.gif)
39
}
40
public static DataSet DecryptDataSetFromBinary(string inBinFilePath)
41
{
42
DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
43
FileStream fin = new FileStream(inBinFilePath, FileMode.Open, FileAccess.Read);
44
//用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
45
CryptoStream objCryptoStream = new CryptoStream(fin, objDES.CreateDecryptor(DESKey, DESIV), CryptoStreamMode.Read);
46
BinaryFormatter bf = new BinaryFormatter();
47
DataSet ds = (DataSet)bf.Deserialize(objCryptoStream);
48
fin.Close();
49
return ds;
50
}
51
}
52
}
![](/Images/OutliningIndicators/None.gif)
2
![](/Images/OutliningIndicators/None.gif)
3
![](/Images/OutliningIndicators/None.gif)
4
![](/Images/OutliningIndicators/None.gif)
5
![](/Images/OutliningIndicators/None.gif)
6
![](/Images/OutliningIndicators/None.gif)
7
![](/Images/OutliningIndicators/None.gif)
8
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](/Images/OutliningIndicators/InBlock.gif)
11
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
12
![](/Images/OutliningIndicators/InBlock.gif)
13
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](/Images/OutliningIndicators/InBlock.gif)
15
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](/Images/OutliningIndicators/InBlock.gif)
17
![](/Images/OutliningIndicators/InBlock.gif)
18
![](/Images/OutliningIndicators/InBlock.gif)
19
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
20
![](/Images/OutliningIndicators/InBlock.gif)
21
![](/Images/OutliningIndicators/InBlock.gif)
22
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
23
![](/Images/OutliningIndicators/InBlock.gif)
24
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
25
![](/Images/OutliningIndicators/InBlock.gif)
26
![](/Images/OutliningIndicators/InBlock.gif)
27
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
28
![](/Images/OutliningIndicators/InBlock.gif)
29
![](/Images/OutliningIndicators/InBlock.gif)
30
![](/Images/OutliningIndicators/InBlock.gif)
31
![](/Images/OutliningIndicators/InBlock.gif)
32
![](/Images/OutliningIndicators/InBlock.gif)
33
![](/Images/OutliningIndicators/InBlock.gif)
34
![](/Images/OutliningIndicators/InBlock.gif)
35
![](/Images/OutliningIndicators/InBlock.gif)
36
![](/Images/OutliningIndicators/InBlock.gif)
37
![](/Images/OutliningIndicators/InBlock.gif)
38
![](/Images/OutliningIndicators/InBlock.gif)
39
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
40
![](/Images/OutliningIndicators/InBlock.gif)
41
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
42
![](/Images/OutliningIndicators/InBlock.gif)
43
![](/Images/OutliningIndicators/InBlock.gif)
44
![](/Images/OutliningIndicators/InBlock.gif)
45
![](/Images/OutliningIndicators/InBlock.gif)
46
![](/Images/OutliningIndicators/InBlock.gif)
47
![](/Images/OutliningIndicators/InBlock.gif)
48
![](/Images/OutliningIndicators/InBlock.gif)
49
![](/Images/OutliningIndicators/InBlock.gif)
50
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
51
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
52
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)