예제 #1
0
        /// <summary>
        /// 加密返回的串
        /// </summary>
        /// <param name="input"></param>
        /// <param name="encodingAesKey"></param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public static string AesEncrypt(String input, string encodingAesKey, string appid)
        {
            var Key = Convert.FromBase64String(encodingAesKey + "=");
            var Iv  = new byte[16];

            Array.Copy(Key, Iv, 16);

            var randCode = CreateRandCode(16);
            var bRand    = Encoding.UTF8.GetBytes(randCode);
            var bAppid   = Encoding.UTF8.GetBytes(appid);
            var btmpMsg  = Encoding.UTF8.GetBytes(input);
            var bMsgLen  = BitConverter.GetBytes(HostToNetworkOrder(btmpMsg.Length));
            var bMsg     = new byte[bRand.Length + bMsgLen.Length + bAppid.Length + btmpMsg.Length];

            Array.Copy(bRand, bMsg, bRand.Length);
            Array.Copy(bMsgLen, 0, bMsg, bRand.Length, bMsgLen.Length);
            Array.Copy(btmpMsg, 0, bMsg, bRand.Length + bMsgLen.Length, btmpMsg.Length);
            Array.Copy(bAppid, 0, bMsg, bRand.Length + bMsgLen.Length + btmpMsg.Length, bAppid.Length);

            #region  对消息进行PKCS7补位

            var msg = new byte[bMsg.Length + 32 - bMsg.Length % 32];
            Array.Copy(bMsg, msg, bMsg.Length);
            var pad = Kcs7Encoder(bMsg.Length);
            Array.Copy(pad, 0, msg, bMsg.Length, pad.Length);

            #endregion

            var xBuff = AesRijndael.Encrypt(Key, msg, Iv, 256, 128, CipherMode.CBC, PaddingMode.None);
            return(Convert.ToBase64String(xBuff));
        }
예제 #2
0
        /// <summary>
        /// 解密方法
        /// </summary>
        /// <param name="Input">密文</param>
        /// <param name="encodingAesKey"></param>
        /// <returns></returns>
        public static string WXAesDecrypt(String Input, string encodingAesKey)
        {
            var Key = Convert.FromBase64String(encodingAesKey + "=");
            var Iv  = new byte[16];

            Array.Copy(Key, Iv, 16);

            var xXml    = Convert.FromBase64String(Input);
            var xBuff   = AesRijndael.Decrypt(Key, xXml, Iv, 256, 128, CipherMode.CBC, PaddingMode.None);
            var btmpMsg = Decode(xBuff);

            var len = BitConverter.ToInt32(btmpMsg, 16);

            len = IPAddress.NetworkToHostOrder(len);

            var bMsg = new byte[len];

            //byte[] bAppid = new byte[btmpMsg.Length - 20 - len];
            Array.Copy(btmpMsg, 20, bMsg, 0, len);
            //Array.Copy(btmpMsg, 20 + len, bAppid, 0, btmpMsg.Length - 20 - len);
            var oriMsg = Encoding.UTF8.GetString(bMsg);

            //appid = Encoding.UTF8.GetString(bAppid);
            return(oriMsg);
        }
예제 #3
0
        public void Encrypt()
        {
            string key = Guid.NewGuid().ToString().Replace("-", string.Empty);

            string result = AesRijndael.Encrypt("owxehuN4Ntt8Gx0AqBJ6O6Jv27Yg", key);

            string r = AesRijndael.Decrypt(result, key);
        }
예제 #4
0
        /// <summary>
        ///  通过token解析出对应的id和key
        /// </summary>
        /// <param name="encryptKey"></param>
        /// <param name="token"></param>
        /// <returns>返回解析信息,Item1为id,Item2为key</returns>
        public static string GetTokenDetail(string encryptKey, string token)
        {
            var tokenDetail = AesRijndael.Decrypt(token.Base64UrlDecode(), encryptKey);

            if (string.IsNullOrEmpty(tokenDetail))
            {
                throw new ArgumentNullException(nameof(token), "不合法的用户Token");
            }

            return(tokenDetail);
        }
