List of usage examples for org.bouncycastle.openpgp PGPPublicKey getPublicKeyPacket
public PublicKeyPacket getPublicKeyPacket()
From source file:bisq.common.crypto.PGP.java
License:Open Source License
@Nullable public static PGPPublicKey getPubKeyFromPem(@Nullable String pem) { if (pem != null) { InputStream inputStream = new ByteArrayInputStream(pem.getBytes(Charsets.UTF_8)); try {//from w ww .jav a2 s. c o m inputStream = PGPUtil.getDecoderStream(inputStream); try { JcaPGPPublicKeyRingCollection ringCollection = new JcaPGPPublicKeyRingCollection(inputStream); Iterator<PGPPublicKeyRing> keyRingsIterator = ringCollection.getKeyRings(); while (keyRingsIterator.hasNext()) { PGPPublicKeyRing pgpPublicKeyRing = keyRingsIterator.next(); Iterator<PGPPublicKey> pubKeysIterator = pgpPublicKeyRing.getPublicKeys(); while (pubKeysIterator.hasNext()) { final PGPPublicKey pgpPublicKey = pubKeysIterator.next(); if ((pgpPublicKey).isEncryptionKey()) { log.debug(pgpPublicKey.getClass().getName() + " KeyID: " + Long.toHexString(pgpPublicKey.getKeyID()) + " type: " + pgpPublicKey.getAlgorithm() + " fingerprint: " + new String(Hex.encode(pgpPublicKey.getFingerprint()))); BCPGKey bcKey = pgpPublicKey.getPublicKeyPacket().getKey(); log.debug(bcKey.getClass().getName()); if (bcKey instanceof RSAPublicBCPGKey) { RSAPublicBCPGKey bcRSA = (RSAPublicBCPGKey) bcKey; RSAPublicKeySpec specRSA = new RSAPublicKeySpec(bcRSA.getModulus(), bcRSA.getPublicExponent()); PublicKey jceKey = KeyFactory.getInstance("RSA").generatePublic(specRSA); // if you want to use the key in JCE, use jceKey // if you want to write "X.509" (SPKI) DER format to a file: //Files.write(new File(pubKeyAsString).toPath(), jceKey.getEncoded()); // if you want to write in PEM, bouncycastle can do that // or you can just do base64 and add BEGIN/END lines // return pubKeyAsString; // assume only one key; if need to handle multiple keys // or select other than the first, specify more clearly } return pgpPublicKey; } } } return null; } catch (PGPException | InvalidKeySpecException | NoSuchAlgorithmException e) { log.error("Error creating publicKey from pem. pem={}, error={}", pem, e); e.printStackTrace(); throw new KeyConversionException(e); } } catch (IOException e) { log.error("Error creating publicKey from pem. pem={}, error={}", pem, e); e.printStackTrace(); throw new KeyConversionException(e); } finally { try { inputStream.close(); } catch (IOException ignore) { } } } else { log.warn("Error creating publicKey from pem. pem=null"); return null; } }
From source file:com.google.e2e.bcdriver.KeyChecker.java
License:Apache License
private static final boolean isGoodDirectSignature(PGPSignature sig, PGPPublicKey signer, PGPPublicKey target, StringBuilder errors) throws PGPException, SignatureException, IOException { sig.init(new BcPGPContentVerifierBuilderProvider(), signer); boolean ok;/*w w w. j av a2s . c om*/ // There's a bug that prevents sig.verifyCertification(signer) // working for DIRECT_KEY signatures. // // So, re-implement the code again here. if (sig.getSignatureType() == PGPSignature.DIRECT_KEY) { byte[] bytes = target.getPublicKeyPacket().getEncodedContents(); sig.update((byte) 0x99); sig.update((byte) (bytes.length >> 8)); sig.update((byte) (bytes.length)); sig.update(bytes); ok = sig.verify(); } else { ok = sig.verifyCertification(target); } // If we have a good signature, also ensure the signature // hasn't expired. return ok && isSignatureCurrent(sig, errors); }
From source file:org.sufficientlysecure.keychain.pgp.SshPublicKey.java
License:Open Source License
private String encodeRSAKey(PGPPublicKey publicKey) { RSAPublicBCPGKey publicBCPGKey = (RSAPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); SshRSAPublicKey pubkey = new SshRSAPublicKey(publicBCPGKey.getPublicExponent(), publicBCPGKey.getModulus()); return pubkey.getPublicKeyBlob(); }
From source file:org.sufficientlysecure.keychain.pgp.SshPublicKey.java
License:Open Source License
private String encodeECKey(PGPPublicKey publicKey) { ECPublicBCPGKey publicBCPGKey = (ECPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); String curveName = getCurveName(publicBCPGKey); SshECDSAPublicKey sshECDSAPublicKey = new SshECDSAPublicKey(curveName, publicBCPGKey.getEncodedPoint()); return sshECDSAPublicKey.getPublicKeyBlob(); }
From source file:org.sufficientlysecure.keychain.pgp.SshPublicKey.java
License:Open Source License
private String encodeEdDSAKey(PGPPublicKey publicKey) { EdDSAPublicBCPGKey publicBCPGKey = (EdDSAPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); SshEd25519PublicKey pubkey = new SshEd25519PublicKey(publicBCPGKey.getEdDSAEncodedPoint()); return pubkey.getPublicKeyBlob(); }
From source file:org.sufficientlysecure.keychain.pgp.SshPublicKey.java
License:Open Source License
private String encodeDSAKey(PGPPublicKey publicKey) { DSAPublicBCPGKey publicBCPGKey = (DSAPublicBCPGKey) publicKey.getPublicKeyPacket().getKey(); SshDSAPublicKey sshDSAPublicKey = new SshDSAPublicKey(publicBCPGKey.getP(), publicBCPGKey.getQ(), publicBCPGKey.getG(), publicBCPGKey.getY()); return sshDSAPublicKey.getPublicKeyBlob(); }