List of usage examples for org.bouncycastle.openpgp PGPPublicKeyRingCollection getKeyRings
public Iterator<PGPPublicKeyRing> getKeyRings()
From source file:SELSKeyGen.java
License:Open Source License
private static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException { in = PGPUtil.getDecoderStream(in);// w ww . j a v a2 s . co m PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in); // // we just loop through the collection till we find a key suitable for encryption, in the real // world you would probably want to be a bit smarter about this. // PGPPublicKey key = null; // // iterate through the key rings. // Iterator rIt = pgpPub.getKeyRings(); while (key == null && rIt.hasNext()) { PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next(); Iterator kIt = kRing.getPublicKeys(); //boolean encryptionKeyFound = false; while (key == null && kIt.hasNext()) { PGPPublicKey k = (PGPPublicKey) kIt.next(); if (k.isEncryptionKey()) { key = k; } } } if (key == null) { throw new IllegalArgumentException("Can't find encryption key in key ring."); } return key; }
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
@Override public void importCryptographyMetadata(final InputStream input) { OpenPGPSecurityUtility.LOGGER.info("Importing cryptography metadata"); try {/* w w w . j av a 2s . c om*/ PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(new FileInputStream(this.publicKeyRing))); final PGPPublicKeyRingCollection pgpPubIncoming = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(input)); PGPPublicKeyRing ppKr; final Iterator<PGPPublicKeyRing> it = pgpPubIncoming.getKeyRings(); while (it.hasNext()) { ppKr = it.next(); if (!pgpPub.contains(ppKr.getPublicKey().getKeyID())) { pgpPub = PGPPublicKeyRingCollection.addPublicKeyRing(pgpPub, ppKr); } } pgpPub.encode(new FileOutputStream(new File(this.publicKeyRing.getAbsolutePath()))); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final PGPException e) { e.printStackTrace(); } }
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
/** * Gets the correct encryption key from local public keyring using the * supplied key information.//from w w w .ja v a2s . c o m * * @param keyInfo * the supplied key information * @return the correct encryption key */ public PGPPublicKey getEncryptionKey(final String keyInfo) { PGPPublicKeyRingCollection pgpPub; try { pgpPub = new PGPPublicKeyRingCollection( PGPUtil.getDecoderStream(new FileInputStream(this.publicKeyRing))); final Iterator<PGPPublicKeyRing> keyRingIter = pgpPub.getKeyRings(); while (keyRingIter.hasNext()) { final PGPPublicKeyRing keyRing = keyRingIter.next(); final Iterator keyIter = keyRing.getPublicKeys(); while (keyIter.hasNext()) { final PGPPublicKey key = (PGPPublicKey) keyIter.next(); final Iterator idIter = key.getUserIDs(); while (idIter.hasNext()) { final String userID = idIter.next().toString(); if (userID.contains(keyInfo) && key.isEncryptionKey()) return key; } } } } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final PGPException e) { e.printStackTrace(); } return null; }
From source file:bisq.desktop.main.overlays.windows.downloadupdate.BisqInstaller.java
License:Open Source License
/** * Verifies detached PGP signatures against GPG/openPGP RSA public keys. Does currently not work with openssl or JCA/JCE keys. * * @param pubKeyFile Path to file providing the public key to use * @param sigFile Path to detached signature file * @param dataFile Path to signed data file * @return {@code true} if signature is valid, {@code false} if signature is not valid * @throws Exception throws various exceptions in case something went wrong. Main reason should be that key or * signature could be extracted from the provided files due to a "bad" format.<br> * <code>FileNotFoundException, IOException, SignatureException, PGPException</code> *///from w w w .ja va 2 s.co m public static VerifyStatusEnum verifySignature(File pubKeyFile, File sigFile, File dataFile) throws Exception { InputStream inputStream; int bytesRead; PGPPublicKey publicKey; PGPSignature pgpSignature; boolean result; // Read keys from file inputStream = PGPUtil.getDecoderStream(new FileInputStream(pubKeyFile)); PGPPublicKeyRingCollection publicKeyRingCollection = new PGPPublicKeyRingCollection(inputStream, new JcaKeyFingerprintCalculator()); inputStream.close(); Iterator<PGPPublicKeyRing> iterator = publicKeyRingCollection.getKeyRings(); PGPPublicKeyRing pgpPublicKeyRing; if (iterator.hasNext()) { pgpPublicKeyRing = iterator.next(); } else { throw new PGPException("Could not find public keyring in provided key file"); } // Would be the solution for multiple keys in one file // Iterator<PGPPublicKey> kIt; // kIt = pgpPublicKeyRing.getPublicKeys(); // publicKey = pgpPublicKeyRing.getPublicKey(0xF5B84436F379A1C6L); // Read signature from file inputStream = PGPUtil.getDecoderStream(new FileInputStream(sigFile)); PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(inputStream, new JcaKeyFingerprintCalculator()); Object o = pgpObjectFactory.nextObject(); if (o instanceof PGPSignatureList) { PGPSignatureList signatureList = (PGPSignatureList) o; checkArgument(!signatureList.isEmpty(), "signatureList must not be empty"); pgpSignature = signatureList.get(0); } else if (o instanceof PGPSignature) { pgpSignature = (PGPSignature) o; } else { throw new SignatureException("Could not find signature in provided signature file"); } inputStream.close(); log.debug("KeyID used in signature: %X\n", pgpSignature.getKeyID()); publicKey = pgpPublicKeyRing.getPublicKey(pgpSignature.getKeyID()); // If signature is not matching the key used for signing we fail if (publicKey == null) return VerifyStatusEnum.FAIL; log.debug("The ID of the selected key is %X\n", publicKey.getKeyID()); pgpSignature.init(new BcPGPContentVerifierBuilderProvider(), publicKey); // Read file to verify byte[] data = new byte[1024]; inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(dataFile))); while (true) { bytesRead = inputStream.read(data, 0, 1024); if (bytesRead == -1) break; pgpSignature.update(data, 0, bytesRead); } inputStream.close(); // Verify the signature result = pgpSignature.verify(); return result ? VerifyStatusEnum.OK : VerifyStatusEnum.FAIL; }
From source file:cc.arduino.contributions.GPGDetachedSignatureVerifier.java
License:Open Source License
private PGPPublicKey readPublicKey(InputStream input, String keyId) throws IOException, PGPException { PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input), new BcKeyFingerprintCalculator()); Iterator keyRingIter = pgpPub.getKeyRings(); while (keyRingIter.hasNext()) { PGPPublicKeyRing keyRing = (PGPPublicKeyRing) keyRingIter.next(); Iterator keyIter = keyRing.getPublicKeys(); while (keyIter.hasNext()) { PGPPublicKey key = (PGPPublicKey) keyIter.next(); if (Long.toHexString(key.getKeyID()).toUpperCase().endsWith(keyId)) { return key; }/* w w w.ja v a 2s .c om*/ } } throw new IllegalArgumentException("Can't find encryption key in key ring."); }
From source file:com.arcusx.simplepgp.PgpKeyUtils.java
@SuppressWarnings("unchecked") private static PGPPublicKey getFirstEncryptionKey(PGPPublicKeyRingCollection pgpPub) { Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings(); while (rIt.hasNext()) { PGPPublicKeyRing kRing = (PGPPublicKeyRing) rIt.next(); Iterator<PGPPublicKey> kIt = kRing.getPublicKeys(); while (kIt.hasNext()) { PGPPublicKey k = (PGPPublicKey) kIt.next(); if (k.isEncryptionKey()) { return k; }//from www . j ava 2 s . c o m } } return null; }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
public PGPPublicKeyRingCollection readPublicKeyRing(String baseDir) throws FileNotFoundException, IOException, PGPException { PGPPublicKeyRingCollection pubRings = null; PGPPublicKeyRing pgpPub = null;/* ww w . j a va2s . c om*/ // directory that contains all the .asc files File dir = new File(baseDir + "/KeyRings/Public"); // list all the files String[] children = dir.list(); if (children == null) { // Either dir does not exist or is not a directory } else { for (int i = 0; i < children.length; i++) { String filename = children[i]; log.info("File Name (.asc) " + "(" + i + ")" + " = " + filename); PGPPublicKeyRingCollection tmpKeyRingCollection = readPublicKeyRingCollection( new File(dir, filename)); if (pubRings == null) { // read the first .asc file and create the // PGPPublicKeyRingCollection to hold all the other key // rings pubRings = tmpKeyRingCollection; } else { PGPPublicKeyRingCollection otherKeyRings = tmpKeyRingCollection; @SuppressWarnings("unchecked") Iterator<PGPPublicKeyRing> rIt = otherKeyRings.getKeyRings(); while (rIt.hasNext()) { pgpPub = rIt.next(); } //TODO bobby doesn't this belong inside the loop? // copy the key ring to PGPPublicKeyCollection pubRings pubRings = PGPPublicKeyRingCollection.addPublicKeyRing(pubRings, pgpPub); } } // end of for // size should equal the number of the .asc files log.debug("Collection size = " + pubRings.size()); } // end of else return pubRings; }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Find the public key for the recipient *///from www.j a va 2s . c o m public PGPPublicKey readPublicKey(PGPPublicKeyRingCollection pubRing, String recipient, boolean encrypting) throws IOException, PGPException { // // we just loop through the collection till we find a key suitable for encryption, in the real // world you would probably want to be a bit smarter about this. // PGPPublicKey key = null; // // iterate through the key rings. // @SuppressWarnings("unchecked") Iterator<PGPPublicKeyRing> rIt = pubRing.getKeyRings(); //System.out.println("processing public key ring, looking for : "+recipient); while (key == null && rIt.hasNext()) { PGPPublicKeyRing kRing = rIt.next(); //System.out.println("Found a ring with keys "); @SuppressWarnings("unchecked") Iterator<PGPPublicKey> kIt = kRing.getPublicKeys(); //TODO bobby make sure it's safe to reuse the name from the prior key! String name = "<not specified>"; while (key == null && kIt.hasNext()) { PGPPublicKey k = kIt.next(); @SuppressWarnings("unchecked") Iterator<String> userIDs = k.getUserIDs(); // String name = "<not specified>"; if (userIDs.hasNext()) { name = userIDs.next(); } //System.out.println("found a key with name "+name); if (name.indexOf(recipient) >= 0) { if (!encrypting || k.isEncryptionKey()) { //System.out.println("Found the key I'm looking for"); key = k; } } } } if (key == null) { if (encrypting) { throw new PGPException("Can't find encryption key in key ring"); } else { throw new PGPException("Can't find signing key in key ring"); } } return key; }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Find the public keys for the recipient *///from w w w . j av a 2s . c o m public PGPPublicKeyRing findPublicKeyRing(PGPPublicKeyRingCollection pubRing, String recipient) throws IOException, PGPException { PGPPublicKeyRing retval = null; String retvalName = null; // // iterate through the key rings. // @SuppressWarnings("unchecked") Iterator<PGPPublicKeyRing> rIt = pubRing.getKeyRings(); //System.out.println("processing public key ring, looking for : "+recipient); while (rIt.hasNext()) { PGPPublicKeyRing kRing = rIt.next(); //System.out.println("Found a ring with keys "); @SuppressWarnings("unchecked") Iterator<PGPPublicKey> kIt = kRing.getPublicKeys(); while (kIt.hasNext()) { PGPPublicKey k = kIt.next(); String name = "<not specified>"; @SuppressWarnings("unchecked") Iterator<String> userIDs = k.getUserIDs(); if (userIDs.hasNext()) { name = userIDs.next(); } //System.out.println("found a key with name "+name); if (name.indexOf(recipient) >= 0) { if (retval == null || retval == kRing) { retval = kRing; retvalName = name; } else { throw new PGPException( "Ambiguous recipient name; matches both " + name + " and " + retvalName); } } } } if (retval == null) { throw new PGPException("Can't find keyring matching " + recipient); } return retval; }
From source file:com.ginema.crypto.encryption.PGPEncryption.java
License:Apache License
@SuppressWarnings("unchecked") public static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException { in = PGPUtil.getDecoderStream(in);//from ww w .j a v a2 s . co m PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(in); // // we just loop through the collection till we find a key suitable for // encryption, in the real // world you would probably want to be a bit smarter about this. // // // iterate through the key rings. // Iterator<PGPPublicKeyRing> rIt = pgpPub.getKeyRings(); while (rIt.hasNext()) { PGPPublicKeyRing kRing = rIt.next(); Iterator<PGPPublicKey> kIt = kRing.getPublicKeys(); while (kIt.hasNext()) { PGPPublicKey k = kIt.next(); if (k.isEncryptionKey()) { return k; } } } throw new IllegalArgumentException("Can't find encryption key in key ring."); }