-
DataSet的加密解密
1
using System;
2
using System.Collections;
3
using System.Security.Cryptography;
4
using System.IO ;
5
using System.Data;
6
using System.Xml.Serialization;
7
namespace zjz.ClsDoCode
8![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
10
/// ClsEncryption 的摘要说明。
11
/// </summary>
12
public class ClsEncryption
13![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
14
public ClsEncryption()
15![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
16
//
17
// TODO: 在此处添加构造函数逻辑
18
//
19
}
20
//密钥
21
//获取或设置对称算法的机密密钥。机密密钥既用于加密,也用于解密。为了保证对称算法的安全,必须只有发送方和接收方知道该机密密钥。有效密钥大小是由特定对称算法实现指定的,密钥大小在 LegalKeySizes 中列出。
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static byte[] DESKey = new byte[]
{11, 23, 93, 102, 72, 41, 18, 12};
23
//获取或设置对称算法的初始化向量
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
private static byte[] DESIV = new byte[]
{75, 158, 46, 97, 78, 57, 17, 36};
25
26![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//**//**//// <summary>
27
/// 加密DataSet
28
/// </summary>
29
/// <param name="objDataSet"></param>
30
/// <param name="outFilePath"></param>
31
public static void EncryptDataSetToXml(DataSet objDataSet,string outXmlFilePath)
32![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
33
DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
34
FileStream fout = new FileStream(outXmlFilePath, FileMode.OpenOrCreate, FileAccess.Write);
35
//用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
36
CryptoStream objCryptoStream = new CryptoStream(fout, objDES.CreateEncryptor(DESKey, DESIV), CryptoStreamMode.Write);
37
StreamWriter objStreamWriter = new StreamWriter(objCryptoStream);
38
XmlSerializer objXmlSer = new XmlSerializer(typeof(DataSet));
39
objXmlSer.Serialize(objStreamWriter, objDataSet);
40
objStreamWriter.Close();
41![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
}
43
public static DataSet DecryptDataSetFromXml(string inXmlFilePath)
44![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
45
DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
46
FileStream fin = new FileStream(inXmlFilePath, FileMode.Open , FileAccess.Read );
47
//用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
48
CryptoStream objCryptoStream = new CryptoStream(fin, objDES.CreateDecryptor(DESKey, DESIV), CryptoStreamMode.Read);
49
TextReader objTxrReader = new StreamReader(objCryptoStream);
50
XmlSerializer objXmlSer = new XmlSerializer(typeof(DataSet));
51
DataSet ds = (DataSet)objXmlSer.Deserialize(objTxrReader);
52
return ds;
53
}
54
}
55
}
56
-
相关阅读:
P1064 金明的预算方案
P1062 数列
P2258 子矩阵
P1095 守望者的逃离
P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
P1203 [USACO1.1]坏掉的项链Broken Necklace
P1478 陶陶摘苹果(升级版)
P2485 [SDOI2011]计算器
逆元模板
CloudStack 物理网络架构
-
原文地址:https://www.cnblogs.com/meiproject/p/754836.html
Copyright © 2020-2023
润新知