使用自定义进制(62位),可以用于加密文本。

例子:

           StringList_ sl = new StringList_();
            for(int i = 0; i < 100; i++)
            {
                sl.Add(lg.SN10ToSN62(i));
            }

            lg.ShowDialogText(sl.ToString(","));

结果:

       //system of numeration (进制)  
       //0123456789 10                                               6061
       public const string SN62 =    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //62进制表示


        //十进制数是组成以10为基础的数字系统,有0,1,2,3, 4, 5, 6, 7, 8, 9十个基本数字组成。十进制,英文名称为Decimal System
        //来源于希腊文Decem,意为十。十进制计数是由印度教教徒在1500年前发明的,由阿拉伯人传承至11世纪。
        //十六进制(hexadecimal)是计算机中数据的一种表示方法。它的规则是“逢十六进一”。

        /// <summary>
        /// 把十进制数转为62进制       
        /// </summary>
        /// <param name="iValue">十进制整数值</param>
        /// 创建时间: 2020-04-26      最后一次修改时间:2020-04-26
        /// <returns>返回62进制字符串</returns>
        public static string SN10ToSN62(int iValue)
        {
            if (iValue == 0)  return "0";            
             
            int iTemp = (iValue > 0 ? iValue : - iValue);

            string sResult = "";

            while (iTemp != 0)
            {
                int n = iTemp % 62;   
                sResult += SN62[n];
                iTemp = iTemp / 62;
            }

            sResult = sResult._reversal();

            return  iValue > 0 ? sResult : "-" + sResult;
        }



        /// <summary>
        /// 把62进制字符串转为十进制整数    
        /// </summary>
        /// <param name="sValue"></param>
        /// 创建时间: 2020-04-25      最后一次修改时间:2020-04-25
        /// <returns></returns>
        public static int SN62ToSN10(string sValue)
        {
            if(sValue.Length == 0)
            {
                throw new Exception("错误的数字!");
            }
   
            string sTemp = (sValue[0] == '-' ?  sValue.Substring(1, sValue.Length - 1) : sValue)._reversal();

            int iResult = 0;
            int iExp = 0;
                   

            foreach(char c in sTemp)
            {
                int n = SN62.IndexOf(c);

                if(n == -1)
                {
                    throw new Exception("错误的62进制数!");
                }    

                iResult +=  n  *  LMath.Pow(62, iExp);

                ++iExp;
            }


            return sValue[0] == '-' ? - iResult : iResult;

        }


      /// <summary>
        /// 把密文解密成明文,如果解密不成功,则回原字符串
        /// </summary>
        /// <param name="sEncryptText"></param>
        /// <returns>成功返回明文,失败返回原字符串</returns>
        /// 创建时间: 2020-04-27      最后一次修改时间:2020-05-15
        public static string TextDecrypt2(string sEncryptText)
        {
            //int 类型的最大值是:2147483647 

            //a~z 97-122   A~Z 65-90  0~9 48-57

            // 字符串长度数值占用的字符位  + (字符串长度)  +   原字符值与字符串长度之和占用的字符位 + (字符值 + 字符串长度)  +  加密字符值总和  + 加密字符值总和占用的字符位(永远1位)

            //  a
            //  1                      +    1        + 2                                + (97+1)


            if (sEncryptText.Length < 5) //不能少于5位,空字符是:101z2
                return sEncryptText;


            //加密字符值总和占用位

            string sCountLength = sEncryptText[sEncryptText.Length - 1].ToString();

            if(!lg.IsSN62Number(sCountLength))
            {
                return sEncryptText;
            }


            int iCountLength = SN62ToSN10(sCountLength);//加密字符值总和占用位

           


            if (iCountLength > sEncryptText.Length - 1 || iCountLength < 0)
            {
                lg.ShowInfo("iCountLength > sEncryptText.Length - 1 || iCountLength < 0");
                return sEncryptText;
            }

                                   
            

            string sCount = sEncryptText.Substring(sEncryptText.Length - 1 - iCountLength, iCountLength);


            //----------------------------------------------------------------2020-05-15加入
            // (1)  10进制 int类型的最大值是:2147483647  =  62进制 2lkCB1
            // (2)   如果sCount不是62进制,则密文错误
            if (sCount.Length > 6 || !lg.IsSN62Number(sCount))   
            {
                return sEncryptText;
            }
            //------------------------------------------------------------------------------



            int iCheckValue = SN62ToSN10(sCount);


            if (iCheckValue <= 0)
            {
                lg.ShowInfo("iCheckValue <= 0");
                return sEncryptText;
            }



            //检查校验码
            string sEncryptData = sEncryptText.Substring(0, sEncryptText.Length - 1 - iCountLength);

            int iCheckValue2 = 0;

            foreach (char c in sEncryptData)
            {
                iCheckValue2 += (int)c;
            }


            if (iCheckValue2 != iCheckValue) //文件检验失败!
            {
                lg.ShowInfo("iCheckValue2 != iCheckValue");
                return sEncryptText;
            }



            //校验码已通过
            int iLengthCount = SN62ToSN10(sEncryptData[0].ToString());



            //  if (iLengthCount >= sEncryptData.Length - 1) 错误: 空字符会失败
            if (iLengthCount > sEncryptData.Length - 1)
            {
                lg.ShowInfo("iLengthCount > sEncryptData.Length - 1");
                return sEncryptText;

            }


            int iTextLength = SN62ToSN10(sEncryptData.Substring(1, iLengthCount));  //明文字符串长度



            int iStart = 1 + iLengthCount;

            string sText = "";

            //一定要加等于,否则最后一个字符是加不进去的
            while (iStart <= sEncryptData.Length - 1 - iLengthCount)
            {
                int iSubLength = SN62ToSN10(sEncryptData.Substring(iStart, 1));

                if (iSubLength < 0 || iSubLength >= sEncryptData.Length)
                {
                    lg.ShowError("iSubLength < 0 || iSubLength >= sEncryptData.Length");
                    return sEncryptText;
                }
                 

                int iCharValue = SN62ToSN10(sEncryptData.Substring(iStart + 1, iSubLength)) - iTextLength;

                if (iCharValue < 0)
                {
                    lg.ShowError("iCharValue < 0");
                    return sEncryptText;
                }

                sText += System.Convert.ToChar(iCharValue);

                iStart = iStart + 1 + iSubLength;
            }



            if (sText.Length != iTextLength)
            {
                lg.ShowError("sText.Length != iTextLength");
                return sEncryptText;  //长度检查
            }


            return sText;
        }



        /// <summary>
        /// 根据密钥来解密字符串
        /// </summary>
        /// <param name="sEncryptText">加密的字符串</param>
        /// <param name="sKey">密钥(用户密码为6~16字符,可使用字母(区分大小写)、数字与特殊符号 用户密码至少包含字母、数字、符号中的两种)</param>
        /// 创建时间: 2020-04-27      最后一次修改时间:2020-04-27
        /// <returns>成功返回解密码字符串,失败返回原字符</returns>
        public static string TextDecrypt2(string sEncryptText, string sKey)
        {

            //(sText.Length的字符占位个数 +  sText.Length) + sText1 +  (sKey.Length的字符占位个数 +  sKey.Length + skey)  +  sText2;

            //关建插入点位置 InsertPos + Length + sKey
            // InsertPos = sKey所有字符相加的和 % sText.Length   //10%7=3
             

            string sDecryptText = TextDecrypt2(sEncryptText);

            if (sDecryptText.Length == 0)  //空字符
                return "";


            if (sDecryptText == sEncryptText) //失败返回原字符
                return sEncryptText;

            int nTextLengthCount = lg.SN62ToSN10(sDecryptText[0].ToString());

            


            //当传递的字符串是不带sKey的加密串时
            if (sDecryptText.Length.ToString().Length < nTextLengthCount) //母字符串的长度的占位数小于子字符串的长度的占位数是错误的
            {
                return sEncryptText;
            }

            if (nTextLengthCount <= 0) //占位最少是1
            {
                return sEncryptText;
            }


            int nTextLength = lg.SN62ToSN10(sDecryptText.Substring(1, nTextLengthCount));



            if (nTextLengthCount == -1 || nTextLength == -1) //失败
                return sEncryptText;




            int nCount = 0;
            foreach (char c in sKey)
            {
                nCount += (int)c;
            }


            int nText1_Lenth = nCount % nTextLength + 1;
            int nStart = 1 + nTextLengthCount + nText1_Lenth;



            int nKeyLengthCount = lg.SN62ToSN10(sDecryptText.Substring(nStart, 1));



            //nKeyLengthCount <= 2  sKey.Length的长度大小是1~15,也就是 <= 2

            if (nKeyLengthCount > 2) //密钥可能不对
            {
                return sEncryptText; //返回原字符

            }



            int nKeyLength = lg.SN62ToSN10(sDecryptText.Substring(nStart + 1, nKeyLengthCount));



            if (nKeyLengthCount == -1 || nKeyLength == -1) //失败
                return sEncryptText;


            string sCheckKey = lg.resumeCode(sDecryptText.Substring(nStart + 1 + nKeyLengthCount, nKeyLength));

            if (sCheckKey != sKey)    //失败
            {
                //return sEncryptText;
                return "密码不正确!";
            }


            string s1 = lg.resumeCode(sDecryptText.Substring(1 + nTextLengthCount, nText1_Lenth));
            string s2 = lg.resumeCode(sDecryptText.Substring(nStart + 1 + nKeyLengthCount + nKeyLength, nTextLength - nText1_Lenth));


            return s1 + s2;

        }



   /// <summary>
        /// 随便打乱字符串,防止一眼就能看清内容,加1取反
        /// </summary>
        /// <param name="aStr"></param>
        /// <returns></returns>
        public static string messCode(string aStr)
        {
            string tmp = "";

            for (int i = 0; i < aStr.Length; ++i)
            {
                tmp += (char)(~((int)aStr[i] + 1));
            }
            return tmp;
        }



        /// <summary>
        ///还原打乱字符串
        /// </summary>
        /// <param name="aStr"></param>
        /// <returns></returns>
        public static string resumeCode(string aStr)
        {
            string tmp = "";

            for (int i = 0; i < aStr.Length; ++i)
            {
                tmp += (char)(~((int)aStr[i]) - 1);
            }
            return tmp;
        }


        /// <summary>
        /// 把字符串加密成密文
        /// </summary>
        /// <param name="aPassword"></param>
        /// 创建时间: ????-??-??      最后一次修改时间:2020-01-06(已丢弃)
        /// <returns></returns>
        public static string Rubbish_passwordEncrypt(string aPassword)
        {
            string mc = messCode(aPassword);

            string tmp = "";

            for (int i = 0; i < mc.Length; ++i)
            {
                tmp += ((int)mc[i] + i * 100).ToString();

                if (i < mc.Length - 1)
                    tmp += ',';
            }

            return tmp;
        }

        /// <summary>
        /// 把字符串译成明文
        /// </summary>
        /// <param name="aPassword"></param>
        /// 创建时间: ????-??-??      最后一次修改时间:2020-01-06(已丢弃)
        /// <returns></returns>
        public static string Rubbish_passwordRevert(string aPassword)
        {
            string[] sList = aPassword.Split(',');


            string tmp = "";

            for (int i = 0; i < sList.Length; ++i)
            {
                tmp += (char)(System.Convert.ToInt32(sList[i]) - (i * 100));
            }

            return resumeCode(tmp);
        }


        /// <summary>
        /// 把正整数的个数,十位、百位等等都用英文表示:例:256 => cFg  123 => bCd
        /// </summary>
        /// <param name="iNumber">正整数</param>
        /// 创建时间: 2020-04-05      最后一次修改时间:2020-04-06
        /// <returns>英文表示的数字</returns>
        public static string NumberShowToEnglishAlphabet(int iNumber)
        {
            //a~z 97-122   A~Z 65-90  0~9 48-57

            if (iNumber < 0)
                return "";

            string sResul = "";

            string sNumber = iNumber.ToString();


            for (int i = 0; i < sNumber.Length; ++i)
            {
                if (i % 2 == 0)
                    sResul += System.Convert.ToChar((int)sNumber[i] + 97 - 48); //小写字母
                else
                    sResul += System.Convert.ToChar((int)sNumber[i] + 65 - 48); //大写字母
            }


            return sResul;
        }

        /// <summary>
        /// 把英文表示的非负数还原成真实的数字,不成功返回-1.  例: 123 => abc   abc = > 123
        /// </summary>
        /// <param name="sEnglishAlphabet"></param>
        /// 创建时间: 2020-04-05      最后一次修改时间:2020-04-06
        /// <returns></returns>
        public static int EnglishAlphabetToNumber(string sEnglishAlphabet)
        {
            string sNumber = "";

            for (int i = 0; i < sEnglishAlphabet.Length; ++i)
            {
                if (i % 2 == 0)
                    sNumber += ((int)sEnglishAlphabet[i] - 97).ToString();
                else
                    sNumber += ((int)sEnglishAlphabet[i] - 65).ToString();
            }

            if (!lg.isNumberString(sNumber))
                return -1;

            return System.Convert.ToInt32(sNumber);
        }


        /// <summary>
        /// 是否62进制数
        /// </summary>
        /// <param name="sValue"></param>
        /// 创建时间: 2020-04-27      最后一次修改时间:2020-04-27
        /// <returns></returns>
        public static bool IsSN62Number(string sValue)
        {
            if (sValue == "")
                return false;

            foreach(char c in sValue)
            {
                if (SN62.IndexOf(c) == -1)
                    return false;
            }
            return true;
        }



        /// <summary>
        /// 把字符串按一定格式加密
        /// </summary>
        /// <param name="sText">明文字符串</param>
        /// 创建时间: 2020-04-05      最后一次修改时间:2020-04-27
        /// <returns>返回加密的字符串</returns>
        public static string TextEncrypt(string sText)
        {
            //a~z 97-122   A~Z 65-90  0~9 48-57

            // 字符串长度数值占用的字符位  + (字符串长度)  +   原字符值与字符串长度和占用的字符位 + (字符值 + 字符串长度)  +  加密字符值总和  + 加密字符值总和占用的字符位

            //  a
            //  1                      +    1        + 2                                + (97+1)


            string sResult = "";
        
            int iLength = sText.Length;

            string sLength = NumberShowToEnglishAlphabet(iLength);
            string sLengthCount = NumberShowToEnglishAlphabet(sLength.Length);

            sResult += sLengthCount;
            sResult += sLength;


            foreach (char c in sText)
            {
                int iCharValue = (int)c;

                string sTemp = NumberShowToEnglishAlphabet(iCharValue + iLength);
                sResult += NumberShowToEnglishAlphabet(sTemp.Length);

                sResult += sTemp;

            }

            int iCheckValue = 0;

            foreach (char c in sResult)
            {
                iCheckValue += (int)c;
            }


             

            string sCheckValue = NumberShowToEnglishAlphabet(iCheckValue);

            sResult += sCheckValue;

            sResult += NumberShowToEnglishAlphabet(sCheckValue.Length);



            return sResult;
        }





        /// <summary>
        /// 创建时间: 2020-04-05      最后一次修改时间:2020-04-19
        /// 把密文解密成明文,如果解密不成功,则回原字符串
        /// </summary>
        /// <param name="sEncryptText"></param>
        /// <returns>成功返回明文,失败返回原字符串</returns>
        public static string TextDecrypt(string sEncryptText)
        {
            //int 类型的最大值是:2147483647 

            //a~z 97-122   A~Z 65-90  0~9 48-57

            // 字符串长度数值占用的字符位  + (字符串长度)  +   原字符值与字符串长度之和占用的字符位 + (字符值 + 字符串长度)  +  加密字符值总和  + 加密字符值总和占用的字符位

            //  a
            //  1                      +    1        + 2                                + (97+1)


            if (sEncryptText.Length < 6) //不能少于6位,空字符是:babJfd
                return sEncryptText;


            int iCountLength = EnglishAlphabetToNumber(sEncryptText[sEncryptText.Length - 1].ToString());



            if (iCountLength > sEncryptText.Length - 1 || iCountLength < 0)
            {

                //lg.ShowInfo("iCountLength > sEncryptText.Length - 1 || iCountLength < 0");
                return sEncryptText;
            }



            int iCheckValue = EnglishAlphabetToNumber(sEncryptText.Substring(sEncryptText.Length - 1 - iCountLength, iCountLength));


            if (iCheckValue <= 0)
            {
                //lg.ShowInfo("iCheckValue <= 0");
                return sEncryptText;
            }



            //检查校验码
            string sEncryptData = sEncryptText.Substring(0, sEncryptText.Length - 1 - iCountLength);

            int iCheckValue2 = 0;

            foreach (char c in sEncryptData)
            {
                iCheckValue2 += (int)c;
            }


            if (iCheckValue2 != iCheckValue) //文件检验失败!
            {
                return sEncryptText;
            }



            //校验码已通过
            int iLengthCount = EnglishAlphabetToNumber(sEncryptData[0].ToString());



            //  if (iLengthCount >= sEncryptData.Length - 1) 错误: 空字符会失败
            if (iLengthCount > sEncryptData.Length - 1)
            {
                return sEncryptText;

            }


            int iTextLength = EnglishAlphabetToNumber(sEncryptData.Substring(1, iLengthCount));  //明文字符串长度



            int iStart = 1 + iLengthCount;

            string sText = "";

            //一定要加等于,否则最后一个字符是加不进去的
            while (iStart <= sEncryptData.Length - 1 - iLengthCount)
            {
                int iSubLength = EnglishAlphabetToNumber(sEncryptData.Substring(iStart, 1));

                if (iSubLength < 0 || iSubLength >= sEncryptData.Length)
                {
                    lg.ShowError("iSubLength < 0 || iSubLength >= sEncryptData.Length");
                    return sEncryptText;
                }

                int iCharValue = EnglishAlphabetToNumber(sEncryptData.Substring(iStart + 1, iSubLength)) - iTextLength;

                if (iCharValue < 0)
                {
                    lg.ShowError("iCharValue < 0");
                    return sEncryptText;
                }

                sText += System.Convert.ToChar(iCharValue);

                iStart = iStart + 1 + iSubLength;
            }



            if (sText.Length != iTextLength)
            {
                lg.ShowError("sText.Length != iTextLength");
                return sEncryptText;  //长度检查
            }


            return sText;
        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值