Ejemplo n.º 1
0
        private AdminCookieDto GetAdminCookieDto(ActionExecutingContext context)
        {
            var cookie = context.GetCookie(CookieSetting.AdminCookie);

            if (string.IsNullOrEmpty(cookie))
            {
                return(null);
            }
            var sp = cookie.Split(".");

            if (sp.Length != 2)
            {
                return(null);
            }
            var bytes = Base64.FromBase64ForUrlString(sp[0]);
            var hash  = HashUtil.GetMd5String(bytes);

            if (hash == sp[1])
            {
                bytes = ThreeRCX.Encrypt(bytes, RsaHelper.Instance.CookiePassword);
                var json = Encoding.UTF8.GetString(bytes);
                try {
                    var userDto = json.ToObject <AdminCookieDto>();
                    if (userDto.ExpireTime < DateTime.Now || userDto.CreateTime > DateTime.Now)
                    {
                        return(null);
                    }
                    return(userDto);
                } catch { }
            }
            return(null);
        }
Ejemplo n.º 2
0
        protected void SetAdminCookie(AdminCookieDto dto, DateTime expireTime)
        {
            var json      = dto.ToJson(true);
            var bytes     = ThreeRCX.Encrypt(json, RsaHelper.Instance.CookiePassword);
            var hash      = HashUtil.GetMd5String(bytes);
            var cookieStr = Base64.ToBase64ForUrlString(bytes) + "." + hash;

            SetCookie(CookieSetting.AdminCookie, cookieStr, expireTime);
        }
