Esempio n. 1
0
        private DataInfo HandleRequest(HttpListenerRequest request, HttpListenerResponse response)
        {
            string data = null;

            try
            {
                var byteList = new List <byte>();
                var byteArr  = new byte[2048];
                int readLen  = 0;
                int len      = 0;
                //接收客户端传过来的数据并转成字符串类型
                do
                {
                    readLen = request.InputStream.Read(byteArr, 0, byteArr.Length);
                    len    += readLen;
                    byteList.AddRange(byteArr);
                } while (readLen != 0);
                data = Encoding.UTF8.GetString(byteList.ToArray(), 0, len);

                //获取得到数据data可以进行其他操作
            }
            catch (Exception ex)
            {
                response.StatusDescription = "404";
                response.StatusCode        = 404;
                Console.ForegroundColor    = ConsoleColor.Red;
                //Console.WriteLine($"在接收数据时发生错误:{ex.ToString()}");
                return(null);                   //把服务端错误信息直接返回可能会导致信息不安全,此处仅供参考
            }
            response.StatusDescription = "200"; //获取或设置返回给客户端的 HTTP 状态代码的文本说明。
            response.StatusCode        = 200;   // 获取或设置返回给客户端的 HTTP 状态代码。
            Console.ForegroundColor    = ConsoleColor.Green;
            //Console.WriteLine($"接收数据完成:{data.Trim()},时间:{DateTime.Now.ToString()}");
            if (request.RawUrl == "/getDogInfo")
            {
                return(getDogInfo());
            }
            else if (request.RawUrl == "/encryp")
            {
                ParamsList param = JsonConvert.DeserializeObject <ParamsList>(data);
                return(encryp(param));
            }
            else if (request.RawUrl == "/decrypt")
            {
                DecryptParamsList param = JsonConvert.DeserializeObject <DecryptParamsList>(data);
                return(decrypt(param));
            }
            else
            {
                DataInfo datainfo = new DataInfo();
                datainfo.Code = 0;
                datainfo.Data = "";
                return(datainfo);
            }
        }
Esempio n. 2
0
        //解密
        private DataInfo decrypt(DecryptParamsList param)
        {
            DecryptList decryptInfo = new DecryptList();

            decryptInfo.Decrypt = param.paramsString;
            Parallel.For(0, decryptInfo.Decrypt.Count, (i) =>
            {
                List <string> strs = decryptInfo.Decrypt[(int)i];
                Parallel.For(0, strs.Count, (j) =>
                {
                    string str = strs[(int)j];
                    if (str != "" && str != null)
                    {
                        if (ht[str] == null)
                        {
                            string dStr = RUtils.Decrypt(bKey, str);
                            decryptInfo.Decrypt[(int)i][(int)j] = dStr;
                            ht[str] = dStr;
                        }
                        else
                        {
                            decryptInfo.Decrypt[(int)i][(int)j] = (string)ht[str];
                        }
                    }
                    else
                    {
                        decryptInfo.Decrypt[(int)i][(int)j] = "";
                    }
                });
            });
            DataInfo datainfo = new DataInfo();

            datainfo.Code = 0;
            datainfo.Data = decryptInfo;
            return(datainfo);
        }