예제 #5
0
        /// <summary>
        ///  通过token解析出对应的id和key
        /// </summary>
        /// <param name="encryptKey"></param>
        /// <param name="token"></param>
        /// <returns>返回解析信息,Item1为id,Item2为key</returns>
        public static Tuple <long, string> GetTokenDetail(string encryptKey, string token)
        {
            string tokenDetail = AesRijndael.Decrypt(token.Base64UrlDecode(), encryptKey);

            if (string.IsNullOrEmpty(tokenDetail))
            {
                throw new ArgumentNullException("token", "不合法的用户Token");
            }

            string[] memberIdSplit = tokenDetail.Split('|');

            return(Tuple.Create(memberIdSplit[0].ToInt64(), memberIdSplit[1]));
        }
예제 #6
0
        /// <summary>
        /// 通过 ID 生成对应的Token
        /// </summary>
        /// <param name="encryptKey"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static MemberInfo GetTokenDetail(string encryptKey, string token)
        {
            string tokenDetail = AesRijndael.Decrypt(token.Base64UrlDecode(), encryptKey);

            if (string.IsNullOrEmpty(tokenDetail))
            {
                throw new ArgumentNullException("token", "不合法的用户Token");
            }

            string[] memberIdSplit = tokenDetail.Split('|');

            var memberInfo = new MemberInfo();

            memberInfo.UserId = memberIdSplit[0].ToInt64();

            return(memberInfo);
        }
예제 #7
0
        private static TRes DecryptTo <TRes>(this WechatMAppEncryptBody encryptBody, string sessionKey)
        {
            try
            {
                var encryptDataBytes = Convert.FromBase64String(encryptBody.encrypt_data);
                var sesssionKeyBytes = Convert.FromBase64String(sessionKey);
                var ivBytes          = Convert.FromBase64String(encryptBody.iv);

                var result = Encoding.UTF8.GetString(AesRijndael.Decrypt(sesssionKeyBytes, encryptDataBytes, ivBytes, 128,
                                                                         128, CipherMode.CBC));

                return(JsonConvert.DeserializeObject <TRes>(result));
            }
            catch
            {
            }

            return(default);
예제 #8
0
 /// <summary>
 /// 通过 ID 生成对应的Token
 /// </summary>
 /// <param name="encryptKey"></param>
 /// <param name="tokenDetail"></param>
 /// <returns></returns>
 public static string GetToken(string encryptKey, string tokenDetail)
 {
     return(AesRijndael.Encrypt(tokenDetail, encryptKey).Base64UrlEncode());
 }
예제 #9
0
        /// <summary>
        /// 通过 ID 生成对应的Token
        /// </summary>
        /// <param name="encryptKey"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static string GetToken(string encryptKey, long userId)
        {
            string sourceData = string.Concat(userId, "|", DateTime.Now.ToUtcSeconds());

            return(AesRijndael.Encrypt(sourceData, encryptKey).Base64UrlEncode());
        }
예제 #10
0
        ///// <summary>
        ///// 获取成员扩展详情
        ///// </summary>
        ///// <typeparam name="TMInfo"></typeparam>
        ///// <returns></returns>
        //public static TMInfo GetMemberInfo<TMInfo>()
        //    where TMInfo : class => Identity?.MemberInfo as TMInfo;

        #endregion

        #region    token  处理

        /// <summary>
        /// 通过 ID 生成对应的Token
        /// </summary>
        /// <param name="encryptKey"></param>
        /// <param name="tokenDetail"></param>
        /// <returns></returns>
        public static string GetToken(string encryptKey, string tokenDetail)
        {
            return(AesRijndael.Encrypt(tokenDetail, encryptKey).ReplaceBase64ToUrlSafe());
        }
예제 #11
0
        public IActionResult TestCommon()
        {
            ViewData["ContentRootPath"] = FileHelper.ContentRootPath;
            ViewData["WebRootPath"]     = FileHelper.WebRootPath;
            ViewData["WebRootName"]     = FileHelper.WebRootName;

            string strRequestMsg = string.Empty;

            strRequestMsg         += " HasFormContentType:" + MyHttpContext.Current.Request.HasFormContentType + Environment.NewLine;
            strRequestMsg         += " Host:" + MyHttpContext.Current.Request.Host + Environment.NewLine;
            strRequestMsg         += " IsHttps:" + MyHttpContext.Current.Request.IsHttps + Environment.NewLine;
            strRequestMsg         += " Method:" + MyHttpContext.Current.Request.Method + Environment.NewLine;
            strRequestMsg         += " Path:" + MyHttpContext.Current.Request.Path + Environment.NewLine;
            strRequestMsg         += " PathBase:" + MyHttpContext.Current.Request.PathBase + Environment.NewLine;
            strRequestMsg         += " Protocol:" + MyHttpContext.Current.Request.Protocol + Environment.NewLine;
            strRequestMsg         += " Scheme:" + MyHttpContext.Current.Request.Scheme + Environment.NewLine;
            ViewData["RequestMsg"] = strRequestMsg;

            ViewData["RequestReferer"] = MyHttpContext.Current.Request.UrlReferrer();
            string strHeaders = string.Empty;

            foreach (String strFormKey in MyHttpContext.Current.Request.Headers.Keys)
            {
                strHeaders += strFormKey + ":" + MyHttpContext.Current.Request.Headers[strFormKey] + "#####";
            }
            ViewData["RequestHeaders"] = strHeaders;

            ViewData["RequestHeadersJSON"] = "";            //MyHttpContext.Current.Request.Headers.ToJson();
            string strForm = string.Empty;

            if (MyHttpContext.Current.Request.Method == "POST")
            {
                foreach (String strFormKey in MyHttpContext.Current.Request.Form.Keys)
                {
                    strForm += strFormKey + ":" + DataConverter.ToString(MyHttpContext.Current.Request.Form[strFormKey]) + "#####";
                }
            }
            ViewData["RequestFormJSON"] = strForm;
            //ViewData["RequestFormJSON"] = MyHttpContext.Current.Request.Form.ToJson();

            string strCurrentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
            var    resultAdd      = CacheHelper.CacheServiceProvider.AddOrUpdate("CurrentTime", strCurrentTime);

            if (CacheHelper.CacheServiceProvider.Exists("CurrentTime"))
            {
                string resultGet = CacheHelper.CacheServiceProvider.Get <string>("CurrentTime");
                ViewData["CurrentTime"] = resultGet;
            }

            string strMsg        = "我是加密文件,请保管好";
            string strKey        = Guid.NewGuid().ToString().Replace("-", string.Empty);
            string strEncryptAes = AesRijndael.Encrypt(strMsg, strKey);
            string strDecryptAes = AesRijndael.Decrypt(strEncryptAes, strKey);

            ViewData["EncryptAes"] = strEncryptAes;
            ViewData["DecryptAes"] = strDecryptAes;

            string strEncryptHmacSha1       = HmacSha1.EncryptUtf8(strMsg, strKey);
            string strEncryptHmacSha1Base64 = HmacSha1.EncryptBase64(strMsg, strKey);

            ViewData["EncryptHmacSha1"]       = strEncryptHmacSha1;
            ViewData["EncryptHmacSha1Base64"] = strEncryptHmacSha1Base64;

            string strEncryptMd5 = Md5.EncryptHexString(strMsg);

            ViewData["EncryptMd5"] = strEncryptMd5;

            string strEncryptSha1 = Sha1.Encrypt(strMsg);

            ViewData["EncryptSha1"] = strEncryptSha1;

            ViewData["ClientIP"] = IPHelper.GetClientIP();
            ViewData["HostIP"]   = IPHelper.GetHostIP();

            ViewData["RandomFormatedNumeric"] = RandomHelper.GetFormatedNumeric(4, 10);
            ViewData["RandomString"]          = RandomHelper.GetRandString(4);
            ViewData["RandomStringByPattern"] = RandomHelper.GetRandStringByPattern("JX###???***");

            ViewData["StringHelper-GetHashKey"]       = StringHelper.GetHashKey(strMsg, StringFilterOptions.HoldChinese);
            ViewData["StringHelper-GetStringHashKey"] = StringHelper.GetStringHashKey(strMsg);
            ViewData["StringHelper-MD5"]         = StringHelper.MD5(strMsg);
            ViewData["StringHelper-MD5D"]        = StringHelper.MD5D(strMsg);
            ViewData["StringHelper-MD5GB2312"]   = StringHelper.MD5GB2312(strMsg);
            ViewData["StringHelper-SHA1"]        = StringHelper.SHA1(strMsg);
            ViewData["StringHelper-ValidateMD5"] = StringHelper.ValidateMD5(strEncryptMd5, StringHelper.MD5(strMsg));

            ViewData["StringHelper-SubString"]       = StringHelper.SubString(strMsg, 12, "...");
            ViewData["StringHelper-GetInitial"]      = StringHelper.GetInitial(strMsg);
            ViewData["StringHelper-MakeSpellCode"]   = ChineseSpell.MakeSpellCode(strMsg, SpellOptions.EnableUnicodeLetter);
            ViewData["StringHelper-FirstLetterOnly"] = ChineseSpell.MakeSpellCode(strMsg, SpellOptions.FirstLetterOnly);

            ViewData["XmlSerializer"] = ConfigHelper.Get <IPLockConfig>().ToXml();
            var strXML       = "<IPLockConfig> <AdminLockIPBlack>10.123.123.1</AdminLockIPBlack> <AdminLockIPType>30.123.123.3</AdminLockIPType> <AdminLockIPWhite>20.123.123.2</AdminLockIPWhite> <LockIPType>60.123.123.6</LockIPType> <LockIPBlack>40.123.123.4</LockIPBlack> <LockIPWhite>50.123.123.5</LockIPWhite> </IPLockConfig>";
            var ipLockConfig = strXML.ToXmlObject <IPLockConfig>();

            ViewData["XmlDeserialize"] = "IPLockConfig:" + ipLockConfig.AdminLockIPBlack + " " + ipLockConfig.AdminLockIPType + " " + ipLockConfig.AdminLockIPWhite;

            ViewData["JsonSerializer"] = ConfigHelper.Get <WebHostConfig>().ToJson();
            ConfigHelper.Save(ConfigHelper.Get <ShopConfig>());
            ConfigHelper.Save(ConfigHelper.Get <SiteConfig>());
            ConfigHelper.Save(ConfigHelper.Get <SiteOptionConfig>());
            ConfigHelper.Save(ConfigHelper.Get <SmsConfig>());
            ConfigHelper.Save(ConfigHelper.Get <UserConfig>());
            ConfigHelper.Save(ConfigHelper.Get <WebHostConfig>());
            var strJSON    = "{\"AuthenticationType\": 0,\"EnabledSsl\": false,\"MailFrom\": \"[email protected]\",\"MailServer\": \"smtp.qq.com\",\"MailServerPassWord\": \"lx123456\",\"MailServerUserName\": \"2094838895\",\"Port\": \"25\"}";
            var mailConfig = strJSON.ToJsonObject <MailConfig>();

            ViewData["JsonDeserialize"] = mailConfig.AuthenticationType + " " + mailConfig.EnabledSsl + " " + mailConfig.MailFrom + " " + mailConfig.MailServer;


            CookieHelper.CreateCookie("cName", "aspnetcore" + strCurrentTime, 10);
            ViewData["CookieHelper"] = CookieHelper.GetCookie("cName");
            //CookieHelper.DeleteCookie("cName");

            DateTime beginDate = new DateTime(2015, 12, 5);
            DateTime endDate   = DateTime.Now;

            ViewData["DateDiff-yyyy"] = Utility.DateDiff(beginDate, endDate, "yyyy");
            ViewData["DateDiff-q"]    = Utility.DateDiff(beginDate, endDate, "q");
            ViewData["DateDiff-m"]    = Utility.DateDiff(beginDate, endDate, "m");
            ViewData["DateDiff-d"]    = Utility.DateDiff(beginDate, endDate, "d");
            ViewData["DateDiff-w"]    = Utility.DateDiff(beginDate, endDate, "w");
            ViewData["DateDiff-h"]    = Utility.DateDiff(beginDate, endDate, "h");
            ViewData["DateDiff-n"]    = Utility.DateDiff(beginDate, endDate, "n");
            ViewData["DateDiff-s"]    = Utility.DateDiff(beginDate, endDate, "s");

            ViewData["Query-name"]   = Utility.Query("name");
            ViewData["Query-date"]   = Utility.Query("date", DateTime.Now).ToString("yyyy-MM-dd HH:mm:ss:fff");
            ViewData["Query-age"]    = Utility.Query("age", 0);
            ViewData["Query-mobile"] = Utility.AddQuery("mobile", "15871375589");

            var mailConfigSession = ConfigHelper.Get <MailConfig>();

            Utility.SetSession("sessionName", mailConfigSession);

            ViewData["GroupTypeEnum"]  = EnumHelper.GetDescription((GroupTypeEnum)(0));
            ViewData["GroupTypeEnum1"] = EnumHelper.GetDescription <GroupTypeEnum>(GroupTypeEnum.Agent.ToString());

            LogFactory.SaveFileLog("日志标题get", "日志内容");


            return(View());
        }