List of usage examples for org.bouncycastle.openpgp PGPSecretKeyRingCollection getSecretKey
public PGPSecretKey getSecretKey(long keyID) throws PGPException
From source file:SELSKeyGen.java
License:Open Source License
/** * Load a secret key ring collection from keyIn and find the secret key corresponding to * keyID if it exists.//from ww w . ja v a2 s .c o m * * @param keyIn input stream representing a key ring collection. * @param keyID keyID we want. * @param pass passphrase to decrypt secret key with. * @return * @throws IOException * @throws PGPException * @throws NoSuchProviderException */ private static PGPPrivateKey findSecretKey(InputStream keyIn, long keyID, char[] pass) throws IOException, PGPException, NoSuchProviderException { PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn)); PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID); if (pgpSecKey == null) { return null; } return pgpSecKey.extractPrivateKey(pass, "BC"); }
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
/** * Finds the secret key of a {@link PGPSecretKeyRingCollection}. * //w w w . j a va 2s . c om * @param pgpSec * the {@link PGPSecretKeyRingCollection} * @param keyID * the key id * @param pass * the secret key password * @return the {@link PGPPrivateKey} * @throws PGPException * thrown if an error is encountered */ private PGPPrivateKey findSecretKey(final PGPSecretKeyRingCollection pgpSec, final long keyID, final char[] pass) throws PGPException { final PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID); if (pgpSecKey == null) return null; return pgpSecKey.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) .build(this.secretKeyRingPassword)); }
From source file:com.arcusx.simplepgp.PgpDataDecryptor.java
private static PGPPrivateKey findSecretKey(PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass) throws PGPException, NoSuchProviderException { PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID); if (pgpSecKey == null) { return null; }//from w w w . ja va 2s . c o m return pgpSecKey.extractPrivateKey(new JcePBESecretKeyDecryptorBuilder().setProvider("BC").build(pass)); }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Decrypt the specified (PKE) input file. * //from w ww . ja va2 s .c o m * Either pubRing and secRing should be null, or pgpSecKey should be null, but not both. * * @param out * @param inFile * @param pubRing * @param secRing * @param pgpSecKey * @param encKey * @param passwd * @param mdcRequired * @throws PGPException */ private void decryptKeyBasedFile(OutputStream out, InputStream inFile, PGPPublicKeyRingCollection pubRing, PGPSecretKeyRingCollection secRing, PGPSecretKey pgpSecKey, char[] passwd, boolean mdcRequired) throws PGPException { try { InputStream fileToDecrypt = PGPUtil.getDecoderStream(inFile); PGPObjectFactory pgpFact = new PGPObjectFactory(fileToDecrypt); Object message = pgpFact.nextObject(); PGPPublicKeyEncryptedData pked = null; // PGPCompressedData cData; // Check for signed only if (!(message instanceof PGPCompressedData)) { // // Encrypted - the first object might be a PGP marker packet. // if (!(message instanceof PGPEncryptedDataList)) { message = pgpFact.nextObject(); if (!(message instanceof PGPEncryptedDataList)) { throw new PGPException("Unrecognised PGP message type: " + message.getClass()); } } PGPEncryptedDataList enc = (PGPEncryptedDataList) message; int count = 0; // find the secret key that is needed while (count != enc.size()) { if (enc.get(count) instanceof PGPPublicKeyEncryptedData) { pked = (PGPPublicKeyEncryptedData) enc.get(count); if (pgpSecKey == null) { pgpSecKey = secRing.getSecretKey(pked.getKeyID()); if (pgpSecKey != null) { break; } } else { if (pgpSecKey.getKeyID() == pked.getKeyID()) { break; } } } count++; } if (pgpSecKey == null) { throw new PGPException("Corresponding secret key not found"); } // Check for revoked key PGPPublicKey encKey = pgpSecKey.getPublicKey(); if (encKey == null) { encKey = findPublicKey(pubRing, pgpSecKey.getKeyID(), true); } if (encKey.isRevoked()) { String keyId = Long.toHexString(encKey.getKeyID()).substring(8); System.out.println("Warning: Encryption key (0x" + keyId + ") has been revoked"); // throw new PGPException("Encryption key (0x"+keyId+") has been revoked"); } InputStream clear = pked.getDataStream(pgpSecKey.extractPrivateKey(passwd, "BC"), "BC"); PGPObjectFactory pgpClearFact = new PGPObjectFactory(clear); message = pgpClearFact.nextObject(); if (message == null) { message = pgpFact.nextObject(); } // // cData = (PGPCompressedData) pgpFact.nextObject(); // } // else { // cData = (PGPCompressedData) message; } if (message instanceof PGPCompressedData) { PGPCompressedData compressedData = (PGPCompressedData) message; pgpFact = new PGPObjectFactory(compressedData.getDataStream()); message = pgpFact.nextObject(); } // Plain file if (message instanceof PGPLiteralData) { PGPLiteralData ld = (PGPLiteralData) message; InputStream dataIn = ld.getInputStream(); int ch; while ((ch = dataIn.read()) >= 0) { out.write(ch); } out.close(); } else if (message instanceof PGPOnePassSignatureList) { // One-pass signature if (!checkOnePassSignature(out, (PGPOnePassSignatureList) message, pgpFact, pubRing)) { throw new PGPException("Signature verification failed"); } System.out.println("Signature verified"); } else if (message instanceof PGPSignatureList) { // Signature list if (!checkSignature(out, (PGPSignatureList) message, pgpFact, pubRing)) { throw new PGPException("Signature verification failed"); } System.out.println("Signature verified"); } else { // what? // System.out.println("Unrecognised message type"); throw new PGPException("Unrecognised PGP message type: " + message.getClass()); } if (pked != null) { if (pked.isIntegrityProtected()) { if (!pked.verify()) { throw new PGPException("Message failed integrity check"); } if (_verbose) { System.out.println("Message integrity check passed"); } } else { if (_verbose) { System.out.println("No message integrity check"); } if (mdcRequired) { throw new PGPException("Missing required message integrity check"); } } } } catch (PGPException e) { throw e; } catch (Exception e) { throw new PGPException("Error in decryption", e); } }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Finds the first key in secretKeyRings which is capable of signing and which corresponds with a key in keyRing. * @param keyRing/*from ww w. ja va 2 s .co m*/ * @param secretKeyRings * @return * @throws PGPException */ public PGPSecretKey findFirstSigningKey(PGPPublicKeyRing keyRing, PGPSecretKeyRingCollection secretKeyRings) throws PGPException { @SuppressWarnings("unchecked") Iterator<PGPPublicKey> kIt = keyRing.getPublicKeys(); PGPSecretKey retval = null; while (retval == null && kIt.hasNext()) { PGPPublicKey k = kIt.next(); PGPSecretKey sk = secretKeyRings.getSecretKey(k.getKeyID()); if (sk.isSigningKey()) { retval = sk; } } if (retval == null) { throw new PGPException("No signing key found"); } return retval; }
From source file:com.geekcommune.identity.EncryptionUtil.java
License:Open Source License
/** * Load a secret key ring collection from keyIn and find the secret key corresponding to * keyID if it exists.//from ww w. j av a2s. c om * * @param keyIn input stream representing a key ring collection. * @param keyID keyID we want. * @param signing indicates whether looking for a signing key. * @return * @throws IOException * @throws PGPException * @throws NoSuchProviderException */ public PGPSecretKey findSecretKey(PGPSecretKeyRingCollection secRing, long keyID, boolean signing) throws IOException, PGPException, NoSuchProviderException { PGPSecretKey pgpSecKey = secRing.getSecretKey(keyID); if (pgpSecKey != null) { if (signing && !pgpSecKey.isSigningKey()) { throw new PGPException("Key is not a signing key"); } } else { throw new PGPException("Can't find secret key in key ring"); } return pgpSecKey; }
From source file:com.ginema.crypto.encryption.PGPEncryption.java
License:Apache License
private static PGPPrivateKey findSecretKey(PGPSecretKeyRingCollection pgpSec, long keyID, char[] pass) throws PGPException, NoSuchProviderException { PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID); if (pgpSecKey == null) { return null; }// ww w. j a va2s. c o m return pgpSecKey.extractPrivateKey(pass, "BC"); }
From source file:com.lyndir.lhunath.opal.crypto.gpg.GPG.java
License:Apache License
/** * @param privateKeyFile The file that contains the private key. * @param privateKeyId The ID of the key to retrieve from the file. * * @return a private key from file.//w ww.j a v a2 s . c om * * @throws FileNotFoundException * @throws IOException * @throws PGPException */ @SuppressFBWarnings({ "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE" }) public static PGPSecretKey getPrivateKey(final File privateKeyFile, final long privateKeyId) throws IOException, PGPException { try (FileInputStream privateKeyInputStream = new FileInputStream(privateKeyFile)) { PGPSecretKeyRingCollection privateKeyRing = new PGPSecretKeyRingCollection( PGPUtil.getDecoderStream(privateKeyInputStream)); return privateKeyRing.getSecretKey(privateKeyId); } }
From source file:eu.mrbussy.security.crypto.pgp.PGPUtils.java
License:Open Source License
public static PGPPrivateKey findPrivateKey(InputStream keyIn, long keyID, char[] pass) throws IOException, PGPException, NoSuchProviderException { PGPSecretKeyRingCollection pgpSec = new PGPSecretKeyRingCollection(PGPUtil.getDecoderStream(keyIn)); PGPSecretKey pgpSecKey = pgpSec.getSecretKey(keyID); if (pgpSecKey == null) { return null; }/* w w w. j av a2s .c o m*/ return pgpSecKey.extractPrivateKey(pass, "BC"); }
From source file:google.registry.keyring.api.PgpHelper.java
License:Open Source License
/** * Same as {@link #lookupPublicKey} but also retrieves the associated private key. * * @throws VerifyException if either keys couldn't be found. * @see #lookupPublicKey//from w w w . j av a2 s. c o m */ @SuppressWarnings("deprecation") public static PGPKeyPair lookupKeyPair(PGPPublicKeyRingCollection publics, PGPSecretKeyRingCollection privates, String query, KeyRequirement want) { PGPPublicKey publicKey = lookupPublicKey(publics, query, want); PGPPrivateKey privateKey; try { PGPSecretKey secret = verifyNotNull(privates.getSecretKey(publicKey.getKeyID()), "Keyring missing private key associated with public key id: %x (query '%s')", publicKey.getKeyID(), query); // We do not support putting a password on the private key so we're just going to // put char[0] here. privateKey = secret.extractPrivateKey( new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(new char[0])); } catch (PGPException e) { throw new VerifyException(e.getMessage()); } return new PGPKeyPair(publicKey, privateKey); }