List of usage examples for org.bouncycastle.openpgp PGPPublicKey isEncryptionKey
public boolean isEncryptionKey()
From source file:SELSKeyGen.java
License:Open Source License
private static PGPPublicKey readPublicKey(InputStream in) throws IOException, PGPException { in = PGPUtil.getDecoderStream(in);/*from w ww . j ava2s.c om*/ 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
/** * Gets the correct encryption key from local public keyring using the * supplied key information.// w ww. j a 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: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 w w w . ja v a2 s . c om } } return null; }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Find the public key for the recipient *//* w w w. j a va 2s . co 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
public PGPPublicKey findFirstEncryptingKey(PGPPublicKeyRing keyRing) throws PGPException { @SuppressWarnings("unchecked") Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys(); PGPPublicKey retval = null;//from www . j a v a 2s . c om while (retval == null && kIt.hasNext()) { PGPPublicKey k = kIt.next(); if (k.isEncryptionKey()) { //System.out.println("Found the key I'm looking for"); retval = k; } } if (retval == null) { throw new PGPException("No encrypting key found in keyring"); } return retval; }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Load a public key ring collection from keyIn and find the key corresponding to * keyID if it exists.//from w w w . jav a2s . c o m * * @param keyIn input stream representing a key ring collection. * @param keyID keyID we want. * @param encrypting whether we are encrypting or not * @return * @throws IOException * @throws PGPException * @throws NoSuchProviderException */ private static PGPPublicKey findPublicKey(PGPPublicKeyRingCollection pubRing, long keyID, boolean encrypting) throws IOException, PGPException, NoSuchProviderException { PGPPublicKey pubKey = pubRing.getPublicKey(keyID); if (pubKey != null) { if (encrypting && !pubKey.isEncryptionKey()) { throw new PGPException("Key is not an encryption key"); } } else { throw new PGPException("Can't find public key in key ring"); } return pubKey; }
From source file:com.geoxp.oss.MasterSecretGenerator.java
License:Apache License
public static Map<PGPPublicKey, byte[]> generate(List<PGPPublicKey> keys, int k, byte[] wrappedsecret) throws OSSException { if (null == keys) { throw new OSSException("Missing public PGP Public Keys."); }// ww w .j av a2 s . c o m // // Make sure each PGP public key can encrypt data // Set<String> nonEncryptionFingerprints = new HashSet<String>(); for (PGPPublicKey key : keys) { if (!key.isEncryptionKey()) { nonEncryptionFingerprints.add(new String(Hex.encode(key.getFingerprint()))); } } if (!nonEncryptionFingerprints.isEmpty()) { StringBuilder sb = new StringBuilder(); sb.append("PGP Public Keys need to be encryption keys, the following keys were not:"); for (String fpr : nonEncryptionFingerprints) { sb.append(" "); sb.append(fpr); } throw new OSSException(sb.toString()); } // // Check value of k // if (k < 1 || k > keys.size()) { throw new OSSException( "Invalid number of needed shares, was " + k + ", should have been in [1," + keys.size() + "]"); } // // Split the secret using Shamir Secret Sharing Scheme if k is > 1 // Map<PGPPublicKey, byte[]> perkeysecret = new HashMap<PGPPublicKey, byte[]>(); if (k == 1) { // // Simply encrypt the secret with each public key // for (PGPPublicKey key : keys) { try { perkeysecret.put(key, CryptoHelper.encryptPGP(wrappedsecret, key, true, "", PGPCompressedData.ZIP, PGPEncryptedData.AES_256)); } catch (IOException ioe) { throw new OSSException(ioe); } } } else { List<byte[]> secrets = CryptoHelper.SSSSSplit(wrappedsecret, keys.size(), k); for (int i = 0; i < keys.size(); i++) { PGPPublicKey key = keys.get(i); byte[] share = secrets.get(i); try { perkeysecret.put(key, CryptoHelper.encryptPGP(share, key, true, "", PGPCompressedData.ZIP, PGPEncryptedData.AES_256)); } catch (IOException ioe) { throw new OSSException(ioe); } } } return perkeysecret; }
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 w ww.j a v a 2 s.c om 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."); }
From source file:com.google.gerrit.server.contact.EncryptedContactStore.java
License:Apache License
private static PGPPublicKey selectKey(final PGPPublicKeyRingCollection rings) { for (final Iterator<?> ri = rings.getKeyRings(); ri.hasNext();) { final PGPPublicKeyRing currRing = (PGPPublicKeyRing) ri.next(); for (final Iterator<?> ki = currRing.getPublicKeys(); ki.hasNext();) { final PGPPublicKey k = (PGPPublicKey) ki.next(); if (k.isEncryptionKey()) { return k; }// w ww . ja va 2s.c o m } } return null; }
From source file:de.sandmage.opportunisticmail.crypto.OpenPGP.java
License:Open Source License
public PGPPublicKey getPublicKeyFromString(String data) { InputStream input = new ByteArrayInputStream(data.getBytes()); try {//from ww w. j a va2s . c om PGPPublicKeyRingCollection pgpPub = new PGPPublicKeyRingCollection(PGPUtil.getDecoderStream(input), new JcaKeyFingerprintCalculator()); 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 (key.isEncryptionKey()) { return key; } } } throw new IllegalArgumentException("Can't find encryption key in key ring."); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (PGPException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }