How can I construct a java.security.PublicKey object from a base64 encoded string?

后端 未结 5 1866
生来不讨喜
生来不讨喜 2020-11-30 02:40

I have a bse64encoded string Public key from external source (Android Store) and I need to use it to verify signed content. How can I convert the string into an instance of

5条回答
  •  囚心锁ツ
    2020-11-30 02:51

    you can try this solution:

    add this dependency:

    
    org.bouncycastle
    bcpkix-jdk15on
    1.61
    

    and use this method:

    private Key parsePublicKey(String publicKey) throws IOException {
        PEMParser pemParser = new PEMParser(new StringReader(publicKey));
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfo.getInstance(pemParser.readObject());
        return converter.getPublicKey(publicKeyInfo);
    }
    

提交回复
热议问题