using System;

namespace LJLib.Tools.Encry
{
    public class psw_bczh3
    {
        //f_psw_BCzh2(STRING obj_string,INT add_expr,STRING keystr) 三倍变长加密,可以用于任意字符串
        //obj_string 目标的处理字符串,不能为空
        //add_expr 0 :加密,1:解密
        //arg_keystr 密钥数字字符串
        //private string obj_string;
        //private int add_expr;
        //private string arg_keystr;

        public string GetEntrypt(string obj_string, int add_expr, string arg_keystr)
        {
            string RT_STRING = "";
            if (obj_string == null || obj_string == "" || arg_keystr.Length == 0)
            {
                // add by lwl 20091217
                if (RT_STRING == "")
                {
                    RT_STRING = "3003003003";
                }
            }

            int LS_XX = 0;
            int LS_YY = 0;
            string ZH_STR = "";

            int STR_LEN = obj_string.Length;
            string FORMAT_STR = arg_keystr;

            try
            {
                if (add_expr == 0)//ADD加密
                {

                    for (LS_YY = 0; LS_YY < (STR_LEN / FORMAT_STR.Length) + 1; LS_YY++)
                        ZH_STR += FORMAT_STR;//67896789

                    for (LS_XX = 0; LS_XX < STR_LEN; LS_XX++)
                    {
                        RT_STRING += (999 - (int)Math.Pow(int.Parse(ZH_STR.Substring(LS_XX, 1)), 2) * 9 - (int)obj_string.Substring(LS_XX, 1)[0]).ToString();
                    }

                    RT_STRING = ReverseString(RT_STRING);
                    // add by lwl 20091217
                    if (RT_STRING == "")
                    {
                        RT_STRING = "3003003003";
                    }

                }
                else//EXPR解密
                {
                    // add by lwl 20091217
                    if (obj_string == "3003003003")
                    {
                        return "";
                    }
                    else if (obj_string == "")
                    {
                        return "";
                    }

                    for (LS_YY = 0; LS_YY < ((STR_LEN / 3) / FORMAT_STR.Length) + 1; LS_YY++)
                        ZH_STR += FORMAT_STR;


                    obj_string = ReverseString(obj_string);

                    for (LS_XX = 0; LS_XX < STR_LEN / 3; LS_XX++)
                    {

                        if (int.Parse(obj_string.Substring(LS_XX * 3, 3)) == 0)
                        {
                            //int1 = int.Parse(obj_string.Substring(LS_XX * 3, 3)) * 13;
                            //RT_STRING += int1.ToString();

                            RT_STRING += (int.Parse(obj_string.Substring(LS_XX * 3, 3)) * 13).ToString();
                        }
                        else
                        {
                            RT_STRING += Convert.ToChar(999 - (int)Math.Pow(int.Parse(ZH_STR.Substring(LS_XX, 1)), 2) * 9 - int.Parse(obj_string.Substring(LS_XX * 3, 3)));
                        }
                    }
                }
            }
            catch
            {
                RT_STRING = "";
            }


            return RT_STRING;

        }

        public string GetEntrypt(string obj_string, int add_expr)
        {
            string arg_keystr = "182457512398663";
            string RT_STRING = "";
            //if (obj_string == null || obj_string == "" || arg_keystr.Length == 0)
            if (obj_string == null)
                return RT_STRING;

            int LS_XX = 0;
            int LS_YY = 0;
            string ZH_STR = "";

            int STR_LEN = obj_string.Length;
            string FORMAT_STR = arg_keystr;

            try
            {
                if (add_expr == 0)//ADD加密
                {
                    if (obj_string == "")
                        return "3003003003";

                    for (LS_YY = 0; LS_YY < (STR_LEN / FORMAT_STR.Length) + 1; LS_YY++)
                        ZH_STR += FORMAT_STR;//67896789

                    for (LS_XX = 0; LS_XX < STR_LEN; LS_XX++)
                    {
                        RT_STRING += (999 - (int)Math.Pow(int.Parse(ZH_STR.Substring(LS_XX, 1)), 2) * 9 - (int)obj_string.Substring(LS_XX, 1)[0]).ToString();
                    }

                    RT_STRING = ReverseString(RT_STRING);

                }
                else//EXPR解密
                {
                    if (obj_string == "3003003003")
                        return "";

                    for (LS_YY = 0; LS_YY < ((STR_LEN / 3) / FORMAT_STR.Length) + 1; LS_YY++)
                        ZH_STR += FORMAT_STR;


                    obj_string = ReverseString(obj_string);

                    for (LS_XX = 0; LS_XX < STR_LEN / 3; LS_XX++)
                    {

                        if (int.Parse(obj_string.Substring(LS_XX * 3, 3)) == 0)
                        {
                            //int1 = int.Parse(obj_string.Substring(LS_XX * 3, 3)) * 13;
                            //RT_STRING += int1.ToString();

                            RT_STRING += (int.Parse(obj_string.Substring(LS_XX * 3, 3)) * 13).ToString();
                        }
                        else
                        {
                            RT_STRING += Convert.ToChar(999 - (int)Math.Pow(int.Parse(ZH_STR.Substring(LS_XX, 1)), 2) * 9 - int.Parse(obj_string.Substring(LS_XX * 3, 3)));
                        }
                    }
                }
            }
            catch
            {
                RT_STRING = "";
            }


            return RT_STRING;

        }

        private string ReverseString(string S)
        {
            string tS = "";
            for (int i = S.Length - 1; i >= 0; i--)
                tS = tS + S.Substring(i, 1);
            return tS;
        }


        public string dEncrypt(string obj_string, string obj_wkeystring, ref string obj_rkeystring)
        {
            string enstr = "";
            string rtstr = "";

            long key = 0;
            key = long.Parse(obj_wkeystring);
            key = (long)Math.Sqrt(key);
            obj_rkeystring = key.ToString();

            for (int i = 0; i < (obj_string.Length / obj_rkeystring.Length) + 1; i++)
                enstr += obj_rkeystring;

            enstr = ReverseString(enstr);

            for (int i = 0; i < obj_string.Length; i++)
                rtstr += (500 - (int)Math.Pow(int.Parse(enstr.Substring(i, 1)), 2) * 3 - (int)obj_string.Substring(i, 1)[0]).ToString();

            return rtstr;


        }

        public string dUnencrypt(string obj_string, string obj_keystring)
        {
            string enstr = "";
            string rtstr = "";

            long key = 0;
            key = long.Parse(obj_keystring);
            key = (long)Math.Pow(key, 2);

            for (int i = 0; i < ((obj_string.Length) / (obj_keystring.Length * 3)) + 1; i++)
                enstr += obj_keystring;
            enstr = ReverseString(enstr);

            for (int i = 0; i < obj_string.Length / 3; i++)
            {

                if (int.Parse(obj_string.Substring(i * 3, 3)) == 0)
                {
                    rtstr += (int.Parse(obj_string.Substring(i * 3, 3)) * 9).ToString();
                }
                else
                {
                    rtstr += Convert.ToChar(500 - (int)Math.Pow(int.Parse(enstr.Substring(i, 1)), 2) * 3 - int.Parse(obj_string.Substring(i * 3, 3)));
                }
            }

            return rtstr;

        }


    }
}
