一个不错的加解密类

这个是一个PHP的加解密的方法,之前用到过,后来把方法移植到C#上,安全系数也不错。

 public static string Encode(string text, string keystr)
        {
            var key = string.IsNullOrEmpty(keystr) ? "as542!#@AGD23aADsgg" : keystr;
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
            var ikey = "-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm";

            Func<Random, int> rand = (r) => r.Next(64);
            var nh1 = rand(new Random(Guid.NewGuid().GetHashCode()));
            var nh2 = rand(new Random(Guid.NewGuid().GetHashCode()));
            var nh3 = rand(new Random(Guid.NewGuid().GetHashCode()));

            var ch1 = chars[nh1];
            var ch2 = chars[nh2];
            var ch3 = chars[nh3];

            var nhnum = nh1 + nh2 + nh3;
            var knum = 0;
            foreach (var s in key)
            {
                knum += (int)s;
            }

            Func<string, string> func = (r) =>
            {
                byte[] array = Encoding.Default.GetBytes(r);
                array = new MD5CryptoServiceProvider().ComputeHash(array);
                var result = new StringBuilder(32);
                for (int i = 0; i < array.Length; i++)
                {
                    result.Append(array[i].ToString("x").PadLeft(2, '0'));
                }
                return result.ToString();

            };
            var mdKey = func(func(func(key + ch1) + ch2 + ikey) + ch3).Substring(nhnum % 8, knum % 8 + 16);
            var txt = Convert.ToBase64String(Encoding.UTF8.GetBytes(text)).Replace('+', '-').Replace('/', '_').Replace('=', '.');
            var tmp = string.Empty;
            var j = 0;
            var k = 0;
            var tlen = txt.Length;
            var klen = mdKey.Length;
            for (var i = 0; i < tlen; i++)
            {
                k = (k == klen) ? 0 : k;
                j = (nhnum + chars.IndexOf(txt[i]) + (int)mdKey[k++]) % 64;
                tmp += chars[j];
            }
            var tmplen = tmp.Length;
            tmp = tmp.Insert(nh2 % ++tmplen, ch3.ToString()).Insert(nh1 % ++tmplen, ch2.ToString()).Insert(knum % ++tmplen, ch1.ToString());
            Console.WriteLine(tmp);
            return tmp;
        }

        public static string Decode(string txt, string keystr)
        {
            var key = string.IsNullOrEmpty(keystr) ? "as542!#@AGD23aADsgg" : keystr;
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
            var ikey = "-x6g6ZWm2G9g_vr0Bo.pOq3kRIxsZ6rm";

            var knum = 0;

            var tlen = txt.Length;

            foreach (var s in key)
            {
                knum += (int)s;
            }

            var ch1 = txt[knum % tlen];
            var nh1 = chars.IndexOf(ch1);
            txt = txt.Remove(knum % tlen--, 1);
            var ch2 = txt[nh1 % tlen];
            var nh2 = chars.IndexOf(ch2);
            txt = txt.Remove(nh1 % tlen--, 1);

            var ch3 = txt[nh2 % tlen];
            var nh3 = chars.IndexOf(ch3);
            txt = txt.Remove(nh2 % tlen--, 1);
            var nhnum = nh1 + nh2 + nh3;

            Func<string, string> func = (r) =>
            {
                byte[] array = Encoding.Default.GetBytes(r);
                array = new MD5CryptoServiceProvider().ComputeHash(array);
                var result = new StringBuilder(32);
                for (int i = 0; i < array.Length; i++)
                {
                    result.Append(array[i].ToString("x").PadLeft(2, '0'));
                }
                return result.ToString();
            };

            var mdKey = func(func(func(key + ch1) + ch2 + ikey) + ch3).Substring(nhnum % 8, knum % 8 + 16);
            var tmp = string.Empty;

            var j = 0;
            var k = 0;
            tlen = txt.Length;
            var klen = mdKey.Length;

            for (var i = 0; i < tlen; i++)
            {
                k = (k == klen) ? 0 : k;
                j = chars.IndexOf(txt[i]) - nhnum - (int)mdKey[k++];
                while (j < 0)
                {
                    j += 64;
                }
                tmp += chars[j];
            }
            tmp = tmp.Replace('-', '+').Replace('_', '/').Replace('.', '=');
            tmp = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(tmp));
            return tmp;
        }

  

你可能感兴趣的:(解密)