Ejemplo n.º 3
0
        protected string GetAdminCookieString(AdminCookieDto dto)
        {
            var json      = dto.ToJson(true);
            var bytes     = ThreeRCX.Encrypt(json, RsaHelper.Instance.CookiePassword);
            var hash      = HashUtil.GetMd5String(bytes);
            var cookieStr = Base64.ToBase64ForUrlString(bytes) + "." + hash;

            return(cookieStr);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="password"></param>
        /// <param name="ignoreNames"></param>
        public void EncryptData(byte[] password, IEnumerable <string> ignoreNames)
        {
            if (password != null && password.Length > 0)
            {
                var json  = this.Data.ToJson(ignoreNames);
                var bytes = Encoding.UTF8.GetBytes(json);

                var bs = ThreeRCX.Encrypt(bytes, password);//解密
                Ciphertext = Base64.ToBase64String(bs);
                Data       = null;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="password"></param>
        public void EncryptData(byte[] password)
        {
            if (password != null && password.Length > 0)
            {
                var json  = JsonConvert.SerializeObject(this.Data);
                var bytes = Encoding.UTF8.GetBytes(json);

                var bs = ThreeRCX.Encrypt(bytes, password);//解密
                Ciphertext = Base64.ToBase64String(bs);
                Data       = null;
            }
        }
Ejemplo n.º 6
0
        private string SetAdminCookieDto(string key, AdminCookieDto dto)
        {
            var json      = dto.ToJson(true);
            var bytes     = ThreeRCX.Encrypt(json, RsaHelper.Instance.CookiePassword);
            var hash      = HashUtil.GetMd5String(bytes);
            var cookieStr = Base64.ToBase64ForUrlString(bytes) + "." + hash;

            SetCookie(key, cookieStr, dto.ExpireTime);

            HttpContext.Response.Cookies.Append(CookieSetting.AdminCookieLogin, "1", new Microsoft.AspNetCore.Http.CookieOptions()
            {
                Path        = "/",
                Expires     = dto.ExpireTime,
                IsEssential = true,
                HttpOnly    = false,
            });
            return(cookieStr);
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Encoding encoding = Encoding.ASCII;

            Console.WriteLine("配置说明:");
            Console.WriteLine("    编码类型(Encoding):ASCII");
            Console.WriteLine("    输出数据类型(output data type):Base64_Url");
            Console.WriteLine("    密钥(secret key):ToolGood ");
            Console.WriteLine("");

            Console.WriteLine("1、测试RCX算法的正确性(Testing the correctness of the RCX algorithm)");
            Console.WriteLine("明文(original text):hello RCX!");
            var bytes = RCX.Encrypt("hello RCX!", "ToolGood", encoding);

            Console.WriteLine("加密后数据(ecrypted data):" + Base64.ToBase64ForUrlString(bytes));
            Console.WriteLine("解密后文本(decrypted text):" + Encoding.UTF8.GetString(RCX.Encrypt(bytes, "ToolGood", encoding)));


            Console.WriteLine("2、测试RCX算法的正确性(Testing the correctness of the RCX algorithm)");
            Console.WriteLine("明文(original text):hello RCY!");
            bytes = RCY.Encrypt("hello RCY!", "ToolGood", encoding);
            Console.WriteLine("加密后数据(ecrypted data):" + Base64.ToBase64ForUrlString(bytes));
            Console.WriteLine("解密后文本(decrypted text):" + Encoding.UTF8.GetString(RCY.Encrypt(bytes, "ToolGood", encoding)));


            Console.WriteLine("");
            Console.WriteLine("3、测试RCX算法的变化能力(Testing the ability to change the RCX algorithm)");
            Console.WriteLine("RC4('ABCDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RC4.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RC4('ACBDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RC4.Encrypt("ACBDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RC4('CBADDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RC4.Encrypt("CBADDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RC4('1234567891234567891234567') => " + Base64.ToBase64ForUrlString(RC4.Encrypt("1234567891234567891234567", "ToolGood", encoding)));
            Console.WriteLine("RC4('1234567800034567891234567') => " + Base64.ToBase64ForUrlString(RC4.Encrypt("1234567800034567891234567", "ToolGood", encoding)));

            Console.WriteLine("");
            Console.WriteLine("RCX('ABCDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCX.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RCX('ACBDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCX.Encrypt("ACBDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RCX('CBADDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCX.Encrypt("CBADDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RCX('1234567891234567891234567') => " + Base64.ToBase64ForUrlString(RCX.Encrypt("1234567891234567891234567", "ToolGood", encoding)));
            Console.WriteLine("RCX('1234567800034567891234567') => " + Base64.ToBase64ForUrlString(RCX.Encrypt("1234567800034567891234567", "ToolGood", encoding)));

            Console.WriteLine("");
            Console.WriteLine("ThreeRCX('ABCDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(ThreeRCX.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCX('ACBDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(ThreeRCX.Encrypt("ACBDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCX('CBADDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(ThreeRCX.Encrypt("CBADDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCX('1234567891234567891234567') => " + Base64.ToBase64ForUrlString(ThreeRCX.Encrypt("1234567891234567891234567", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCX('1234567800034567891234567') => " + Base64.ToBase64ForUrlString(ThreeRCX.Encrypt("1234567800034567891234567", "ToolGood", encoding)));

            Console.WriteLine("");
            Console.WriteLine("RCY('ABCDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCY.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RCX('ACBDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCY.Encrypt("ACBDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RCY('CBADDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCY.Encrypt("CBADDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("RCY('1234567891234567891234567') => " + Base64.ToBase64ForUrlString(RCY.Encrypt("1234567891234567891234567", "ToolGood", encoding)));
            Console.WriteLine("RCY('1234567800034567891234567') => " + Base64.ToBase64ForUrlString(RCY.Encrypt("1234567800034567891234567", "ToolGood", encoding)));

            Console.WriteLine("");
            Console.WriteLine("ThreeRCY('ABCDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(ThreeRCY.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCY('ACBDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(ThreeRCY.Encrypt("ACBDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCY('CBADDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(ThreeRCY.Encrypt("CBADDDDDDDDDDDDDDDDDDDDDD", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCY('1234567891234567891234567') => " + Base64.ToBase64ForUrlString(ThreeRCY.Encrypt("1234567891234567891234567", "ToolGood", encoding)));
            Console.WriteLine("ThreeRCY('1234567800034567891234567') => " + Base64.ToBase64ForUrlString(ThreeRCY.Encrypt("1234567800034567891234567", "ToolGood", encoding)));


            //Console.WriteLine("RCX('ABCDDDDDDDDDDDDDDDDDDDDDD') => " + Base64.ToBase64ForUrlString(RCX.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", Encoding.ASCII)));
            var t  = RCX.Encrypt("ABCDDDDDDDDDDDDDDDDDDDDDD", "ToolGood", Encoding.UTF8);
            var dd = Convert.ToBase64String(t);
            var t1 = RCX.Encrypt("测试RCX算法的性能", "ToolGood");

            Console.WriteLine("");
            Console.WriteLine("3、测试RCX算法的性能(Testing the performance of the RCX algorithm)");
            Console.WriteLine("数据长度(data length):10000 ");
            Console.WriteLine("加密次数(encryption count):1000 ");
            var str   = new string('a', 10000);
            var count = 1000;

            DoRC4(str, "ToolGood", count);
            DoRCX(str, "ToolGood", count);
            DoRCY(str, "ToolGood", count);

            Console.ReadKey();
        }