/// <summary> /// 从支付宝公钥证书中提取支付宝公钥 /// </summary> /// <param name="certPath">证书路径</param> /// <returns>公钥字符串</returns> public static string ExtractPemPublicKeyFromCert(string certPath) { ArgumentValidator.CheckArgument(!String.IsNullOrEmpty(certPath), "证书文件路径不可为空。"); X509Certificate alipayPublicKeyCert = AntCertificationUtil.ParseCert(File.ReadAllText(certPath)); return(AntCertificationUtil.ExtractPemPublicKeyFromCert(alipayPublicKeyCert)); }
/// <summary> /// 初始化证书所需运行时参数 /// </summary> /// <param name="certParams">用户传入的证书相关参数</param> /// <param name="signType">算法类型</param> public CertEnvironment(CertParams certParams, string signType) { //解析支付宝根证书序列号 if (!string.IsNullOrEmpty(certParams.RootCertContent)) { this.RootCertContent = certParams.RootCertContent; } else { this.RootCertContent = File.ReadAllText(certParams.RootCertPath); } this.RootCertSN = AntCertificationUtil.GetRootCertSN(RootCertContent, signType); //解析应用证书序列号 if (!string.IsNullOrEmpty(certParams.AppCertContent)) { X509Certificate appCert = AntCertificationUtil.ParseCert(certParams.AppCertContent); this.AppCertSN = AntCertificationUtil.GetCertSN(appCert); } else { X509Certificate appCert = AntCertificationUtil.ParseCert(File.ReadAllText(certParams.AppCertPath)); this.AppCertSN = AntCertificationUtil.GetCertSN(appCert); } //解析支付宝公钥证书序列号 X509Certificate alipayPublicKeyCert; if (!string.IsNullOrEmpty(certParams.AlipayPublicCertContent)) { alipayPublicKeyCert = AntCertificationUtil.ParseCert(certParams.AlipayPublicCertContent); } else { alipayPublicKeyCert = AntCertificationUtil.ParseCert(File.ReadAllText(certParams.AlipayPublicCertPath)); } string alipayPublicKeyCertSN = AntCertificationUtil.GetCertSN(alipayPublicKeyCert); //解析支付宝公钥 string alipayPublicKey = AntCertificationUtil.ExtractPemPublicKeyFromCert(alipayPublicKeyCert); //缓存支付宝公钥证书序号对应的支付宝公钥 AddNewAlipayPublicKey(alipayPublicKeyCertSN, alipayPublicKey); }
/// <summary> /// 获取公钥证书序列号 /// </summary> /// <param name="certPath">公钥证书路径</param> /// <returns>公钥证书序列号</returns> public static String GetCertSN(String certPath) { X509Certificate cert = AntCertificationUtil.ParseCert(File.ReadAllText(certPath)); return(AntCertificationUtil.GetCertSN(cert)); }