예제 #1
0
        /// <summary>
        /// 加密敏感信息字段
        /// </summary>
        /// <remarks>
        /// <para><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_3.shtml">敏感信息加解密</a></para>
        /// </remarks>
        private static void EncryptPrivacyProperty(WeChatPayObject obj, RSA rsa)
        {
            foreach (var propertyInfo in obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (propertyInfo.PropertyType == typeof(string))                                  // 为string类型
                {
                    if (propertyInfo.IsDefined(typeof(WeChatPayPrivacyPropertyAttribute), false)) // 是否标记为敏感信息
                    {
                        var value = propertyInfo.GetValue(obj, null);                             // 获取值
                        if (value is string strValue)
                        {
                            if (string.IsNullOrEmpty(strValue))
                            {
                                continue; // 跳过空字符串
                            }

                            // 加密并将加密串设置回对象
                            var strEncrypted = OaepSHA1WithRSA.Encrypt(rsa, strValue);
                            propertyInfo.SetValue(obj, strEncrypted, null);
                        }
                    }
                }
                else if (propertyInfo.PropertyType.IsClass)
                {
                    var value = propertyInfo.GetValue(obj, null); // 获取子对象
                    if (value is null)
                    {
                        continue; // 跳过空值
                    }

                    if (value is WeChatPayObject weChatPayObject)
                    {
                        EncryptPrivacyProperty(weChatPayObject, rsa); // 继续加密
                    }
                    else if (value is IList list)                     // 获取列表对象
                    {
                        foreach (var item in list)
                        {
                            if (item is WeChatPayObject wcpObj)
                            {
                                EncryptPrivacyProperty(wcpObj, rsa); // 继续加密
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        continue; // 跳过其他类型
                    }
                }
            }
        }
예제 #2
0
        public void PrimaryHandler(WeChatPayDictionary sortedTxtParams, WeChatPayOptions options)
        {
            if (string.IsNullOrEmpty(options.RsaPublicKey))
            {
                throw new WeChatPayException($"{nameof(WeChatPayPayBankRequest)}.{nameof(PrimaryHandler)}: {nameof(options.RsaPublicKey)} is empty!");
            }

            sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr());
            sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId);

            sortedTxtParams.Add(WeChatPayConsts.enc_bank_no, OaepSHA1WithRSA.Encrypt(BankNo, options.RsaPublicKey));
            sortedTxtParams.Add(WeChatPayConsts.enc_true_name, OaepSHA1WithRSA.Encrypt(TrueName, options.RsaPublicKey));

            sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.APIKey, signType));
        }
예제 #3
0
        /// <summary>
        /// 加密敏感信息字段
        /// </summary>
        /// <remarks>
        /// <para><a href="https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_3.shtml">敏感信息加解密</a></para>
        /// </remarks>
        private static void EncryptPrivacyProperty(object obj, RSA rsa)
        {
            foreach (var prop in obj.GetType().GetProperties())
            {
                if (prop.PropertyType == typeof(string)) // 为string类型
                {
                    var val = prop.GetValue(obj, null);  // 获取值
                    if (val is null)                     // 跳过空值
                    {
                        continue;
                    }

                    if (prop.IsDefined(typeof(WeChatPayPrivacyPropertyAttribute), false)) // 是否标记为敏感信息
                    {
                        if (val is string str)
                        {
                            if (string.IsNullOrEmpty(str)) // 跳过空字符串
                            {
                                continue;
                            }

                            // 加密并将加密串设置回对象。
                            var encStr = OaepSHA1WithRSA.Encrypt(rsa, str);
                            prop.SetValue(obj, encStr, null);
                        }
                    }
                }
                else if (prop.PropertyType.IsClass)
                {
                    var subObj = prop.GetValue(obj, null); // 获取子对象
                    if (subObj is null)                    // 跳过空值
                    {
                        continue;
                    }

                    if (subObj is not WeChatPayObject) // 跳过除WeChatPayObject和基类为WeChatPayObject以外的一切类型。
                    {
                        continue;
                    }

                    EncryptPrivacyProperty(subObj, rsa); // 继续加密子对象
                }
            }
        }
예제 #4
0
        public void PrimaryHandler(WeChatPayOptions options, WeChatPaySignType signType, WeChatPayDictionary sortedTxtParams)
        {
            if (string.IsNullOrEmpty(options.RsaPublicKey))
            {
                throw new WeChatPayException("WeChatPayPayBankRequest: RsaPublicKey is null!");
            }

            sortedTxtParams.Add(WeChatPayConsts.nonce_str, WeChatPayUtility.GenerateNonceStr());
            sortedTxtParams.Add(WeChatPayConsts.mch_id, options.MchId);

            var no = OaepSHA1WithRSA.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_bank_no), options.RsaPublicKey);

            sortedTxtParams.SetValue(WeChatPayConsts.enc_bank_no, no);

            var name = OaepSHA1WithRSA.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_true_name), options.RsaPublicKey);

            sortedTxtParams.SetValue(WeChatPayConsts.enc_true_name, name);

            sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType));
        }