List of usage examples for org.bouncycastle.openpgp.operator.bc BcPBESecretKeyDecryptorBuilder BcPBESecretKeyDecryptorBuilder
public BcPBESecretKeyDecryptorBuilder(PGPDigestCalculatorProvider calculatorProvider)
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
@Override public void sign(final OutputStream outputStream, final InputStream inputStream, final String keyInfo) { try {/*from w w w .j ava 2s . c om*/ final File keyFile = this.secretKeyRing; final char[] pass = this.secretKeyRingPassword; final ArmoredOutputStream out = new ArmoredOutputStream(outputStream); final PGPSecretKey pgpSec = this.getSignKey(keyInfo); // readSecretKey(new // FileInputStream(keyFile)); final PGPPrivateKey pgpPrivKey = pgpSec.extractPrivateKey( new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()).build(pass)); final PGPSignatureGenerator sGen = new PGPSignatureGenerator( new BcPGPContentSignerBuilder(pgpSec.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1)); sGen.init(PGPSignature.BINARY_DOCUMENT, pgpPrivKey); final Iterator it = pgpSec.getPublicKey().getUserIDs(); if (it.hasNext()) { final PGPSignatureSubpacketGenerator spGen = new PGPSignatureSubpacketGenerator(); spGen.setSignerUserID(false, (String) it.next()); sGen.setHashedSubpackets(spGen.generate()); } final PGPCompressedDataGenerator cGen = new PGPCompressedDataGenerator(CompressionAlgorithmTags.ZLIB); final BCPGOutputStream bOut = new BCPGOutputStream(cGen.open(out)); sGen.generateOnePassVersion(false).encode(bOut); final PGPLiteralDataGenerator lGen = new PGPLiteralDataGenerator(); final byte[] buffer = new byte[1 << 16]; final OutputStream lOut = lGen.open(bOut, PGPLiteralData.BINARY, "", new Date(), buffer); int ch = 0; while ((ch = inputStream.read()) >= 0) { lOut.write(ch); sGen.update((byte) ch); } lGen.close(); sGen.generate().encode(bOut); cGen.close(); out.close(); } catch (final FileNotFoundException e) { e.printStackTrace(); } catch (final IOException e) { e.printStackTrace(); } catch (final PGPException e) { e.printStackTrace(); } catch (final SignatureException e) { e.printStackTrace(); } }
From source file:alpha.offsync.security.OpenPGPSecurityUtility.java
License:Apache License
/** * Finds the secret key of a {@link PGPSecretKeyRingCollection}. * //ww w . j a v a 2 s .c o m * @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.PgpKeyUtils.java
public static PGPPrivateKey getPrivateKeyFrom(PGPSecretKey secretKey) throws PGPException, IOException { PBESecretKeyDecryptor decryptor = new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) .build("".toCharArray()); return secretKey.extractPrivateKey(decryptor); }
From source file:com.github.sannies.nexusaptplugin.sign.PGPSigner.java
License:Apache License
public PGPSigner(InputStream keyring, String keyId, String passphrase) throws IOException, PGPException { secretKey = getSecretKey(keyring, keyId); if (secretKey == null) { throw new PGPException(String.format("Specified key %s does not exist in key ring %s", keyId, keyring)); }/*from w ww.jav a2 s . c om*/ privateKey = secretKey .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) .build(passphrase.toCharArray())); }
From source file:com.google.e2e.bcdriver.Decryptor.java
License:Apache License
static final PGPPrivateKey extractDecryptionKey(PGPSecretKeyRing pskr, String pass) throws PGPException { Iterator<PGPSecretKey> skit = Util.getTypedIterator(pskr.getSecretKeys(), PGPSecretKey.class); PGPSecretKey selected = null;//from w w w .j a va 2s. c o m // Pass #1 - use key flags on signatures. while (skit.hasNext()) { PGPSecretKey sk = skit.next(); Iterator<PGPSignature> sigit = Util.getTypedIterator(sk.getPublicKey().getSignatures(), PGPSignature.class); while (sigit.hasNext()) { if (Util.hasKeyFlag(sigit.next(), KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) { selected = sk; break; } } } if (selected == null) { // Pass #2 - use intrinsic key capabilities, but prefer subkeys // where possible. skit = Util.getTypedIterator(pskr.getSecretKeys(), PGPSecretKey.class); while (skit.hasNext()) { PGPSecretKey sk = skit.next(); if (sk.getPublicKey().isEncryptionKey()) { selected = sk; // But continue the loop, so subkeys will be chosen. } } } if (selected != null) { return selected .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) .build(pass.toCharArray())); } else { return null; } }
From source file:com.google.gerrit.gpg.testutil.TestKey.java
License:Apache License
public PGPPrivateKey getPrivateKey() throws PGPException { return getSecretKey() .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) // All test keys have no passphrase. .build(new char[0])); }
From source file:com.google.gerrit.server.git.gpg.TestKey.java
License:Apache License
PGPPrivateKey getPrivateKey() throws PGPException { return sec.extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) // All test keys have no passphrase. .build(new char[0])); }
From source file:de.dentrassi.pm.signing.pgp.internal.PgpSigningService.java
License:Open Source License
public PgpSigningService(final InputStream keyring, final String keyId, final String passphrase) throws IOException, PGPException { this.secretKey = PgpHelper.loadSecretKey(keyring, keyId); if (this.secretKey == null) { throw new IllegalStateException(String.format("Signing key '%08X' could not be found", keyId)); }/*from w ww . jav a2s .c o m*/ this.privateKey = this.secretKey .extractPrivateKey(new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) .build(passphrase.toCharArray())); }
From source file:de.dentrassi.pm.signing.pgp.web.ServiceController.java
License:Open Source License
@ControllerValidator(formDataClass = AddEntry.class) public void validateAdd(final AddEntry data, final ValidationContext context) { final String keyring = data.getKeyring(); final File file = new File(keyring); if (!file.exists()) { context.error("keyring", String.format("File '%s' does not exist on the server", file.getAbsolutePath())); return;//from ww w. j av a 2 s. c o m } if (!file.isFile()) { context.error("keyring", String.format("File '%s' is not a file", file.getAbsolutePath())); return; } if (!file.canRead()) { context.error("keyring", String.format("File '%s' cannot be read", file.getAbsolutePath())); return; } final String keyId = data.getKeyId(); if (keyId != null) { try { try (InputStream input = new FileInputStream(file)) { final PGPSecretKey key = PgpHelper.loadSecretKey(input, keyId); if (key == null) { context.error("keyId", "Key not found in keyring"); } else if (data.getKeyPassphrase() != null) { try { final PGPPrivateKey privateKey = key.extractPrivateKey( new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider()) .build(data.getKeyPassphrase().toCharArray())); if (privateKey == null) { Thread.sleep(1_000); context.error("keyPassphrase", "Unable to unlock private key"); } } catch (final Exception e) { context.error("Failed to load key. Probably a wrong phassphrase: " + ExceptionHelper.getMessage(e)); } } } } catch (final Exception e) { context.error("Failed to load key: " + ExceptionHelper.getMessage(e)); } } }
From source file:dorkbox.util.crypto.CryptoPGP.java
License:Apache License
/** * Creates the signature that will be used to PGP sign data * * @param secretKeys//from w w w .ja va 2 s . c o m * these are the secret keys * @param password * this is the password to unlock the secret key * * @return the signature used to sign data * * @throws PGPException */ private static PGPSignatureGenerator createSignature(List<PGPSecretKey> secretKeys, char[] password, int signatureType, boolean generateUserIdSubPacket) throws PGPException { PGPSecretKey secretKey = null; for (int i = 0; i < secretKeys.size(); i++) { secretKey = secretKeys.get(i); // we ONLY want the signing master key if (!secretKey.isSigningKey() || !secretKey.isMasterKey()) { secretKey = null; } } if (secretKey == null) { throw new PGPException("Secret key is not the signing master key"); } // System.err.println("Signing key = " + tmpKey.isSigningKey() +", Master key = " + tmpKey.isMasterKey() + ", UserId = " + // userId ); if (password == null) { password = new char[0]; } PBESecretKeyDecryptor build = new BcPBESecretKeyDecryptorBuilder(digestCalculatorProvider).build(password); SecureRandom random = new SecureRandom(); BcPGPContentSignerBuilder bcPGPContentSignerBuilder = new BcPGPContentSignerBuilder( secretKey.getPublicKey().getAlgorithm(), PGPUtil.SHA1).setSecureRandom(random); PGPSignatureGenerator signature = new PGPSignatureGenerator(bcPGPContentSignerBuilder); signature.init(signatureType, secretKey.extractPrivateKey(build)); Iterator userIds = secretKey.getPublicKey().getUserIDs(); // use the first userId that matches if (userIds.hasNext()) { if (generateUserIdSubPacket) { PGPSignatureSubpacketGenerator subpacketGenerator = new PGPSignatureSubpacketGenerator(); subpacketGenerator.setSignerUserID(false, (String) userIds.next()); signature.setHashedSubpackets(subpacketGenerator.generate()); } else { signature.setHashedSubpackets(null); } return signature; } else { throw new PGPException("Did not find specified userId"); } }