// First key is the Master Key
@Override
      public KeyringConfig build()
          throws NoSuchAlgorithmException, PGPException, NoSuchProviderException,
          InvalidAlgorithmParameterException, IOException {
        // Hash Calculator
        final PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder()
            .setProvider(BouncyCastleProvider.PROVIDER_NAME)
            .build()
            .get(PGPHashAlgorithms.SHA1.getAlgorithmId());
        // Encryptor for encrypting secret keys
        final boolean withPassphrase = !passphrase.isEmpty();
        @Nullable final PBESecretKeyEncryptor encryptor;
        if (withPassphrase) {
          // AES-256 encrypted
          encryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator)
              .setProvider(BouncyCastleProvider.PROVIDER_NAME)
              .build(passphrase.getChars());
        } else {
          // unencrypted key pair
          encryptor = null;
        }
        // First key is the Master Key
        final KeySpec certKeySpec = keySpecs.get(0);
        // Remove master key, so that we later only add sub keys.
        keySpecs.remove(0);
        // Generate Master Key
        final PGPKeyPair certKey = generateKeyPair(certKeySpec);
        // Signer for creating self-signature
        final PGPContentSignerBuilder signer = new JcaPGPContentSignerBuilder(
            certKey.getPublicKey().getAlgorithm(), PGPHashAlgorithms.SHA_512.getAlgorithmId())
            .setProvider(BouncyCastleProvider.PROVIDER_NAME);
        final PGPSignatureSubpacketVector hashedSubPackets = certKeySpec.getSubpackets();
        // Generator which the user can get the key pair from
        final PGPKeyRingGenerator ringGenerator = new PGPKeyRingGenerator(
            PGPSignature.POSITIVE_CERTIFICATION, certKey,
            userId, calculator,
            hashedSubPackets, null, signer, encryptor);
        for (final KeySpec subKeySpec : keySpecs) {
          final PGPKeyPair subKey = generateKeyPair(subKeySpec);
          if (subKeySpec.isInheritedSubPackets()) {
            ringGenerator.addSubKey(subKey);
          } else {
            ringGenerator.addSubKey(subKey, subKeySpec.getSubpackets(), null);
          }
        }
        final PGPPublicKeyRing publicKeys = ringGenerator.generatePublicKeyRing();
        PGPSecretKeyRing secretKeys = ringGenerator.generateSecretKeyRing();
        // TODO: Remove once BC 1.61 is released
        final PBESecretKeyDecryptor decryptor;
        if (withPassphrase) {
          // AES-256 encrypted
          decryptor = new JcePBESecretKeyDecryptorBuilder(
              new JcaPGPDigestCalculatorProviderBuilder()
                  .setProvider(BouncyCastleProvider.PROVIDER_NAME)
                  .build()).build(passphrase.getChars());
        } else {
          // unencrypted key pair
          decryptor = null;
        }
        secretKeys = KeyRingSubKeyFixUtil.repairSubkeyPackets(secretKeys, decryptor, encryptor);
        final InMemoryKeyring keyring;
        if (passphrase.isEmpty()) {
          keyring = KeyringConfigs
              .forGpgExportedKeys(KeyringConfigCallbacks.withUnprotectedKeys());
        } else {
          keyring = KeyringConfigs
              .forGpgExportedKeys(KeyringConfigCallbacks.withPassword(passphrase.getChars()));
        }
        keyring.addSecretKeyRing(secretKeys);
        keyring.addPublicKeyRing(publicKeys);
        passphrase.clear();
        return keyring;
      }
encryptor = null;
}
// First key is the Master Key
final KeySpec certKeySpec = keySpecs.get(0);
// Remove master key, so that we later only add sub keys.
@Nullable final PBESecretKeyEncryptor encryptor;
if (withPassphrase) {
// AES-256 encrypted
encryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator)
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build(passphrase.getChars());
} else {
// unencrypted key pair
encryptor = null;
}
// First key is the Master Key
final KeySpec certKeySpec = keySpecs.get(0);
// Remove master key, so that we later only add sub keys.
keySpecs.remove(0);
// Generate Master Key
final PGPKeyPair certKey = generateKeyPair(certKeySpec);
// Signer for creating self-signature
final PGPContentSignerBuilder signer = new JcaPGPContentSignerBuilder(
certKey.getPublicKey().getAlgorithm(), PGPHashAlgorithms.SHA_512.getAlgorithmId())
.setProvider(BouncyCastleProvider.PROVIDER_NAME);
final PGPSignatureSubpacketVector hashedSubPackets = certKeySpec.getSubpackets();
public KeyringConfig build()
throws NoSuchAlgorithmException, PGPException, NoSuchProviderException,
InvalidAlgorithmParameterException, IOException {
// Hash Calculator
final PGPDigestCalculator calculator = new JcaPGPDigestCalculatorProviderBuilder()
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build()
.get(PGPHashAlgorithms.SHA1.getAlgorithmId());
// Encryptor for encrypting secret keys
final boolean withPassphrase = !passphrase.isEmpty();
@Nullable final PBESecretKeyEncryptor encryptor;
if (withPassphrase) {
// AES-256 encrypted
encryptor = new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, calculator)
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build(passphrase.getChars());
} else {
// unencrypted key pair
encryptor = null;
}
// First key is the Master Key
final KeySpec certKeySpec = keySpecs.get(0);
// Remove master key, so that we later only add sub keys.
keySpecs.remove(0);
// Generate Master Key
final PGPKeyPair certKey = generateKeyPair(certKeySpec);
// Signer for creating self-signature
final PGPContentSignerBuilder signer = new JcaPGPContentSignerBuilder(
certKey.getPublicKey().getAlgorithm(), PGPHashAlgorithms.SHA_512.getAlgorithmId())
.setProvider(BouncyCastleProvider.PROVIDER_NAME);
final PGPSignatureSubpacketVector hashedSubPackets = certKeySpec.getSubpackets();
// Generator which the user can get the key pair from
final PGPKeyRingGenerator ringGenerator = new PGPKeyRingGenerator(
PGPSignature.POSITIVE_CERTIFICATION, certKey,
userId, calculator,
hashedSubPackets, null, signer, encryptor);
for (final KeySpec subKeySpec : keySpecs) {
final PGPKeyPair subKey = generateKeyPair(subKeySpec);
if (subKeySpec.isInheritedSubPackets()) {
ringGenerator.addSubKey(subKey);
} else {