X509证书获取PublicKey

时间:2018-06-15 06:17:14

标签: java security base64 certificate

我有一个以Base64编码的X509证书(RSA)字符串值(以MII开头......)。

我使用Base64对其进行解码并创建X509证书。

  public X509Certificate getCertificateX509(CertificateFactory factory, InputStream input) throws XX {
    try {
      return (X509Certificate) factory.generateCertificate(input);
    } catch (CertificateException e) {
      throw new XX("");
    }
  }

  public CertificateFactory getCertificateFactoryX509() throws XX {
    try {
      return CertificateFactory.getInstance("X.509");
    } catch (CertificateException e) {
      throw new XX("");
    }
  }

我不明白的是,当我x509.getPublicKey().getEncoded()时,我得到了一个字节数组。

编码到Base64我得到一个新的字符串开始与MII(与第一个不同),我假设不是PublicKey?只有证书以MII开头不是吗?

由于

2 个答案:

答案 0 :(得分:0)

我得到的证书是: MIIDKTCCAhGgAwIBAgIDC5hFMA0GCSqGSIb3DQEBCwUAMEkxCzAJBgNVBAYTAkZSMQ4wDAYDVQQKEwVFQklDUzESMBAGA1UECxMJVFJBTlNQT1JUMRYwFAYDVQQDEw1LaXRVdGlsaXRhaXJlMB4XDTE2MTEwODA5MTcyNFoXDTIxMTEwNzA5MTcyNFowSTELMAkGA1UEBhMCRlIxDjAMBgNVBAoTBUVCSUNTMRIwEAYDVQQLEwlUUkFOU1BPUlQxFjAUBgNVBAMTDUtpdFV0aWxpdGFpcmUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDBPcI+m6bb1lGrI+rZGmXj8alF8T2eM3WSN7qI5QQi/GhZDmoH8fxUUWSmHtT2Zv9Ar7tG3/1390JN2u7YF8w+yRtdgamF61ANjd7wMRAKhFc2djLZ3OSSPW4la4sUTBnAQftkekQ/0edZagQES7SvqMIkGeIlGvNTxQcpnlLl+Qb8E5y8Akh0LJLUd4Ks5pDjs86LGLtt3Q/T6x9ciNKVjp5/xbwLeRHej6ZUxmjOQLw8CmhDNsJYywWnVAaUu5KR4/4Vc25EebWvo1fVRUOxLv5L7Hx1dzjxWpXHBMbkYCXBKFDr2lMGpGxjQQ815Vhyal1nT93i2593nIiJVG+xAgMBAAGjGjAYMAkGA1UdEwQCMAAwCwYDVR0PBAQDAgZAMA0GCSqGSIb3DQEBCwUAA4IBAQCmv2wm1Nb0IXD6iMnVvFAM2ymSp/5xPBkZEFRPVmIxg+fPtX8xcDYzHX3msAcLIUry5bh6FqT2Mdd11RHdjAMucFO2CB9kjxzxdTcN8MJIkrpCybnh5Ei+O61eqanBNsg45mHxIbiEf2F3QBmZQXKEhK9Ept7lByeFhPzmhY1sD1YRu6mr5iN/J3OdIgLfPPJ6wnrOY8vZ/+qQualM2po7yBzSgzFb4vXW7CRtiIxmHvKOC1nGkI8UPW0kUfY2xjL3gwlQ0EXoCMD4/6E7RDIW9jP5l94eiJTmtBHMP/DMLsYAkEFRYjN2hGUQN4p4vQJ3P1f7hXAIf2QhULffT+J+

Base64中的x509.PublicKey.getEncoded(): MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwT3CPpum29ZRqyPq2Rpl4/GpRfE9njN1kje6iOUEIvxoWQ5qB/H8VFFkph7U9mb/QK+7Rt/9d/dCTdru2BfMPskbXYGphetQDY3e8DEQCoRXNnYy2dzkkj1uJWuLFEwZwEH7ZHpEP9HnWWoEBEu0r6jCJBniJRrzU8UHKZ5S5fkG/BOcvAJIdCyS1HeCrOaQ47POixi7bd0P0+sfXIjSlY6ef8W8C3kR3o+mVMZozkC8PApoQzbCWMsFp1QGlLuSkeP+FXNuRHm1r6NX1UVDsS7+S+x8dXc48VqVxwTG5GAlwShQ69pTBqRsY0EPNeVYcmpdZ0/d4tufd5yIiVRvsQIDAQAB

答案 1 :(得分:0)

是的。

字符串“ MII”是以base64字符串格式开头的。

如果需要公共密钥字节,则仅使用此代码:

byte[] public = x509.getPublicKey();

并获取公共密钥字符串:

byte[] public = x509.getPublicKeyString();

此函数为您返回公钥作为字节。

但是使用.getEncoded()函数会返回公钥字节的base64。