Пример #1
0
        public async Task EncryptMessage()
        {
            var    config    = new Helper.WeChatConfig().Value;
            string cleartext =
                @"<xml><ToUserName><![CDATA[oFQVa1aoCaRWMHh-y15eg32JXhmU]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>1348831860</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[888888]]></Content><MsgId>1234567890123456</MsgId></xml>";
            var    encrypt = MessageCryptography.AesEncrypt(cleartext, config.EncodingAesKey, config.AppId);
            var    sign    = Check.GetWeChatMessageSign(config.EncodingAesKey, "123456789", "1411034505", encrypt);
            string input   = $"<xml><Encrypt>{encrypt}</Encrypt><MsgSignature>{sign}</MsgSignature><TimeStamp>1411034505</TimeStamp><Nonce>123456789</Nonce></xml>";
            var    buffer  = Encoding.UTF8.GetBytes(input);
            Stream stream  = new MemoryStream(buffer);
            var    result  = await new WeChatMessageHandler(config, new MockCache())
                             .AddHandlerService(new MockTextMessageHandler())
                             .SetStreamMessage(stream, sign)
                             .ExecutedAsync();

            Assert.True(string.IsNullOrEmpty(result) == false, "string.IsNullOrEmpty(result) == false");
            _outputHelper.WriteLine(result);
        }
Пример #2
0
        private string FormatMessage(MessageResponse response)
        {
            var message = response.ToXml().Replace("\0", string.Empty);

            if (_messageConfig.MessageEncryptModel == EncryptModel.Encrypt)
            {
                var encryptResponse = new EncryptMessageResponse
                {
                    Nonce     = Helper.GetNonceStr(),
                    TimeStamp = Helper.GetTimestamp().ToString(CultureInfo.InvariantCulture),
                    Value     = MessageCryptography.AesEncrypt(message, _messageConfig.EncodingAesKey, _messageConfig.AppId)
                };
                string sign = Check.GetWeChatMessageSign(_messageConfig.EncodingAesKey, encryptResponse.Nonce,
                                                         encryptResponse.TimeStamp,
                                                         message);
                encryptResponse.Sign = sign;
                return(XmlSerializeHelper.ObjectToXmlString(encryptResponse).Replace("\0", string.Empty));
            }

            return(message);
        }