Exemple #1
0
        /// <summary>
        /// 动态改变或者连接字符串
        /// </summary>
        /// <returns></returns>
        protected virtual string GetConnectionString(string dbConfigName)
        {
            string           connectionString = "";
            DatabaseSettings setting          = ConfigurationManager.GetSection("dataConfiguration") as DatabaseSettings;

            if (setting != null)
            {
                string connection = string.IsNullOrEmpty(dbConfigName) ? setting.DefaultDatabase : dbConfigName;
                connectionString = ConfigurationManager.ConnectionStrings[connection].ConnectionString;

                #region 加密解密操作

                //使用自定义加密
                //if (!connectionString.EndsWith(";"))
                //{
                //    connectionString += ";";
                //}
                //connectionString += string.Format("Jet OLEDB:Database Password=wuhuacong2013;");

                string passwordKey = "Jet OLEDB:Database Password";
                string password    = GetSubValue(connectionString, passwordKey);
                if (!string.IsNullOrEmpty(password))
                {
                    //尝试使用AES解密
                    string decryptStr = password;
                    try
                    {
                        decryptStr = EncodeHelper.AES_Decrypt(password);
                    }
                    catch
                    {
                        decryptStr = password;
                        //throw new InvalidOperationException("无法解密数据库");
                    }

                    connectionString += string.Format(";{0}={1};", passwordKey, decryptStr);
                }
                #endregion
            }

            return(connectionString);
        }
Exemple #2
0
        /// <summary>
        /// 动态改变或者连接字符串
        /// </summary>
        /// <returns></returns>
        protected virtual string GetConnectionString(string dbConfigName)
        {
            string           connectionString = "";
            DatabaseSettings setting          = ConfigurationManager.GetSection("dataConfiguration") as DatabaseSettings;

            if (setting != null)
            {
                string connection = string.IsNullOrEmpty(dbConfigName) ? setting.DefaultDatabase : dbConfigName;
                connectionString = ConfigurationManager.ConnectionStrings[connection].ConnectionString;

                #region 加密解密操作

                //使用自定义加密
                //if (!connectionString.EndsWith(";"))
                //{
                //    connectionString += ";";
                //}
                //connectionString += string.Format("Jet OLEDB:Database Password=wuhuacong2013;");

                string passwordKey = "Jet OLEDB:Database Password";
                string password    = GetSubValue(connectionString, passwordKey);
                if (!string.IsNullOrEmpty(password))
                {
                    //尝试使用AES解密
                    string decryptStr = password;
                    try
                    {
                        decryptStr = EncodeHelper.AES_Decrypt(password);
                    }
                    catch (Exception ex)
                    {
                        decryptStr = password;
                        LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(BaseDALAccess <T>));
                    }

                    connectionString += string.Format(";{0}={1};", passwordKey, decryptStr);
                }
                #endregion
            }

            return(connectionString);
        }
Exemple #3
0
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            string original = "测试加密字符串";

            Console.WriteLine("original:" + original);

            string encrypt = EncodeHelper.SHA256(original);

            Console.WriteLine("EncodeHelper.SHA256:" + encrypt);

            //DES加解密
            encrypt = EncodeHelper.DesEncrypt(original);
            Console.WriteLine("EncodeHelper.DesEncrypt:" + encrypt);

            string decrypt = EncodeHelper.DesDecrypt(encrypt);

            Console.WriteLine("EncodeHelper.DesDecrypt:" + decrypt);

            //MD5加密
            encrypt = EncodeHelper.MD5Encrypt(original);
            Console.WriteLine("EncodeHelper.MD5Encrypt:" + encrypt);
            encrypt = EncodeHelper.MD5EncryptHash(original);
            Console.WriteLine("EncodeHelper.MD5EncryptHash:" + encrypt);
            encrypt = EncodeHelper.MD5EncryptHashHex(original);
            Console.WriteLine("EncodeHelper.MD5EncryptHashHex:" + encrypt);
            encrypt = EncodeHelper.EncyptMD5_3_16(original);
            Console.WriteLine("EncodeHelper.EncyptMD5_3_16:" + encrypt);

            //Base64加解密
            encrypt = EncodeHelper.Base64Encrypt(original);
            Console.WriteLine("EncodeHelper.Base64Encrypt:" + encrypt);
            decrypt = EncodeHelper.Base64Decrypt(encrypt);
            Console.WriteLine("EncodeHelper.Base64Encrypt:" + decrypt);

            encrypt = EncodeHelper.AES_Encrypt(original);
            Console.WriteLine("EncodeHelper.AES_Encrypt:" + encrypt);
            decrypt = EncodeHelper.AES_Decrypt(encrypt);
            Console.WriteLine("EncodeHelper.AES_Decrypt:" + decrypt);

            //MD5加密字符串然后检查是否被篡改
            encrypt = MD5Util.GetMD5_32(original);
            bool flag = MD5Util.ValidateValue(encrypt);

            Console.WriteLine("flag: " + flag);

            //为文件增加MD5编码标签,然后验证是否被修改
            string file  = @"DB\test1.xls";
            bool   flag2 = MD5Util.AddMD5(file);

            Console.WriteLine("flag2: " + flag2);

            //对给定路径的文件进行验证,如果一致返回True,否则返回False
            bool flag3 = MD5Util.CheckMD5(file);

            Console.WriteLine("flag3: " + flag3);

            //生成加解密私钥、公钥
            string publicKey  = "";
            string privateKey = "";

            RSASecurityHelper.GenerateRSAKey(out privateKey, out publicKey);

            string originalString  = "testdata";
            string encryptedString = RSASecurityHelper.RSAEncrypt(publicKey, originalString);
            string originalString2 = RSASecurityHelper.RSADecrypt(privateKey, encryptedString);

            if (originalString == originalString2)
            {
                MessageDxUtil.ShowTips("解密完全正确");
            }
            else
            {
                MessageDxUtil.ShowWarning("解密失败");
            }

            string regcode   = RSASecurityHelper.RSAEncrypSignature(privateKey, originalString);
            bool   validated = RSASecurityHelper.Validate(originalString, regcode, publicKey);

            MessageDxUtil.ShowTips(validated ? "验证成功" : "验证失败");
        }