读取本地xml进行DES加密、解密

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace JHEMR.JHOODCommonLib.Utility
{
    /// <summary>
    /// DES加密、解密
    /// </summary>
    public static class SecurityDES
    {
        //默认密钥向量  
        private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
        /// <summary>
        /// DES加密字符串       
        /// </summary>
        /// <param name="encryptString"> 待加密的字符串  </param>
        /// <param name="encryptKey">加密密钥,要求为8位 </param>
        /// <returns>加密成功返回加密后的字符串,失败返回空字符  </returns>
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowInfo(ex.Message.ToString());
                return string.Empty;
            }
        }
        /// <summary>
        /// DES解密字符串  
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey"> 解密密钥,要求为8位,和加密密钥相同 </param>
        /// <returns>解密成功返回解密后的字符串,失败返空 </returns>
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch (Exception ex)
            {
                MessageBoxHelper.ShowInfo(ex.Message.ToString());
                return string.Empty;
            }
        }  
    }
}
     private void btn_leadingout_Click(object sender, EventArgs e)
        {
            string localFilePath = "";
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "XML(*.XML)|*.XML";
            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;
            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;

            //点了保存按钮进入 
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                localFilePath = sfd.FileName.ToString(); //获得文件路径 
                //string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名,不带路径
                try
                {
                    StreamWriter sw = new StreamWriter(localFilePath, false);

                    if (_dtDiagnosis.Rows.Count > 0)
                    {
                           DataRow[] r= _dtDiagnosis.Select("selected");
                           if (r.Count() <= 0)
                           {
                               MessageBox.Show("请在要导出行的复选框中打钩。");
                               sw.Close();
                               return;
                           }
                           DataTable dt = r.CopyToDataTable();
                        dt.TableName = "Diagnosis";
                        System.IO.TextWriter tw = new System.IO.StringWriter();
                        dt.WriteXml(tw);
                        string str = SecurityDES.EncryptDES(tw.ToString(), "12345678");
                        if(string.IsNullOrEmpty(str))
                        {
                            MessageBoxHelper.ShowInfo("导出失败,请重试。");
                             sw.Close();
                            return;
                        }
                        sw.WriteLine(str);
                    }
                    sw.Close();
                    MessageBoxHelper.ShowInfo("导出成功");
                }
                catch(Exception f)
                {
                    MessageBoxHelper.ShowInfo("导出失败,请重试");

                }
            }
        }

        private void btn_leadingin_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "XML";
            fileDialog.Filter = "XML(*XML*)|*.XML*"; //设置要选择的文件的类型
            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                string file = fileDialog.FileName;//返回文件的完整路径  
                string strTxtAll = "";   
                FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.None);
                StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("UTF-8"));
                string line = sr.ReadLine();
                strTxtAll = line + "\r\n";
                for (int ac = 0; line != null; ac++)
                {
                    line = sr.ReadLine();
                    strTxtAll += line + "\r\n";
                }
                string xml = SecurityDES.DecryptDES(strTxtAll, "12345678");
                if (string.IsNullOrEmpty(xml))
                {
                    MessageBoxHelper.ShowInfo("导入失败,请重试。");
                    sr.Close();
                    fs.Close();
                    return;
                }
                StringReader stream = null;
                XmlTextReader reader = null;
                try
                {
                    DataSet xmlDS = new DataSet();
                    stream = new StringReader(xml);
                    reader = new XmlTextReader(stream);
                    xmlDS.ReadXml(reader);
                    reader.Close();
                    _bl.DiagnoisLeadingin(xmlDS.Tables[0]);
                    //todo'''''
                }
                catch (Exception ex)
                {
                    MessageBoxHelper.ShowInfo("导入失败,请重试。");
                    reader.Close();
                }
                sr.Close();
                fs.Close();
            }
        }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
xml加密XML Encryption)是w3c加密xml的标准。这个加密过程包括加密xml文档的元素及其子元素,通过加密xml的初始内容将被替换,但其xml格式仍然被完好的保留。 介绍 我们有3个加密xml的方法 1、仅仅使用对称加密的方法加密xml 这种加密方法只使用一个密钥,也就是说无论是加密xml还是解密xml都使用一个相同的密钥。因为这个密钥不会在被加密xml中保存,所以我们需要在加密解密的过程中加载这个密钥并保护它不被窃取。 2、使用对称加密和非对称加密相结合的方法来加密xml 这种方法需要一个用于加密数据的对称密钥和一个用于保护这个对称密钥的非对称密钥。被加密的对称密钥和被加密的数据一起保存在xml文档中。当用私有非对称密钥解密密钥的时候要用公开非对称密钥对密钥进行加密。 本文就将使用这种方法。想学到其他更多的方法请参看MSDN等到更多的信息。 (译者注:非对称加密算法需要两个密钥:公开密钥(publickey)和私有密钥(privatekey)。公开密钥与私有密钥是一对,如果用公开密钥对数据进行加密,只有用对应的私有密钥才能解密;如果用私有密钥对数据进行加密,那么只有用对应的公开密钥才能解密。因为加密解密使用的是两个不同的密钥,所以这种算法叫作非对称加密算法。) 3、使用X.509加密xml,这种方法是用X.509作为非对称密钥,它由诸如VeriSign之类的第三方提供。 方法 不管xml加密是如何完成的,保存加密数据总是用两种方法之一。 1、加密后所有的元素都被命名为 2、加密后只有数据被替换,而元素名称仍然是可读的,不会发生变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值