zoukankan      html  css  js  c++  java
  • 加干扰字符

    public class keyformat
        {
            /// <summary>
            /// 验证规则是否符合标准
            /// </summary>
            /// <param name="format">需要验证的字符串</param>
            public bool TestFormat(string format)
            {
                Boolean f = true;
                if (IsStr(format, @"[^@_\$\d]+"))
                {
                    f = false;
                }
                else if (IsStr(format, @"[^_]\$"))
                {
                    f = false;
                }
                //数字只能在$之后且数字在字符串最前
                else if (IsStr(format, @"[^\$]\d+|^\d+"))
                {
                    f = false;
                }
                return f;
            }
    
    
            /// <summary>
            /// 将字符串按照format和key进行
            /// </summary>
            /// <param name="str">要整理的字符串</param>
            /// <param name="format">规则</param>
            /// <param name="key">加密用的字符串</param>
            /// <returns></returns>
            public string GetFkstring(string str, string format, string key)
            {
                StringBuilder result = new StringBuilder();
                if (TestFormat(format))
                {
                    format = GetFormat(format);
    
                    int j = 0, k = 0;
                    for (int i = 0; ; i++)
                    {
                        if (i == format.Length)
                        {
                            //规则先用完,先将字符串剩下的拼上,再将加密用的字符串拼上
                            result.Append(key.Substring(k));
                            if (j < str.Length)
                                result.Append(str.Substring(j));
                            break;
                        }
    
                        if (format[i] == '@')
                        {
                            //若用完用“ ”补足
                            if (j >= str.Length)
                                result.Append(" ");
                            else
                                result.Append(str[j]);
                            j++;
                        }
                        else
                        {
                            result.Append(key[k]);
                            k++;
                            if (k == key.Length)
                            {
                                //加密用的字符串不够,将字符串剩下的拼上
                                if (j < str.Length)
                                    result.Append(str.Substring(j));
                                break;
                            }
                        }
                    }
                }
                else
                {
                    result.Append(str);
                }
                return result.ToString();
            }
            /// <summary>
            /// 将key中的$加数字转换为@和_
            /// </summary>
            /// <param name="format">需要转换的key</param>
            /// <returns></returns>
            private string GetFormat(string format)
            {
                if (TestFormat(format))
                {
                    int n = format.LastIndexOf("_$");
                    while (n > -1)
                    {
                        string formatbefore = format.Substring(0, n + 1);
                        string formatafter = format.Substring(n + 2);
    
                        //取出$后面的数字并把formatafter中的数字截掉
                        int reReplaceN = 1;
                        string reReplaceNStr = MatchStr(formatafter, @"^(\d+)[@_]*");
                        if (reReplaceNStr != "")
                        {
                            reReplaceN = Int16.Parse(reReplaceNStr);
                            formatafter = formatafter.Substring(reReplaceNStr.Length);
                        }
                        //得到要重复的字段
                        //_$之前提一个@或$
                        int replaceIndex = Math.Max(formatbefore.LastIndexOf("@"), formatbefore.LastIndexOf("$"));
                        string replaceOne = "";
                        if (replaceIndex > -1)
                        {
                            replaceOne = formatbefore.Substring(replaceIndex);
                            //如果是$把$和数字(如果有)去掉
                            if (replaceOne[0] == '$')
                            {
                                replaceOne = replaceOne.Substring(replaceOne.IndexOf('_'));
                            }
                        }
                        //拼接
                        format = formatbefore + GetRepeatStr(replaceOne, reReplaceN) + formatafter;
    
                        n = format.LastIndexOf("_$");
                    }
                    return format.Replace("$", "");
                }
                return "";
            }
    
            /// <summary>
            /// 将字符串重复
            /// </summary>
            /// <param name="str">需要重复的字符串</param>
            /// <param name="n">重复的次数</param>
            /// <returns></returns>
            private string GetRepeatStr(string str, int n)
            {
                string result = "";
                for (int i = 0; i < n; i++)
                {
                    result += str;
                }
                return result;
            }
    
            /// <summary>
            /// 根据正则找出匹配字符串
            /// </summary>
            /// <param name="input">需要验证的字符串</param>
            /// <param name="pattern">正则</param>
            /// <returns></returns>
            private string MatchStr(string input, string pattern)
            {
                string result = "";
                Regex r = new Regex(pattern);
                Match m = r.Match(input);
                if (m.Success)
                {
                    result = m.Groups[1].ToString();
                }
                return result;
            }
            /// <summary>
            /// 根据正则验证字符串是否符合要求
            /// </summary>
            /// <param name="input">需要验证的字符串</param>
            /// <param name="pattern">正则</param>
            /// <returns></returns>
            private bool IsStr(string input, string pattern)
            {
                Regex r = new Regex(pattern);
                Match m = r.Match(input);
                return m.Success;
            }
            
        }
  • 相关阅读:
    访问控制模型+强制访问控制
    防火墙体系结构
    信息安全工程师手记
    关于PHPSESSIONID的一些发现
    中级测评师10-物联网
    WAPI学习记录
    1. Jenkins 入门使用
    Springboot druid监控配置
    Springboot 添加数据源报错
    mysql function 查询子级机构
  • 原文地址:https://www.cnblogs.com/yzj1212/p/2616835.html
Copyright © 2011-2022 走看看