DataSet的加密解密

        最近在一个工程中需要对DataSet进行加密,然后以文件形式传输后在另一端解密还原。于是查询了一些资料,发现多是对字符串和文件加密的方法。于是只好自力更生,参考.net的对称加密技术和网上的一些资料,研究出了DataSet的加密方法。
    代码比较简单,附下:

 1 None.gif    using  System;
 2 None.gif   using  System.Collections;
 3 None.gif   using  System.Security.Cryptography; 
 4 None.gif   using  System.IO ;
 5 None.gif   using  System.Data;
 6 None.gif   using  System.Xml.Serialization;
 7 None.gif   namespace  zjz.ClsDoCode
 8 ExpandedBlockStart.gifContractedBlock.gif   dot.gif {
 9ExpandedSubBlockStart.gifContractedSubBlock.gif      /**//**//**//// <summary>
10InBlock.gif    /// ClsEncryption 的摘要说明。
11ExpandedSubBlockEnd.gif    /// </summary>

12InBlock.gif     public class ClsEncryption
13ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
14InBlock.gif        public ClsEncryption()
15ExpandedSubBlockStart.gifContractedSubBlock.gif         dot.gif{
16InBlock.gif             //
17InBlock.gif            // TODO: 在此处添加构造函数逻辑
18InBlock.gif             //
19ExpandedSubBlockEnd.gif         }

20InBlock.gif         //密钥
21InBlock.gif        //获取或设置对称算法的机密密钥。机密密钥既用于加密,也用于解密。为了保证对称算法的安全,必须只有发送方和接收方知道该机密密钥。有效密钥大小是由特定对称算法实现指定的,密钥大小在 LegalKeySizes 中列出。
22ExpandedSubBlockStart.gifContractedSubBlock.gif         private static byte[] DESKey = new byte[] dot.gif{11239310272411812};
23InBlock.gif         //获取或设置对称算法的初始化向量
24ExpandedSubBlockStart.gifContractedSubBlock.gif         private static byte[] DESIV = new byte[] dot.gif{75158469778571736};
25InBlock.gif 
26ExpandedSubBlockStart.gifContractedSubBlock.gif          /**//**//**//// <summary>
27InBlock.gif         /// 加密DataSet
28InBlock.gif        /// </summary>
29InBlock.gif         /// <param name="objDataSet"></param>
30ExpandedSubBlockEnd.gif        /// <param name="outFilePath"></param>

31InBlock.gif        public static void EncryptDataSetToXml(DataSet objDataSet,string outXmlFilePath)
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33InBlock.gif            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
34InBlock.gif            FileStream fout = new FileStream(outXmlFilePath, FileMode.OpenOrCreate, FileAccess.Write);
35InBlock.gif            //用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
36InBlock.gif            CryptoStream objCryptoStream = new CryptoStream(fout, objDES.CreateEncryptor(DESKey, DESIV), CryptoStreamMode.Write);
37InBlock.gif            StreamWriter objStreamWriter = new StreamWriter(objCryptoStream);
38InBlock.gif            XmlSerializer objXmlSer = new XmlSerializer(typeof(DataSet));
39InBlock.gif            objXmlSer.Serialize(objStreamWriter, objDataSet);
40InBlock.gif            objStreamWriter.Close();
41InBlock.gif
42ExpandedSubBlockEnd.gif        }

43InBlock.gif        public static DataSet DecryptDataSetFromXml(string inXmlFilePath)
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45InBlock.gif            DESCryptoServiceProvider objDES = new DESCryptoServiceProvider();
46InBlock.gif            FileStream fin = new FileStream(inXmlFilePath, FileMode.Open , FileAccess.Read );
47InBlock.gif            //用指定的 Key 和初始化向量 (IV) 创建对称数据加密标准 (DES) 加密器对象
48InBlock.gif           CryptoStream objCryptoStream = new CryptoStream(fin, objDES.CreateDecryptor(DESKey, DESIV), CryptoStreamMode.Read);
49InBlock.gif            TextReader objTxrReader = new StreamReader(objCryptoStream);
50InBlock.gif            XmlSerializer objXmlSer = new XmlSerializer(typeof(DataSet));
51InBlock.gif            DataSet ds = (DataSet)objXmlSer.Deserialize(objTxrReader);
52InBlock.gif            return ds;
53ExpandedSubBlockEnd.gif        }

54ExpandedSubBlockEnd.gif    }

55ExpandedBlockEnd.gif}

56 None.gif
        小结:关于对称加密算法,大家可以参考微软的资料和网上的资料,这里就不多做解释了。下面是源代码文件,里面还包含有字符串和文件加密的方法,当然那些是别人的代码。
/Files/zjzkiss/ClsDoCode.rar

转载于:https://www.cnblogs.com/zjzkiss/archive/2006/06/11/423446.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值