I am trying to create, encode, store, retrieve and decode the PGP Bouncy Castle public key. I get what seems to be the wrong output, and an EOFException when I try to read the key back. The key will be stored in the database as a string.
The RSA public encryption public key is retrieved from the key ring as follows:
@SuppressWarnings("unchecked")
public PGPPublicKey getPublicKey() {
PGPPublicKey pk = null;
Iterator<PGPPublicKey> it = publicKeyRing.getPublicKeys();
while (pk == null && it.hasNext()) {
PGPPublicKey key = it.next();
if (key.isEncryptionKey()) {
pk = key;
}
}
return pk;
}
It is encoded, ASCII armored and stored as a string as follows:
PGPPublicKey contactPK = realContact.getPublicKey();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ArmoredOutputStream armored = new ArmoredOutputStream(out);
contactPK.encode(armored);
armored.close();
publicKey = new String(out.toByteArray(), Charset.forName("US-ASCII"));
This gets me a PGP message block, where I expect a PGP public key lock:
----- BEGIN PGP MESSAGE -----\nVersion: BCPG v1.50\N\nuQINBFO8StkCEACQ4vrDnBTDjEvQkGwrAHuJSBZL8tNLxhZ9B74afhObhLVzW6ZB\nT3pk/5XcSPOTvcWd9k1yOKJUabCuF5ixFmMz + niFqUVQTtnl7aqOZ + GrDEzmoYmG\nNQROP0EiA1TWtm2 + Ja0FqiJauXytt1sIF/Pr5L47FCjtmZKVoXTP8RVFfGLPB0kT\ndjOz53PaEE3GSValh85w24XIH2/gczURUnjphCX1bRwTFr14SfA9X/rFWqv9SqWQ\nV8OiIWrSiwNd5RLJ9q0B + viDzoxrjmnMJZikxhKiuNVKJCu2ccBdMrbW42iBM2w3\
... ( )
\n ----- END PGP MESSAGE -----
, , EOFException:
ByteArrayInputStream in =
new ByteArrayInputStream(stored.publicKey.getBytes(
Charset.forName("US-ASCII")));
InputStream decoded = PGPUtil.getDecoderStream(in);
BCPGInputStream bcpgIn = new BCPGInputStream(decoded);
RSAPublicBCPGKey bcpgKey = new RSAPublicBCPGKey(bcpgIn);
PublicKeyPacket pkPacket = new PublicKeyPacket(PublicKeyAlgorithmTags.RSA_ENCRYPT,
new Date(), bcpgKey);
publicKey = new PGPPublicKey(pkPacket, new BcKeyFingerprintCalculator());
, RSAPublicBCPGKey.
- , , . - ?