Exemplo n.º 1
0
        private string GenarateSinature(string encryptContent, CryptographyContext context)
        {
            var sortList = new SortedSet <string>
            {
                context.ServerToken,
                context.MsgTimestamp,
                context.MsgNonce,
                encryptContent
            };

            var raw = string.Join("", sortList);

            var encoding = System.Text.Encoding.ASCII;
            var hash     = _sha1Hasher.HashString(raw, encoding);

            return(hash);
        }
Exemplo n.º 2
0
        public XDocument DecryptMessage(XDocument xml, CryptographyContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var encryptElement = xml.Root?.Element(EncryptNodeName);

            if (encryptElement == null)
            {
                throw new CryptographyException(CrypographyConstant.WXBizMsgCrypt_ParseXml_Error);
            }

            var encryptContent = encryptElement.Value;

            return(DecryptingMessage(encryptContent, context));
        }
Exemplo n.º 3
0
        private string EncryptMessage(string rawMessage, CryptographyContext context)
        {
            if (context.EncodingAesKey.Length != 43)
            {
                throw new CryptographyException(CrypographyConstant.WXBizMsgCrypt_IllegalAesKey);
            }
            var raw = "";

            try
            {
                raw = _aesCrypography.AesEncrypting(rawMessage, context.EncodingAesKey, context.AppId);
            }
            catch (Exception)
            {
                throw new CryptographyException(CrypographyConstant.WXBizMsgCrypt_EncryptAES_Error);
            }

            return(raw);
        }
Exemplo n.º 4
0
        public IPassiveMessage EncryptMessage(IPassiveMessage message, CryptographyContext context)
        {
            using (var stream = new MemoryStream()) {
                message.SerializeTo(stream, context.HubContext);
                stream.Position = 0;
                using (var reader = new StreamReader(stream)) {
                    var originalMessage  = reader.ReadToEnd();
                    var encryptedMessage = EncryptMessage(originalMessage, context);
                    var signature        = GenarateSinature(originalMessage, context);

                    return(new EncryptedPassiveMessage {
                        MsgSignature = signature,
                        TimeStamp = context.MsgTimestamp,
                        Nonce = context.MsgNonce,
                        Encrypt = encryptedMessage
                    });
                }
            }
        }
Exemplo n.º 5
0
        private bool VerifySignature(string encryptContent, CryptographyContext context)
        {
            var hash = GenarateSinature(encryptContent, context);

            return(hash == context.MsgSignature);
        }