/// <summary>
        /// 加密后入库  以及搜索   数据库存储的加密数据   先解密  后 加密
        /// </summary>
        /// <param name="pf"></param>
        /// <param name="dbSecurity"></param>
        /// <param name="sqlConfig"></param>
        /// <param name="field"></param>
        /// <param name="fieldValue"></param>
        /// <returns></returns>
        public string SetDesEnDbPswd(PFGlobalParameter pf, LoginSecurity dbSecurity, SqlConfig sqlConfig, string field, string fieldValue)
        {
            if (sqlConfig.Columns.ContainsKey(field) && ValidateUtil.ValidateExistsValue(fieldValue))
            {
                SqlColumn sqlColumn = sqlConfig.Columns[field];
                if (sqlColumn != null && (sqlColumn.EncDesMethod == EncDesMethod.Qt || sqlColumn.EncDesMethod == EncDesMethod.Qt2Db))
                {
                    fieldValue = _desService.DecryptDefault(pf.OwnerId, pf.SysId, fieldValue);
                }
                if (sqlColumn.EncDesMethod == EncDesMethod.Qt2Db || sqlColumn.EncDesMethod == EncDesMethod.Db)
                {
                    fieldValue = SetEnDbPswd(pf, null, fieldValue) ?? fieldValue;
                }
            }
            return fieldValue;
        }

        /// <summary>
        /// 加密后入库  以及搜索   数据库存储的加密数据
        /// </summary>
        /// <param name="pf"></param>
        /// <param name="dbSecurity"></param>
        /// <param name="value"></param>
        public string SetEnDbPswd(PFGlobalParameter pf, LoginSecurity dbSecurity, string value)
        {
            if (!ValidateUtil.ValidateExistsValue(value)) return null;

            if (dbSecurity == null)
            {
                dbSecurity = new LoginSecurity();
                _configuration.GetSection("dbSecurity").Bind(dbSecurity);
            }
            StringBuilder sbValue = new StringBuilder();

            if (value.Length > 6)
            {
                sbValue.Append(value.Substring(0, 3));
                sbValue.Append("|");
                sbValue.Append(_desService.Encrypt(pf.OwnerId, pf.SysId, value.Substring(3, value.Length - 6), dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);
                sbValue.Append("|");
                sbValue.Append(value.Substring(value.Length - 3, 3));
            }
            else if (value.Length > 4)
            {
                sbValue.Append(value.Substring(0, 2));
                sbValue.Append("|");
                sbValue.Append(_desService.Encrypt(pf.OwnerId, pf.SysId, value.Substring(3, value.Length - 4), dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);
                sbValue.Append("|");
                sbValue.Append(value.Substring(value.Length - 2, 2));
            }
            else if (value.Length > 2)
            {
                sbValue.Append(value.Substring(0, 1));
                sbValue.Append("|");
                sbValue.Append(_desService.Encrypt(pf.OwnerId, pf.SysId, value.Substring(3, value.Length - 2), dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);
                sbValue.Append("|");
                sbValue.Append(value.Substring(value.Length - 1, 1));
            }
            else if (value.Length == 2)
            {
                sbValue.Append(value.Substring(0, 1));
                sbValue.Append("|");
                sbValue.Append(_desService.Encrypt(pf.OwnerId, pf.SysId, value.Substring(1, 1), dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);
            }
            else
            {
                sbValue.Append(value);
            }
            //for (int i = 0; i < value.Length; i++)
            //{
            //    sbValue.Append(_desService.Encrypt(pf.OwnerId, pf.SysId, value.Substring(i, 1), dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result).Append("|");
            //}
            return sbValue.ToString().TrimEnd('|');
        }

        /// <summary>
        /// 解密后显示  把数据库存储的数据进行解密
        /// </summary>
        /// <param name="pf"></param>
        /// <param name="dbSecurity"></param>
        /// <param name="value"></param>
        public string SetDeDbPswd(PFGlobalParameter pf, LoginSecurity dbSecurity, object value)
        {
            StringBuilder sbValue = new StringBuilder();
            if (!ValidateUtil.ValidateExistsValue(value)) return null;

            if (dbSecurity == null)
            {
                dbSecurity = new LoginSecurity();
                _configuration.GetSection("dbSecurity").Bind(dbSecurity);
            }
            string[] values = value.ToString().Split("|", StringSplitOptions.RemoveEmptyEntries);
            if (values.Length == 1)
            {
                sbValue.Append(values[0]);
            }
            else if (values.Length == 2)
            {
                sbValue.Append(values[0]);
                sbValue.Append(_desService.Decrypt(pf.OwnerId, pf.SysId, values[1], dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);

            }
            else if (values.Length == 3)
            {
                sbValue.Append(values[0]);
                sbValue.Append(_desService.Decrypt(pf.OwnerId, pf.SysId, values[1], dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);
                sbValue.Append(values[2]);
            }

            //for (int i = 0; i < values.Length; i++)
            //{
            //    sbValue.Append(_desService.Decrypt(pf.OwnerId, pf.SysId, values[i], dbSecurity.WebSecurityKey, dbSecurity.WebSecurityVI).Result);
            //}
            return sbValue.ToString();
        }
    }
文档更新时间: 2022-09-18 13:25   作者:admin