コード例 #1
0
ファイル: ClientCryptoHandler.cs プロジェクト: daywrite/OBear
        private HttpResponseMessage DecryptResponse(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return(response);
            }
            string data = response.Content.ReadAsStringAsync().Result;

            if (string.IsNullOrEmpty(data))
            {
                return(response);
            }
            try
            {
                data = CommunicationCryptor.DecryptAndVerifyData(data, _clientPrivateKey, _hashType, _publicKey);
                if (data == null)
                {
                    throw new OBearException(Resources.Http_Security_Client_VerifyResponse_Failt);
                }
                HttpContent content = new StringContent(data);
                content.Headers.ContentType = response.Content.Headers.ContentType;
                response.Content            = content;
                return(response);
            }
            catch (Exception ex)
            {
                //HttpError error = new HttpError(Resources.Http_Seciruty_Client_DecryptResponse_Failt);
                response = response.RequestMessage.CreateErrorResponse(HttpStatusCode.InternalServerError, Resources.Http_Seciruty_Client_DecryptResponse_Failt);
                return(response);
            }
        }
コード例 #2
0
        /// <summary>
        /// 数据传到Hub之前进行数据解密
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context)
        {
            //_canCrypto = CanCrypto(context.Hub.Context);
            //if (!_canCrypto)
            //{
            //    return base.OnBeforeIncoming(context);
            //}
            //数据解密
            string facePublicKey = context.Hub.Context.Headers.Get(HttpHeaderNames.MesClientPublicKey);

            if (string.IsNullOrEmpty(facePublicKey))
            {
                return(false);
            }
            _cryptor = new CommunicationCryptor(_ownPrivateKey, facePublicKey, _hashType);
            if (context.Args.Count == 1)
            {
                string         encrypt = (string)context.Args[0];
                string         json    = _cryptor.DecryptAndVerifyData(encrypt);
                IList <object> args    = JsonConvert.DeserializeObject <IList <object> >(json);
                context.Args.Clear();
                IList <object> values = context.MethodDescriptor.Parameters.Zip(args, (desc, arg) => ResolveParameter(desc, arg)).ToList();
                foreach (object arg in values)
                {
                    context.Args.Add(arg);
                }
            }
            return(base.OnBeforeIncoming(context));
        }
コード例 #3
0
        private HttpResponseMessage DecryptResponse(HttpResponseMessage response)
        {
            if (!response.IsSuccessStatusCode)
            {
                return(response);
            }
            string data = response.Content.ReadAsStringAsync().Result;

            if (string.IsNullOrEmpty(data))
            {
                return(response);
            }
            try
            {
                data = _cryptor.DecryptAndVerifyData(data);
                if (data == null)
                {
                    throw new OSharpException(Resources.Http_Security_Client_VerifyResponse_Failt);
                }
                HttpContent content = new StringContent(data);
                content.Headers.ContentType = response.Content.Headers.ContentType;
                response.Content            = content;
                return(response);
            }
            catch (Exception ex)
            {
                Logger.Error(Resources.Http_Seciruty_Client_DecryptResponse_Failt, ex);
                response = response.RequestMessage.CreateErrorResponse(HttpStatusCode.InternalServerError, Resources.Http_Seciruty_Client_DecryptResponse_Failt);
                return(response);
            }
        }
コード例 #4
0
        /// <summary>
        /// 数据传到Hub之前进行数据解密
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        protected override bool OnBeforeIncoming(IHubIncomingInvokerContext context)
        {
            _canCrypto = CanCrypto(context.Hub.Context);
            if (!_canCrypto)
            {
                return(base.OnBeforeIncoming(context));
            }
            //数据解密
            string facePublicKey = context.Hub.Context.Headers.Get(HttpHeaderNames.OSharpClientPublicKey);

            if (string.IsNullOrEmpty(facePublicKey))
            {
                return(false);
            }
            _cryptor = new CommunicationCryptor(_ownPrivateKey, facePublicKey, _hashType);
            if (context.Args.Count == 1)
            {
                string         encrypt = (string)context.Args[0];
                string         json    = _cryptor.DecryptAndVerifyData(encrypt);
                IList <object> args    = JsonConvert.DeserializeObject <IList <object> >(json);
                context.Args.Clear();
                foreach (object arg in args)
                {
                    context.Args.Add(arg);
                }
            }
            return(base.OnBeforeIncoming(context));
        }
コード例 #5
0
        private Task <HttpResponseMessage> DecryptRequest(HttpRequestMessage request)
        {
            if (!request.Headers.Contains(HttpHeaderNames.TomNetClientPublicKey))
            {
                return(CreateResponseTask(request, HttpStatusCode.BadRequest, "在请求头中客户端公钥信息无法找到。"));
            }
            string publicKey = request.Headers.GetValues(HttpHeaderNames.TomNetClientPublicKey).First();

            _cryptor = new CommunicationCryptor(_privateKey, publicKey, _hashType);

            if (request.Content == null)
            {
                return(null);
            }
            string data = request.Content.ReadAsStringAsync().Result;

            if (string.IsNullOrEmpty(data))
            {
                return(null);
            }
            try
            {
                if (_cryptor != null)
                {
                    data = _cryptor.DecryptAndVerifyData(data);
                }
                if (data == null)
                {
                    throw new TomNetException("服务器解析请求数据时发生异常。");
                }
                HttpContent content = new StringContent(data);
                content.Headers.ContentType = request.Content.Headers.ContentType;
                request.Content             = content;
                return(null);
            }
            catch (CryptographicException ex)
            {
                const string message = "服务器解析传输数据时发生异常。";
                Logger.Error(message, ex);
                return(CreateResponseTask(request, HttpStatusCode.BadRequest, message, ex));
            }
            catch (Exception ex)
            {
                Logger.Error(Resources.Http_Security_Host_DecryptRequest_Failt, ex);
                return(CreateResponseTask(request, HttpStatusCode.BadRequest, Resources.Http_Security_Host_DecryptRequest_Failt, ex));
            }
        }
コード例 #6
0
        /// <summary>
        /// 接收到服务端数据之后对数据进行解密
        /// </summary>
        /// <param name="message"></param>
        protected override void OnMessageReceived(JToken message)
        {
            if (_cryptor == null)
            {
                throw new InvalidOperationException("通信加密尚未初始化。");
            }
            if (message["P"] == null && message["I"] == null)
            {
                HubInvocation invocation = message.ToObject <HubInvocation>(JsonSerializer);
                if (invocation.Args.Length == 1)
                {
                    string encrypt = invocation.Args[0].ToString();
                    if (!encrypt.StartsWith("{") && !encrypt.StartsWith("["))
                    {
                        string   json = _cryptor.DecryptAndVerifyData(encrypt);
                        JToken[] args = this.JsonDeserializeObject <JToken[]>(json);
                        message["A"] = JToken.FromObject(args, JsonSerializer);
                    }
                }
            }

            base.OnMessageReceived(message);
        }