private PublicKey readKey ( InputStream is ) { InputStreamReader isr = new InputStreamReader ( is ) ; PEMParser r = new PEMParser ( isr ) ; try { Object o = r . readObject ( ) ; if ( o instanceof PEMKeyPair ) { PEMKeyPair keyPair = ( PEMKeyPair ) o ; if ( keyPair . getPublicKeyInfo ( ) != null && keyPair . getPublicKeyInfo ( ) . getEncoded ( ) . length > 0 ) { return provider . getPublicKey ( keyPair . getPublicKeyInfo ( ) ) ; } } else if ( o instanceof SubjectPublicKeyInfo ) { return provider . getPublicKey ( ( SubjectPublicKeyInfo ) o ) ; } } catch ( IOException e ) { // Ignoring, returning null log . warn ( "Failed to get key from PEM file" , e ) ; } finally { IoUtils . closeQuietly ( isr , r ) ; } return null ; }
Read the key with bouncycastle s PEM tools