예제 #1
0
        public void SetOption(IAlipayOptions options)
        {
            if (options == null)
            {
                throw new ArgumentException(nameof(options));
            }

            //SignType私钥检查
            if (string.IsNullOrEmpty(options.SignType))
            {
                throw new Exception("您的支付宝配置未能通过检查,详细信息:签名类型未指定!");
            }

            //RSA私钥检查
            if (string.IsNullOrEmpty(options.PrivateKey))
            {
                throw new Exception("您的支付宝配置未能通过检查,详细信息:未能获取到商户私钥!");
            }

            //RSA私钥格式检查
            RSA rsaCsp = AlipaySignature.LoadCertificateString(options.PrivateKey, options.SignType);

            if (rsaCsp == null)
            {
                throw new Exception("您的支付宝配置未能通过检查,详细信息:商户私钥格式错误,未能导入!");
            }

            this.Uid             = options.Uid;
            this.AlipayPublicKey = options.AlipayPublicKey;
            this.AppId           = options.AppId;
            this.CharSet         = options.CharSet;
            this.Gatewayurl      = options.Gatewayurl;
            this.PrivateKey      = options.PrivateKey;
            this.SignType        = options.SignType;
        }
예제 #2
0
파일: TestRSA.cs 프로젝트: hnnaa/AATool
        public void Test1()
        {
            try
            {
                string prikey = "MIIEpAIBAAKCAQEAqOTxQbDUUkm/CS/eVTHgARhqCfs5mWe/FImEIDyPrsExswLUbc6pIXBulqtYOJ9Nl4fljNgXJbh4CyoUtiM5U9jf5yoPthJq667rPwtRVOIDnmNWKZNHponXjFBQs/juNkUgdX4kcWx695cWGkm+SRXCQQsKcSsqubFwLgXRP9HErfPwColtfmwVZVNXWA4ZtHTrSczbVDplMS8Fg73eprwIxYvpOFxFhAAwC4+Xw753Zd+u0P05YhXrQ/76g1YqNBkiHlOpWtwLVdL+zU9Nk5PxRGgjUqvJpDUxwjEHCEDxD4fxdgS+ml5pm0SsRKyXFm2+ZyPDlu7LQdx28kgAeQIDAQABAoIBAQCZvGQa/qIWEUjSTP9dn1uew6FqWoLwr64QPKjHSzkiwnrBzradCXuMk7ImdeuFBwTzBOGfledkD4k96F0X+fIp74TH/9JzUkp/JCbAqUWsNVtD5no8t/KFln1dHJKJb/Mt9Th5mowDrtIz4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
                string pubkey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqOTxQbDUUkm/CS/eVTHgARhqCfs5mWe/FImEIDyPrsExswLUbc6pIXBulqtYOJ9Nl4fljNgXJbh4CyoUtiM5U9jf5yoPthJq667rPwtRVOIDnmNWKZNHponXjFBQs/juNkUgdX4kcWx695cWGkm+SRXCQQsKcSsqubFwLgXRP9HErfPwColtfmwVZVNXWA4ZtHTrSczbVDplMS8Fg73eprwIxYvpOFxFhAAwC4+Xw753Zd+u0P05YhXrQ/76g1YqNBkiHlOpWtwLVdL+zU9Nk5PxRGgjUqvJpDUxwjEHCEDxD4fxdgS+ml5pm0SsRKyXFm2+ZyPDlu7LQdx28kgAeQIDAQAB";

                string abc = "abc123";

                RSACryptoServiceProvider rsaCsp = AlipaySignature.LoadCertificateString(prikey, "RSA2");
                string r1 = Convert.ToBase64String(AlipaySignature.RSAEncrypt(rsaCsp, Encoding.UTF8.GetBytes(abc)));
                string r2 = Encoding.UTF8.GetString(AlipaySignature.RSADecrypt(rsaCsp, Convert.FromBase64String(r1)));

                var sPublicKeyPEM = "-----BEGIN PUBLIC KEY-----\r\n";
                sPublicKeyPEM += pubkey;
                sPublicKeyPEM += "-----END PUBLIC KEY-----\r\n\r\n";
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                rsa.PersistKeyInCsp = false;
                RSACryptoServiceProviderExtension.LoadPublicKeyPEM(rsa, sPublicKeyPEM);
                string r11 = Convert.ToBase64String(AlipaySignature.RSAEncrypt(rsa, Encoding.UTF8.GetBytes(abc)));
                string r22 = Encoding.UTF8.GetString(AlipaySignature.RSADecrypt(rsaCsp, Convert.FromBase64String(r1)));
            }
            catch (Exception)
            {
            }
        }
        public static void Check(string signType, string privateKey)
        {
            //SignType私钥检查
            if (string.IsNullOrEmpty(signType))
            {
                throw new Exception("您的支付宝配置未能通过检查,详细信息:签名类型未指定!");
            }

            //RSA私钥检查
            if (string.IsNullOrEmpty(privateKey))
            {
                throw new Exception("您的支付宝配置未能通过检查,详细信息:未能获取到商户私钥!");
            }

            //RSA私钥格式检查
            RSA rsaCsp = AlipaySignature.LoadCertificateString(privateKey, signType);

            if (rsaCsp == null)
            {
                throw new Exception("您的支付宝配置未能通过检查,详细信息:商户私钥格式错误,未能导入!");
            }
        }