org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder Java Examples

The following examples show how to use org.bouncycastle.openpgp.operator.bc.BcPBESecretKeyDecryptorBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EncryptionServicePgpImpl.java    From pgptool with GNU General Public License v3.0 6 votes vote down vote up
private PGPPrivateKey getPrivateKey(String passphrase, PGPSecretKey secretKey) throws InvalidPasswordException {
	try {
		PBESecretKeyDecryptor decryptorFactory = new BcPBESecretKeyDecryptorBuilder(
				new BcPGPDigestCalculatorProvider()).build(passphrase.toCharArray());
		PGPPrivateKey privateKey = secretKey.extractPrivateKey(decryptorFactory);
		return privateKey;
	} catch (Throwable t) {
		log.warn("Failed to extract private key. Most likely it because of incorrect passphrase provided", t);
		throw new InvalidPasswordException();
	}
}
 
Example #2
Source File: PgpHelper.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Same as {@link #lookupPublicKey} but also retrieves the associated private key.
 *
 * @throws VerifyException if either keys couldn't be found.
 * @see #lookupPublicKey
 */
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(String.format("Could not load PGP private key for: %s", query), e);
  }
  return new PGPKeyPair(publicKey, privateKey);
}
 
Example #3
Source File: KeyFilesOperationsPgpImpl.java    From pgptool with GNU General Public License v3.0 5 votes vote down vote up
private PGPPrivateKey getPrivateKey(String passphrase, PGPSecretKey secretKey) throws InvalidPasswordException {
	try {
		PBESecretKeyDecryptor decryptorFactory = new BcPBESecretKeyDecryptorBuilder(
				new BcPGPDigestCalculatorProvider()).build(passphrase.toCharArray());
		PGPPrivateKey privateKey = secretKey.extractPrivateKey(decryptorFactory);
		return privateKey;
	} catch (Throwable t) {
		log.warn("Failed to extract private key. Most likely it because of incorrect passphrase provided", t);
		throw new InvalidPasswordException();
	}
}
 
Example #4
Source File: Subkey.java    From jpgpj with MIT License 5 votes vote down vote up
/**
 * Builds a secret key decryptor for the specified passphrase.
 */
protected PBESecretKeyDecryptor buildDecryptor(char[] passphraseChars) {
    char[] chars = passphraseChars != null &&
        !Arrays.equals(passphraseChars, NO_PASSPHRASE) ?
        passphraseChars : EMPTY_PASSPHRASE;
    return new BcPBESecretKeyDecryptorBuilder(
        new BcPGPDigestCalculatorProvider()).build(chars);
}
 
Example #5
Source File: KmsTestHelper.java    From nomulus with Apache License 2.0 5 votes vote down vote up
static PGPKeyPair getKeyPair() throws Exception {
  PGPSecretKey secretKey = getPrivateKeyring().getSecretKey();
  return new PGPKeyPair(
      secretKey.getPublicKey(),
      secretKey.extractPrivateKey(
          new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
          .build(new char[0])));
}
 
Example #6
Source File: KeySerializerTest.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private static PGPPrivateKey extractPrivateKey(PGPSecretKey secretKey, String password) {
  try {
    return secretKey.extractPrivateKey(
        new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
            .build(password.toCharArray()));
  } catch (PGPException e) {
    throw new Error(e);
  }
}
 
Example #7
Source File: PgpHelper.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
public static PGPPrivateKey loadPrivateKey ( final InputStream input, final String keyId, final char[] passPhrase ) throws IOException, PGPException
{
    final PGPSecretKey secretKey = loadSecretKey ( input, keyId );
    if ( secretKey == null )
    {
        return null;
    }

    return secretKey.extractPrivateKey ( new BcPBESecretKeyDecryptorBuilder ( new BcPGPDigestCalculatorProvider () ).build ( passPhrase ) );
}
 
Example #8
Source File: KeySerializer.java    From nomulus with Apache License 2.0 4 votes vote down vote up
private static PBESecretKeyDecryptor createSecretKeyDecryptor() {
  // There shouldn't be a passphrase on the key
  return new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
      .build(new char[0]);
}
 
Example #9
Source File: BouncyCastleTest.java    From nomulus with Apache License 2.0 4 votes vote down vote up
private static PGPPrivateKey extractPrivateKey(PGPSecretKey secretKey) throws PGPException {
  return secretKey.extractPrivateKey(
      new BcPBESecretKeyDecryptorBuilder(new BcPGPDigestCalculatorProvider())
          .build(PASSWORD));
}
 
Example #10
Source File: AbstractSecretKeySigningService.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
public AbstractSecretKeySigningService ( final PGPSecretKey secretKey, final String passphrase ) throws PGPException
{
    this.secretKey = secretKey;
    this.privateKey = this.secretKey.extractPrivateKey ( new BcPBESecretKeyDecryptorBuilder ( new BcPGPDigestCalculatorProvider () ).build ( passphrase.toCharArray () ) );
}