Esempio n. 1
0
        /// <summary>
        /// 通过参数配置初始化客户端
        /// 如果参数中配置了证书相关参数,需在此时初始化证书运行时环境对象,缓存证书相关上下文
        /// </summary>
        /// <param name="config">参数集合</param>
        public BaseClient(Dictionary <string, object> config)
        {
            this.Config = config;
            ArgumentValidator.CheckArgument(AlipayConstants.RSA2.Equals(GetConfig(AlipayConstants.SIGN_TYPE_CONFIG_KEY)),
                                            "Alipay Easy SDK只允许使用RSA2签名方式,RSA签名方式由于安全性相比RSA2弱已不再推荐。");

            if (!string.IsNullOrEmpty(GetConfig(AlipayConstants.ALIPAY_CERT_PATH_CONFIG_KEY)))
            {
                CertEnvironment = new CertEnvironment(
                    GetConfig(AlipayConstants.MERCHANT_CERT_PATH_CONFIG_KEY),
                    GetConfig(AlipayConstants.ALIPAY_CERT_PATH_CONFIG_KEY),
                    GetConfig(AlipayConstants.ALIPAY_ROOT_CERT_PATH_CONFIG_KEY));
            }
        }
Esempio n. 2
0
        public Context(Config config, string sdkVersion)
        {
            this.config = config.ToMap();
            SdkVersion  = sdkVersion;
            ArgumentValidator.CheckArgument(AlipayConstants.RSA2.Equals(GetConfig(AlipayConstants.SIGN_TYPE_CONFIG_KEY)),
                                            "Alipay Easy SDK只允许使用RSA2签名方式,RSA签名方式由于安全性相比RSA2弱已不再推荐。");

            if (!string.IsNullOrEmpty(GetConfig(AlipayConstants.ALIPAY_CERT_PATH_CONFIG_KEY)))
            {
                CertEnvironment = new CertEnvironment(
                    GetConfig(AlipayConstants.MERCHANT_CERT_PATH_CONFIG_KEY),
                    GetConfig(AlipayConstants.ALIPAY_CERT_PATH_CONFIG_KEY),
                    GetConfig(AlipayConstants.ALIPAY_ROOT_CERT_PATH_CONFIG_KEY));
            }
        }
        public string Sign(string content, string charset, string privateKey)
        {
            try
            {
                ArgumentValidator.CheckNotNull(content, "待签名内容不可为Null");
                ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(privateKey), "私钥不可为空");

                if (String.IsNullOrEmpty(charset))
                {
                    charset = DEFAULT_CHARSET;
                }
                return(DoSign(content, charset, privateKey));
            }
            catch (Exception ex)
            {
                String errorMessage = GetAsymmetricType() + "签名遭遇异常,请检查私钥格式是否正确。" + ex.Message +
                                      " content=" + content + ",charset=" + charset + ",privateKeySize=" + privateKey.Length;
                Console.WriteLine(errorMessage);
                throw new AopException(errorMessage, ex);
            }
        }
        public string Encrypt(string plainText, string charset, string publicKey)
        {
            try
            {
                ArgumentValidator.CheckNotNull(plainText, "密文不可为Null");
                ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(publicKey), "公钥不可为空");

                if (String.IsNullOrEmpty(charset))
                {
                    charset = DEFAULT_CHARSET;
                }
                return(DoEncrypt(plainText, charset, publicKey));
            }
            catch (Exception ex)
            {
                String errorMessage = GetAsymmetricType() + "非对称解密遭遇异常,请检查公钥格式是否正确。" + ex.Message +
                                      " plainText=" + plainText + ",charset=" + charset + ",publicKey=" + publicKey;
                Console.WriteLine(errorMessage);
                throw new AopException(errorMessage, ex);
            }
        }
        public string Decrypt(string cipherTextBase64, string charset, string privateKey)
        {
            try
            {
                ArgumentValidator.CheckNotNull(cipherTextBase64, "密文不可为Null");
                ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(privateKey), "私钥不可为空");

                if (String.IsNullOrEmpty(charset))
                {
                    charset = DEFAULT_CHARSET;
                }
                return(DoDecrypt(cipherTextBase64, charset, privateKey));
            }
            catch (Exception ex)
            {
                String errorMessage = GetAsymmetricType() + "非对称解密遭遇异常,请检查私钥格式是否正确。" + ex.Message +
                                      " cipherTextBase64=" + cipherTextBase64 + ",charset=" + charset + ",privateKeySize=" + privateKey.Length;
                Console.WriteLine(errorMessage);
                throw new AopException(errorMessage, ex);
            }
        }
        public bool Verify(string content, string charset, string publicKey, string sign)
        {
            try
            {
                ArgumentValidator.CheckNotNull(content, "待验签内容不可为Null");
                ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(publicKey), "公钥不可为空");
                ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(sign), "签名串不可为空");

                if (String.IsNullOrEmpty(charset))
                {
                    charset = DEFAULT_CHARSET;
                }
                return(DoVerify(content, charset, publicKey, sign));
            }
            catch (Exception ex)
            {
                String errorMessage = GetAsymmetricType() + "验签遭遇异常,请检查公钥格式是否正确。" + ex.Message +
                                      " content=" + content + ",charset=" + charset + ",publicKey=" + publicKey + ",sign=" + sign;
                Console.WriteLine(errorMessage);
                throw new AopException(errorMessage, ex);
            }
        }