网址参数递增。简单的加密

我们经常会遇到一些递增量作为参数的网址,但是我们并不想把一些数据轻易的全都给别人,那么通过简单的 映射替换加以解决。。

http://hotels.ctrip.com/hotel/441359.html#ctm_ref=hod_hp_hv_def_n_1

http://hotels.ctrip.com/hotel/441360.html#ctm_ref=hod_hp_hv_def_n_1

http://hotels.ctrip.com/hotel/441361.html#ctm_ref=hod_hp_hv_def_n_1

主要采取把十进制转成62进制结合取余数运算。

 /// <summary>
        /// 加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetEncryption(int str)
        {
            List<char> chars = new List<char>//顺序建议随机打乱
             {
                 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
                 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
                 'y', 'z', '0', '1', '2', '3', '4', '5',
                 '6', '7', '8', '9', 'A', 'B', 'C', 'D',
                 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
                 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
                 'U', 'V', 'W', 'X', 'Y', 'Z'
              };


            int num = str;
            num = num * 6 + 5653565;//随机加密方法
            string result = "";
            int count = 0;//统计字符数值和
            while (num != 0)
            {
                var re = chars[num % 62];//62进制的转换
                count+= (int)re;
                result = re + result;
                int c = num / 62;
                num = c;
            }
            result = chars[count % 26] + result;//前缀
            count += (int)chars[count % 26 +30];//再次位移
            result = result + chars[count % 62];//后缀--校验作用,防止随机输入
            return result;


        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetDecrypt(string str)
        {
            if (str.Length<3)
            {
                return "非法字符串";
            }
            int count = 0;//统计字符数值和
            char start = (char)str[0];//前缀
            char end = (char)str[str.Length-1];//后缀
            str = str.Substring(1, str.Length - 2);//核心数据
            List<char> chars = new List<char>
           {
               'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
             'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
             'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
             'y', 'z', '0', '1', '2', '3', '4', '5',
             '6', '7', '8', '9', 'A', 'B', 'C', 'D',
             'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
             'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
             'U', 'V', 'W', 'X', 'Y', 'Z'
           };
            for (int i = 0; i < str.Length; i++)
            {
                count += (int)(str[i]);//计算字符数值和
            }
           
            if ((chars[count % 26]==start))//校验前缀
            {
                count += (int)(chars[count % 26+30]);//位移
                if (!(chars[count % 62] == end))//校验后缀
                {
                    return "非法字符串";
                }
                
            }
            else
            {
                return "非法字符串";
            }
            int sum = 0;//解密结果
            for (int i = 0; i < str.Length; i++)
            {
                char c = str[i];
                var index = chars.IndexOf(c);
                sum = index * (int)Math.Pow(62, str.Length-1-i) + sum; //转换成十进制
            }
            sum = (sum - 5653565);//位移
            if (sum% 6 == 0)//还原
            {
                sum = sum / 6;
                return sum.ToString();//返回结果
            }
            else
            {
                return "非法字符串";
            }
        }



猜你喜欢

转载自blog.csdn.net/qq_15555767/article/details/79205230