⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmappconfig.cs

📁 在设计程序时,需要经常计算一个字串的哈希值,得到加密文本,生成加密过的配置文件或都转换EXCEL文件到SQL数据库等等,根据自己习惯定制部分功能,由于时间关系,没有详细说明
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Security.Cryptography;
using System.IO;

namespace LingangTools
{
    public partial class FrmAppConfig : Form
    {
        public FrmAppConfig()
        {
            InitializeComponent();
        }

        private DataSet ds_Config;
        XmlDocument xml_Config;
        XmlElement xml_Appsettings;
        byte[] Key, IV;

        private void btn_AddKeyValue_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_Key.Text) || string.IsNullOrEmpty(txt_Value.Text))
            {
                MessageBox.Show("键名或值为空,无法添加一对空值", "添加失败");
                return;
            }
            foreach (DataRow r in ds_Config.Tables[0].Rows)
            {
                if (r["key"].ToString() == txt_Key.Text.Trim())
                {
                    MessageBox.Show("键名重复,无法添加", "添加失败");
                    return;
                }
            }
            DataRow dr = ds_Config.Tables[0].NewRow();
            dr["key"] = txt_Key.Text.Trim();
            dr["value"] = txt_Value.Text;
            ds_Config.Tables[0].Rows.Add(dr);
            txt_Key.Text = string.Empty;
            txt_Value.Text = string.Empty;
        }

        private void FrmAppConfig_Load(object sender, EventArgs e)
        {
            ds_Config = new DataSet("App.config");
            DataTable table = new DataTable("configuration");
            table.Columns.Add("key",typeof(string));
            table.Columns.Add("value", typeof(string));
            ds_Config.Tables.Add(table);
            dataGridView_KeyValueList.DataSource = ds_Config;
            dataGridView_KeyValueList.DataMember = "configuration";
            xml_Config = new XmlDocument();
            XmlDeclaration xd = xml_Config.CreateXmlDeclaration("1.0", "utf-8", null);
            XmlElement xe = xml_Config.CreateElement("configuration");
            xml_Config.AppendChild(xd);
            xml_Config.AppendChild(xe);

            xml_Appsettings = xml_Config.CreateElement("appSettings");
            xe.AppendChild(xml_Appsettings);
        }

        private void tsmi_Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView_KeyValueList.SelectedRows.Count != 0)
            {
                int index = dataGridView_KeyValueList.SelectedRows[0].Index;
                ds_Config.Tables[0].Rows[index].Delete();
            }
        }

        private void btn_GenPlainConfig_Click(object sender, EventArgs e)
        {
            foreach (DataRow dr in ds_Config.Tables[0].Rows)
            {
                string strKey = dr["key"].ToString();
                string strValue = dr["value"].ToString();

                XmlElement xe = xml_Config.CreateElement("add");
                XmlAttribute xKey = xml_Config.CreateAttribute("key");
                xKey.Value = strKey;
                XmlAttribute xValue = xml_Config.CreateAttribute("value");
                xValue.Value = strValue;
                xe.Attributes.Append(xKey);
                xe.Attributes.Append(xValue);
                xml_Appsettings.AppendChild(xe);
            }
            FrmTextBox frm = new FrmTextBox();
            frm.MyText = xml_Config.InnerXml;
            frm.ShowDialog();
        }

        private void btn_GenCryptConfig_Click(object sender, EventArgs e)
        {
            GenCryptoKeyAndIV();
            foreach (DataRow dr in ds_Config.Tables[0].Rows)
            {
                string strKey = dr["key"].ToString();
                string strValue = dr["value"].ToString();

                XmlElement xe = xml_Config.CreateElement("add");
                XmlAttribute xKey = xml_Config.CreateAttribute("key");
                xKey.Value = strKey;
                XmlAttribute xValue = xml_Config.CreateAttribute("value");
                xValue.Value = EncryptoText(strValue);
                xe.Attributes.Append(xKey);
                xe.Attributes.Append(xValue);
                xml_Appsettings.AppendChild(xe);
            }
            FrmTextBox frm = new FrmTextBox();
            frm.MyText = xml_Config.InnerXml;
            frm.ShowDialog();
        }

        private void GenCryptoKeyAndIV()
        {
            if (!string.IsNullOrEmpty(txt_Password.Text))
            {
                MD5 md5 = MD5.Create();
                Key = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(txt_Password.Text));
                string textTransfered = txt_Password.Text + "lingang";
                IV = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(textTransfered));                
            }
            else
            {
                MessageBox.Show("初始密码不能为空,请输入密钥后再进行加密操作", "不能为空");
            }
        }

        private string EncryptoText(string plainText)
        {
            byte [] plainBytes = System.Text.Encoding.Default.GetBytes(plainText);
            byte[] cryptoBytes;
            Rijndael sa = Rijndael.Create();
            sa.Key = Key;
            sa.IV = IV;

            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);
            try
            {
                cs.Write(plainBytes, 0, plainBytes.Length);
                cs.Close();
                cryptoBytes = ms.ToArray();
                return System.Convert.ToBase64String(cryptoBytes);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -