Esempio n. 1
0
        /// <summary>
        /// 验证请求内容是否可以解密(des)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string CheckRequestBodyForDecrypt(string requestBody)
        {
            var body = string.Empty;

            try
            {
                body = Encoding.UTF8.GetString(Helper.AESDecrypt(Helper.DecodeBytes(requestBody), CtripConfig.AesKey, CtripConfig.AesIv));
            }
            catch
            {
                body = "";
            }
            return(body);
        }
Esempio n. 2
0
 public string Decrypt(string input)
 {
     if (input.StartsWith("non"))
     {
         return(input.Substring(13));
     }
     if (input.StartsWith("rsa"))
     {
         input = input.Substring(13);
         input = Helper.RSADecrypt(input, this.RSAParams);
         var split = input.Split(new string[] { Helper.MacSeperator }, StringSplitOptions.RemoveEmptyEntries);
         var data  = split[0];
         var hash  = split[1];
         if (!Helper.RSAVerify(hash, data, this.ServerParams))
         {
             return("");
         }
         return(data);
     }
     else
     {
         input = input.Substring(13);
         var buffer   = new StringBuilder();
         var aesChunk = input.Split(new string[] { Helper.AESChunkSeperator }, StringSplitOptions.RemoveEmptyEntries);
         foreach (var item in aesChunk)
         {
             if (item.StartsWith(Helper.SessionKeySeperator) && item.EndsWith(Helper.SessionKeySeperator))
             {
                 var sepLen     = Helper.SessionKeySeperator.Length;
                 var encSession = item.Substring(sepLen, item.Length - sepLen * 2);
                 this.SessionKey = JsonConvert.DeserializeObject <SessionKey>(Helper.AESDecrypt(encSession, this.PhysicalKey));
                 continue;
             }
             buffer.Append(Helper.AESDecrypt(item, this.SessionKey.AESKey));
         }
         input = buffer.ToString();
         var split = input.Split(new string[] { Helper.MacSeperator }, StringSplitOptions.RemoveEmptyEntries);
         var data  = split[0];
         var hash  = split[1];
         if (!Helper.RSAVerify(hash, data, this.ServerParams))
         {
             return("");
         }
         return(data);
     }
 }
        private void InitClient(int id, string name, string rsaParamStr, string physicalKey, string sessionStr)
        {
            if (!Clients.ContainsKey(id))
            {
                return;
            }
            var param      = JsonConvert.DeserializeObject <RSAParameters>(rsaParamStr);
            var phys       = JsonConvert.DeserializeObject <AESKey>(physicalKey);
            var sessionKey = JsonConvert.DeserializeObject <SessionKey>(Helper.AESDecrypt(sessionStr, phys));
            var cl         = Clients[id];

            cl.Name          = name;
            cl.RSAParameters = param;
            cl.PhysicalKey   = phys;
            cl.SessionKey    = sessionKey;
            foreach (var item in Clients)
            {
                if (item.Key != id)
                {
                    item.Value.Send($"user{Helper.SocketMessageAttributeSeperator}{id}{Helper.SocketMessageAttributeSeperator}{name}");
                    cl.Send($"user{Helper.SocketMessageAttributeSeperator}{item.Key}{Helper.SocketMessageAttributeSeperator}{item.Value.Name}");
                }
            }
        }