博主
258
258
258
258
专辑

第十节 查看证书序列号

亮子 2021-08-23 14:17:56 5575 0 0 0

1、什么是证书序列号

每个证书都有一个由CA颁发的唯一编号,即证书序列号。

2、如何查看证书序列号?

  • 登陆商户平台【API安全】->【API证书】->【查看证书】,可查看商户API证书序列号。

图片alt

  • 商户API证书和微信支付平台证书均可以使用第三方的证书解析工具,查看证书内容。或者使用openssl命令行工具查看证书序列号。
$ openssl x509 -in 1900009191_20180326_cert.pem -noout -serial
serial=1DDE55AD98ED71D6EDD4A4A16996DE7B47773A8C

3、如何在程序中加载证书

推荐使用微信支付提供的SDK。你也可以查看下列编程语言的示例代码。

  /**
   * 获取证书。
   *
   * @param filename 证书文件路径  (required)
   * @return X509证书
   */
  public static X509Certificate getCertificate(String filename) throws IOException {
    InputStream fis = new FileInputStream(filename);
    BufferedInputStream bis = new BufferedInputStream(fis);

    try {
      CertificateFactory cf = CertificateFactory.getInstance("X509");
      X509Certificate cert = (X509Certificate) cf.generateCertificate(bis);
      cert.checkValidity();
      return cert;
    } catch (CertificateExpiredException e) {
      throw new RuntimeException("证书已过期", e);
    } catch (CertificateNotYetValidException e) {
      throw new RuntimeException("证书尚未生效", e);
    } catch (CertificateException e) {
      throw new RuntimeException("无效的证书文件", e);
    } finally {
      bis.close();
    }
  }