读取本地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();
            }
        }

猜你喜欢

转载自blog.csdn.net/lz37025/article/details/80484984