Пример #1
0
        public static bool RSACheckContent(string data, string sign, string publicKey, string signType)
        {
            var key = RSAUtilities.GetRSAParametersFormPublicKey(publicKey);

            switch (signType)
            {
            case "RSA2":
                return(SHA256WithRSA.Verify(data, sign, key));

            default:
                return(SHA1WithRSA.Verify(data, sign, key));
            }
        }
Пример #2
0
        public static string RSASignContent(string data, string privateKey, string signType)
        {
            var key = RSAUtilities.GetRSAParametersFormRsaPrivateKey(privateKey);

            switch (signType)
            {
            case "RSA2":
                return(SHA256WithRSA.Sign(data, key));

            default:
                return(SHA1WithRSA.Sign(data, key));
            }
        }
Пример #3
0
        public AlipayNotifyClient(
            IOptions <AlipayOptions> optionsAccessor,
            ILogger <AlipayNotifyClient> logger)
        {
            Options = optionsAccessor.Value;
            Logger  = logger;

            if (string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                throw new ArgumentNullException(nameof(Options.RsaPublicKey));
            }

            PublicRSAParameters = RSAUtilities.GetRSAParametersFormPublicKey(Options.RsaPublicKey);
        }
Пример #4
0
        private static void SHA256WithDSASample()
        {
            var keyParameter = DSAKeyGenerator.Generator();

            var sign = SHA256WithDSA.GenerateSignature("hello dsa",
                                                       RSAUtilities.GetAsymmetricKeyParameterFormAsn1PrivateKey(keyParameter.PrivateKey));

            Console.WriteLine($"sign:{sign}");

            var verified = SHA256WithDSA.VerifySignature("hello dsa", sign,
                                                         RSAUtilities.GetAsymmetricKeyParameterFormPublicKey(keyParameter.PublicKey));

            Console.WriteLine(verified ? "signature verified" : "signature not verified");
        }
Пример #5
0
 public AlipayNotifyClient(
     IOptions <AlipayOptions> optionsAccessor,
     ILogger <AlipayNotifyClient> logger)
 {
     this.logger = logger;
     try
     {
         Options = optionsAccessor.Value;
         if (string.IsNullOrEmpty(Options.RsaPublicKey))
         {
             throw new ArgumentNullException(nameof(Options.RsaPublicKey));
         }
         PublicRSAParameters = RSAUtilities.GetRSAParametersFormPublicKey(Options.RsaPublicKey);
     }
     catch (Exception ex)
     {
         logger?.LogError($"{DateTime.Now} 支付宝回调初始化日志报错", ex.Message);
     }
 }
Пример #6
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 key = RSAUtilities.GetAsymmetricKeyParameterFormRsaPublicKey(options.RsaPublicKey);

            var no = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_bank_no), key);

            sortedTxtParams.SetValue(WeChatPayConsts.enc_bank_no, no);

            var name = RSA_ECB_OAEPWithSHA1AndMGF1Padding.Encrypt(sortedTxtParams.GetValue(WeChatPayConsts.enc_true_name), key);

            sortedTxtParams.SetValue(WeChatPayConsts.enc_true_name, name);

            sortedTxtParams.Add(WeChatPayConsts.sign, WeChatPaySignature.SignWithKey(sortedTxtParams, options.Key, signType));
        }
Пример #7
0
        public WeChatPayClient(
            IOptions <WeChatPayOptions> optionsAccessor,
            ILogger <WeChatPayClient> logger)
        {
            Options = optionsAccessor.Value;
            Logger  = logger;
            Client  = new HttpClientEx();

            if (string.IsNullOrEmpty(Options.AppId))
            {
                throw new ArgumentNullException(nameof(Options.AppId));
            }

            if (string.IsNullOrEmpty(Options.MchId))
            {
                throw new ArgumentNullException(nameof(Options.MchId));
            }

            if (string.IsNullOrEmpty(Options.Key))
            {
                throw new ArgumentNullException(nameof(Options.Key));
            }

            if (!string.IsNullOrEmpty(Options.Certificate))
            {
                var clientHandler = new HttpClientHandler();
                clientHandler.ClientCertificates.Add(
                    File.Exists(Options.Certificate) ? new X509Certificate2(Options.Certificate, Options.MchId) :
                    new X509Certificate2(Convert.FromBase64String(Options.Certificate), Options.MchId, X509KeyStorageFlags.MachineKeySet));
                CertificateClient = new HttpClientEx(clientHandler);
            }

            if (!string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                PublicKey = RSAUtilities.GetPublicKeyParameterFormAsn1PublicKey(Options.RsaPublicKey);
            }
        }
Пример #8
0
        public LianLianPayNotifyClient(
            IOptions <LianLianPayOptions> optionsAccessor,
            ILogger <LianLianPayClient> logger)
        {
            Options = optionsAccessor?.Value ?? new LianLianPayOptions();
            Logger  = logger;

            if (string.IsNullOrEmpty(Options.OidPartner))
            {
                throw new ArgumentNullException(nameof(Options.OidPartner));
            }

            if (string.IsNullOrEmpty(Options.BusiPartner))
            {
                throw new ArgumentNullException(nameof(Options.BusiPartner));
            }

            if (string.IsNullOrEmpty(Options.RsaPublicKey))
            {
                throw new ArgumentNullException(nameof(Options.RsaPublicKey));
            }

            PublicKey = RSAUtilities.GetKeyParameterFormPublicKey(Options.RsaPublicKey);
        }
 /// <summary>
 /// Initializes a new instance of the Tikkie API configuration.
 /// </summary>
 /// <param name="apiKey">The API key that allows the usage of the Tikkie API</param>
 /// <param name="privateKeyPath">The directory path where the RSA private key PEM file can be found.</param>
 /// <param name="useTestEnvironment">True if the connexion is made to the development/sandbox Tikkie API environment. False by default.</param>
 /// <exception cref="ArgumentException">If the API Key is null or empty.</exception>
 /// <exception cref="FileNotFoundException">If the private key pem file doesn't exist in the specified location.</exception>
 /// <exception cref="InvalidDataException">If the RSA private key PEM file cannot be read.</exception>
 public TikkieConfiguration(string apiKey, string privateKeyPath, bool useTestEnvironment = false)
 {
     ApiKey            = string.IsNullOrEmpty(apiKey) ? throw new ArgumentException(nameof(apiKey)) : apiKey;
     RSAKey            = RSAUtilities.GetPrivateKeyFromPemFile(privateKeyPath);
     IsTestEnvironment = useTestEnvironment;
 }
 public void GetPrivateKeyFromPemFile_FileIsNotAnRSAFile_ExpectedInvalidDataException()
 {
     // Arrange + Act + Assert
     Assert.Throws <InvalidDataException>(() => RSAUtilities.GetPrivateKeyFromPemFile("non_rsa.pem"));
 }
 public void GetPrivateKeyFromPemFile_FileDoesNotExist_ExpectedFileNotFoundException()
 {
     // Arrange + Act + Assert
     Assert.Throws <FileNotFoundException>(() => RSAUtilities.GetPrivateKeyFromPemFile("non/existing/path/file.pem"));
 }