• 加密解密实例分析


    public class GetMd5Encrypt

    {

          ///


            /// DES数据加密 通过DESCryptoServiceProvider对象对字符串进行加密解密
            ///

            /// 目标值
            /// 密钥
            /// 加密值
            public static string Encrypt(string targetValue, string key)
            {
                if (string.IsNullOrEmpty(targetValue))
                {
                    return string.Empty;
                }

                var returnValue = new StringBuilder();
                var des = new DESCryptoServiceProvider();
                byte[] inputByteArray = Encoding.Default.GetBytes(targetValue);
                // 通过两次哈希密码设置对称算法的初始化向量   
                des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
                                                        (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
                                                            Substring(0, 8), "sha1").Substring(0, 8));
                // 通过两次哈希密码设置算法的机密密钥   
                des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
                                                        (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
                                                            .Substring(0, 8), "md5").Substring(0, 8));
                var ms = new MemoryStream();
                var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                foreach (byte b in ms.ToArray())
                {
                    returnValue.AppendFormat("{0:X2}", b);
                }
                return returnValue.ToString();
            }

            ///


            /// DES数据解密
            ///

            ///
            ///
            ///
            public static string Decrypt(string targetValue, string key)
            {
                if (string.IsNullOrEmpty(targetValue))
                {
                    return string.Empty;
                }
                // 定义DES加密对象
                var des = new DESCryptoServiceProvider();
                int len = targetValue.Length / 2;
                var inputByteArray = new byte[len];
                int x, i;
                for (x = 0; x < len; x++)
                {
                    i = Convert.ToInt32(targetValue.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }
                // 通过两次哈希密码设置对称算法的初始化向量   
                des.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
                                                        (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5").
                                                            Substring(0, 8), "sha1").Substring(0, 8));
                // 通过两次哈希密码设置算法的机密密钥   
                des.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile
                                                        (FormsAuthentication.HashPasswordForStoringInConfigFile(key, "md5")
                                                            .Substring(0, 8), "md5").Substring(0, 8));
                // 定义内存流
                var ms = new MemoryStream();
                // 定义加密流
                var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Encoding.Default.GetString(ms.ToArray());
            }
        }

    }

    加密按钮示例:

    private void btnPwdMd5_Click(object sender, EventArgs e)
            {
                string DBLink_No = Convert.ToString(this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "DBLink_No") == null ? "" : this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "DBLink_No"));
                string PWD = Convert.ToString(this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "PWD") == null ? "" : this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "PWD"));
                bool isPWD_Encyption = Convert.ToBoolean(this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "isPWD_Encyption") == null ? "0" : this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "isPWD_Encyption"));
                if (isPWD_Encyption == true)
                {
                    MessageBox.Show("该密码已经加密过,不需再加密!");
                }
                else
                {
                    //string PWD_Encyption = CommonHelper.Md5Encrypt(PWD);  //这个加密后不能解密
                    string PWD_Encyption = GetMd5Encrypt.Encrypt(PWD, "88");
                    this.txtPWD.Text = PWD_Encyption;
                    ProcSqlHelper.ExecuteNonQuery(@"update MultilayerDataInterface_DBLink set isPWD_Encyption=1,PWD=@PWD_Encyption where DBLink_No = @DBLink_No", CommandType.Text, new SqlParameter[] { new SqlParameter("@DBLink_No", DBLink_No), new SqlParameter("@PWD_Encyption", PWD_Encyption) });
                    AuditDBLink();
                    this.gridControlDataInterfaceDBLink.DataSource = ProcSqlHelper.ExecuteDatatable(@"SELECT * FROM MultilayerDataInterface_DBLink where 1=1 ", CommandType.Text);
                    MessageBox.Show("密码加密成功!");
                }
            }

    解密按钮示例:

      private void btnDecryptMd5_Click(object sender, EventArgs e)
            {
                string DBLink_No = Convert.ToString(this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "DBLink_No") == null ? "" : this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "DBLink_No"));
                string PWD = Convert.ToString(this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "PWD") == null ? "" : this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "PWD"));
                bool isPWD_Encyption = Convert.ToBoolean(this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "isPWD_Encyption") == null ? "0" : this.gridViewDataInterfaceDBLink.GetRowCellValue(this.gridViewDataInterfaceDBLink.FocusedRowHandle, "isPWD_Encyption"));
                try
                {
                    if (isPWD_Encyption == false)
                    {
                        MessageBox.Show("该密码没有加密过,不需解密!");
                    }
                    else
                    {
                        //string PWD_Encyption = CommonHelper.Md5Encrypt(PWD);  //加密
                        string PWD_Decrypt = GetMd5Encrypt.Decrypt(PWD, "88");  //解密
                        this.txtPWD.Text = PWD_Decrypt;
                        string PWDnew = ";PWD=" + PWD_Decrypt.ToString(); //组成新的;PWD=及后续所有字符串
                        string DBConnString = this.txtDBConnString.Text.Trim();
                        this.txtPWDnew.Text = PWDnew;
                        this.txtDBConnString.Text = DBConnString.Substring(0, DBConnString.LastIndexOf(";PWD=")) + PWDnew.ToString();  //移除;PWD=及后续所有字符串,然后加上新的;PWD=及后续所有字符串
                        
                        ProcSqlHelper.ExecuteNonQuery(@"update MultilayerDataInterface_DBLink set isPWD_Encyption=0,PWD=@PWD_Decrypt where DBLink_No = @DBLink_No", CommandType.Text, new SqlParameter[] { new SqlParameter("@DBLink_No", DBLink_No), new SqlParameter("@PWD_Decrypt", PWD_Decrypt.ToString()) });
                        AuditDBLink();
                        this.gridControlDataInterfaceDBLink.DataSource = ProcSqlHelper.ExecuteDatatable(@"SELECT * FROM MultilayerDataInterface_DBLink where 1=1 ", CommandType.Text);
                        MessageBox.Show("密码解密成功!");
                    }
                }
                catch (Exception)
                {
                    throw new Exception("此密码非此方法加密,无法解密!");
                }           
            }

  • 相关阅读:
    修炼k8s+flink+hdfs+dlink(六:学习namespace,service)
    C# 12 中新增的八大功能你都知道吗?
    Stata绘制分类带可信区间的折线图
    PCB阻抗计算
    Java项目:SSM教师师资管理系统
    python --监听鼠标事件
    java-php-python+nodejs+vue生物遗传病的治疗和防范系统
    6.filters滤波
    基于Docker-compose搭建LNMP
    C语言的文件操作
  • 原文地址:https://blog.csdn.net/he198108/article/details/127450289