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aWNhdGVSZXZvY2F0aW9uTGlz
dD9iYXNlP29iamVjdGNsYXNzPWNSTERpc3RyaWJ1dGlvblBvaW50MA0GCSqGSIb3
DQEBBQUAA4GBAMUfsbhrAM0XDB3P1qppqa26qJCvOqngIT1DgAcZgVmLysJPvtji
fkJU4XgQVP3Hb5u3KcSyXqIip0kvpBOBbXVbc6S5/2AocNgr77sdGLdy/QQrHl7m
tbbqz8MS5b6T3S4hb7t3RTFdhH+vWHGzQSA5zBQpNztMq34qTZjfySWz
—–END CERTIFICATE—–

抽取后:
—–BEGIN PUBLIC KEY—–
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDJAX6jrQNUDzkkulBIHv6XTn0bXoBBe1q8HQhj
cD+RQiIDSH6jTffRVUjggEr9t+L8xZIhePwoFWw4gn/pQIN2LDJUgPbgjlvXo4PgYbKOi9PUIbe+
9bMhVKzl9o39/Fbhy1Ahj0cjnNnsakco7Lw7yjCs4maCQZP8GCO4qrbB1wIDAQAB
—–END PUBLIC KEY—–

代码如下:

    FileInputStream file = new FileInputStream("server.cer");
    CertificateFactory ft = CertificateFactory.getInstance("X.509");
    X509Certificate certificate = (X509Certificate)ft.generateCertificate(file);
    PublicKey publicKey = certificate.getPublicKey();
    BASE64Encoder b64 = new BASE64Encoder();
    System.out.println("-----BEGIN PUBLIC KEY-----");
    System.out.println(b64.encode(publicKey.getEncoded()) );
    System.out.println("-----END PUBLIC KEY-----");

你可能感兴趣的:(certificate,public,key,BEGIN,BEGIN)