If you want to keep the public key, why don't you just save the key content to the Java keystore? Then extract the content and convert it to a PGPPublicKey object when you need it.
Create the first shell class
public class PgpPublicKeyWrapper implements Key { private final String keyContent; public PgpPublicKeyWrapper(final String keyContent) { this.keyContent = keyContent; } @Override public String getAlgorithm() { return "PGP-PublicKey";
Then you can do it to save it
keyStore.setKeyEntry("think a name for alias", new PgpPublicKeyWrapper(key), PASSWORD, null);
If you want to get it
Key key = this.keyStore.getKey(alias, PASSWORD); InputStream is = new ByteArrayInputStream(key.getEncoded()); PGPPublicKey publicKey = readPublicKey(is);
For readPublicKey (), you can find many online examples on how to read an InputStream for a PGPPublicKey object.
source share