Program: 8406
Project: 8406
Buggy source:
public void buildSecretKey(ArrayList<String> userIds,ArrayList<PGPSecretKey> keys,ArrayList<Integer> keysUsages,ArrayList<GregorianCalendar> keysExpiryDates,long masterKeyId,String oldPassPhrase,String newPassPhrase) throws PgpGeneralException, NoSuchProviderException, PGPException, NoSuchAlgorithmException, SignatureException, IOException {
  Log.d(Constants.TAG,"String_Node_Str" + userIds.toString());
  updateProgress(R.string.progress_building_key,0,100);
  if (oldPassPhrase == null) {
    oldPassPhrase="String_Node_Str";
  }
  if (newPassPhrase == null) {
    newPassPhrase="String_Node_Str";
  }
  updateProgress(R.string.progress_preparing_master_key,10,100);
  int usageId=keysUsages.get(0);
  boolean canSign=(usageId & KeyFlags.SIGN_DATA) > 0;
  boolean canEncrypt=(usageId & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) > 0;
  String mainUserId=userIds.get(0);
  PGPSecretKey masterKey=keys.get(0);
  PGPPublicKey tmpKey=masterKey.getPublicKey();
  PGPPublicKey masterPublicKey=new PGPPublicKey(tmpKey.getAlgorithm(),tmpKey.getKey(new BouncyCastleProvider()),tmpKey.getCreationTime());
  PBESecretKeyDecryptor keyDecryptor=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(oldPassPhrase.toCharArray());
  PGPPrivateKey masterPrivateKey=masterKey.extractPrivateKey(keyDecryptor);
  updateProgress(R.string.progress_certifying_master_key,20,100);
  for (  String userId : userIds) {
    PGPContentSignerBuilder signerBuilder=new JcaPGPContentSignerBuilder(masterPublicKey.getAlgorithm(),HashAlgorithmTags.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
    PGPSignatureGenerator sGen=new PGPSignatureGenerator(signerBuilder);
    sGen.init(PGPSignature.POSITIVE_CERTIFICATION,masterPrivateKey);
    PGPSignature certification=sGen.generateCertification(userId,masterPublicKey);
    masterPublicKey=PGPPublicKey.addCertification(masterPublicKey,userId,certification);
  }
  PGPKeyPair masterKeyPair=new PGPKeyPair(masterPublicKey,masterPrivateKey);
  PGPSignatureSubpacketGenerator hashedPacketsGen=new PGPSignatureSubpacketGenerator();
  PGPSignatureSubpacketGenerator unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
  hashedPacketsGen.setKeyFlags(true,usageId);
  hashedPacketsGen.setPreferredSymmetricAlgorithms(true,PREFERRED_SYMMETRIC_ALGORITHMS);
  hashedPacketsGen.setPreferredHashAlgorithms(true,PREFERRED_HASH_ALGORITHMS);
  hashedPacketsGen.setPreferredCompressionAlgorithms(true,PREFERRED_COMPRESSION_ALGORITHMS);
  if (keysExpiryDates.get(0) != null) {
    GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
    creationDate.setTime(masterPublicKey.getCreationTime());
    GregorianCalendar expiryDate=keysExpiryDates.get(0);
    long numDays=(expiryDate.getTimeInMillis() / 86400000) - (creationDate.getTimeInMillis() / 86400000);
    if (numDays <= 0)     throw new PgpGeneralException(mContext.getString(R.string.error_expiry_must_come_after_creation));
    hashedPacketsGen.setKeyExpirationTime(false,numDays * 86400);
  }
 else {
    hashedPacketsGen.setKeyExpirationTime(false,0);
  }
  updateProgress(R.string.progress_building_master_key,30,100);
  PGPDigestCalculator sha1Calc=new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
  PGPContentSignerBuilder certificationSignerBuilder=new JcaPGPContentSignerBuilder(masterKeyPair.getPublicKey().getAlgorithm(),HashAlgorithmTags.SHA1);
  PBESecretKeyEncryptor keyEncryptor=new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5,sha1Calc).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(newPassPhrase.toCharArray());
  PGPKeyRingGenerator keyGen=new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,masterKeyPair,mainUserId,sha1Calc,hashedPacketsGen.generate(),unhashedPacketsGen.generate(),certificationSignerBuilder,keyEncryptor);
  updateProgress(R.string.progress_adding_sub_keys,40,100);
  for (int i=1; i < keys.size(); ++i) {
    updateProgress(40 + 50 * (i - 1) / (keys.size() - 1),100);
    PGPSecretKey subKey=keys.get(i);
    PGPPublicKey subPublicKey=subKey.getPublicKey();
    PBESecretKeyDecryptor keyDecryptor2=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(oldPassPhrase.toCharArray());
    PGPPrivateKey subPrivateKey=subKey.extractPrivateKey(keyDecryptor2);
    PGPKeyPair subKeyPair=new PGPKeyPair(subPublicKey,subPrivateKey);
    hashedPacketsGen=new PGPSignatureSubpacketGenerator();
    unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
    usageId=keysUsages.get(i);
    canSign=(usageId & KeyFlags.SIGN_DATA) > 0;
    canEncrypt=(usageId & (KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE)) > 0;
    if (canSign) {
      Date todayDate=new Date();
      hashedPacketsGen.setSignatureCreationTime(false,todayDate);
      PGPSignatureSubpacketGenerator subHashedPacketsGen=new PGPSignatureSubpacketGenerator();
      subHashedPacketsGen.setSignatureCreationTime(false,todayDate);
      PGPContentSignerBuilder signerBuilder=new JcaPGPContentSignerBuilder(subPublicKey.getAlgorithm(),PGPUtil.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
      PGPSignatureGenerator sGen=new PGPSignatureGenerator(signerBuilder);
      sGen.init(PGPSignature.PRIMARYKEY_BINDING,subPrivateKey);
      sGen.setHashedSubpackets(subHashedPacketsGen.generate());
      PGPSignature certification=sGen.generateCertification(masterPublicKey,subPublicKey);
      unhashedPacketsGen.setEmbeddedSignature(false,certification);
    }
    hashedPacketsGen.setKeyFlags(false,usageId);
    if (keysExpiryDates.get(i) != null) {
      GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
      creationDate.setTime(subPublicKey.getCreationTime());
      GregorianCalendar expiryDate=keysExpiryDates.get(i);
      long numDays=(expiryDate.getTimeInMillis() / 86400000) - (creationDate.getTimeInMillis() / 86400000);
      if (numDays <= 0)       throw new PgpGeneralException(mContext.getString(R.string.error_expiry_must_come_after_creation));
      hashedPacketsGen.setKeyExpirationTime(false,numDays * 86400);
    }
 else {
      hashedPacketsGen.setKeyExpirationTime(false,0);
    }
    keyGen.addSubKey(subKeyPair,hashedPacketsGen.generate(),unhashedPacketsGen.generate());
  }
  PGPSecretKeyRing secretKeyRing=keyGen.generateSecretKeyRing();
  PGPPublicKeyRing publicKeyRing=keyGen.generatePublicKeyRing();
  updateProgress(R.string.progress_saving_key_ring,90,100);
  ProviderHelper.saveKeyRing(mContext,secretKeyRing);
  ProviderHelper.saveKeyRing(mContext,publicKeyRing);
  updateProgress(R.string.progress_done,100,100);
}

Fixed version (ground truth):
public void buildSecretKey(ArrayList<String> userIds,ArrayList<PGPSecretKey> keys,ArrayList<Integer> keysUsages,ArrayList<GregorianCalendar> keysExpiryDates,String oldPassPhrase,String newPassPhrase) throws PgpGeneralException, PGPException, SignatureException, IOException {
  Log.d(Constants.TAG,"String_Node_Str" + userIds.toString());
  updateProgress(R.string.progress_building_key,0,100);
  if (oldPassPhrase == null) {
    oldPassPhrase="String_Node_Str";
  }
  if (newPassPhrase == null) {
    newPassPhrase="String_Node_Str";
  }
  updateProgress(R.string.progress_preparing_master_key,10,100);
  int usageId=keysUsages.get(0);
  boolean canSign;
  String mainUserId=userIds.get(0);
  PGPSecretKey masterKey=keys.get(0);
  PGPPublicKey tmpKey=masterKey.getPublicKey();
  PublicKey tmpPuK=new JcaPGPKeyConverter().setProvider(new BouncyCastleProvider()).getPublicKey(tmpKey);
  PGPPublicKey masterPublicKey=new JcaPGPKeyConverter().getPGPPublicKey(tmpKey.getAlgorithm(),tmpPuK,tmpKey.getCreationTime());
  PBESecretKeyDecryptor keyDecryptor=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(oldPassPhrase.toCharArray());
  PGPPrivateKey masterPrivateKey=masterKey.extractPrivateKey(keyDecryptor);
  updateProgress(R.string.progress_certifying_master_key,20,100);
  for (  String userId : userIds) {
    PGPContentSignerBuilder signerBuilder=new JcaPGPContentSignerBuilder(masterPublicKey.getAlgorithm(),HashAlgorithmTags.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
    PGPSignatureGenerator sGen=new PGPSignatureGenerator(signerBuilder);
    sGen.init(PGPSignature.POSITIVE_CERTIFICATION,masterPrivateKey);
    PGPSignature certification=sGen.generateCertification(userId,masterPublicKey);
    masterPublicKey=PGPPublicKey.addCertification(masterPublicKey,userId,certification);
  }
  PGPKeyPair masterKeyPair=new PGPKeyPair(masterPublicKey,masterPrivateKey);
  PGPSignatureSubpacketGenerator hashedPacketsGen=new PGPSignatureSubpacketGenerator();
  PGPSignatureSubpacketGenerator unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
  hashedPacketsGen.setKeyFlags(true,usageId);
  hashedPacketsGen.setPreferredSymmetricAlgorithms(true,PREFERRED_SYMMETRIC_ALGORITHMS);
  hashedPacketsGen.setPreferredHashAlgorithms(true,PREFERRED_HASH_ALGORITHMS);
  hashedPacketsGen.setPreferredCompressionAlgorithms(true,PREFERRED_COMPRESSION_ALGORITHMS);
  if (keysExpiryDates.get(0) != null) {
    GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
    creationDate.setTime(masterPublicKey.getCreationTime());
    GregorianCalendar expiryDate=keysExpiryDates.get(0);
    long numDays=(expiryDate.getTimeInMillis() / 86400000) - (creationDate.getTimeInMillis() / 86400000);
    if (numDays <= 0)     throw new PgpGeneralException(mContext.getString(R.string.error_expiry_must_come_after_creation));
    hashedPacketsGen.setKeyExpirationTime(false,numDays * 86400);
  }
 else {
    hashedPacketsGen.setKeyExpirationTime(false,0);
  }
  updateProgress(R.string.progress_building_master_key,30,100);
  PGPDigestCalculator sha1Calc=new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
  PGPContentSignerBuilder certificationSignerBuilder=new JcaPGPContentSignerBuilder(masterKeyPair.getPublicKey().getAlgorithm(),HashAlgorithmTags.SHA1);
  PBESecretKeyEncryptor keyEncryptor=new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5,sha1Calc).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(newPassPhrase.toCharArray());
  PGPKeyRingGenerator keyGen=new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,masterKeyPair,mainUserId,sha1Calc,hashedPacketsGen.generate(),unhashedPacketsGen.generate(),certificationSignerBuilder,keyEncryptor);
  updateProgress(R.string.progress_adding_sub_keys,40,100);
  for (int i=1; i < keys.size(); ++i) {
    updateProgress(40 + 50 * (i - 1) / (keys.size() - 1),100);
    PGPSecretKey subKey=keys.get(i);
    PGPPublicKey subPublicKey=subKey.getPublicKey();
    PBESecretKeyDecryptor keyDecryptor2=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(oldPassPhrase.toCharArray());
    PGPPrivateKey subPrivateKey=subKey.extractPrivateKey(keyDecryptor2);
    PGPKeyPair subKeyPair=new PGPKeyPair(subPublicKey,subPrivateKey);
    hashedPacketsGen=new PGPSignatureSubpacketGenerator();
    unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
    usageId=keysUsages.get(i);
    canSign=(usageId & KeyFlags.SIGN_DATA) > 0;
    if (canSign) {
      Date todayDate=new Date();
      hashedPacketsGen.setSignatureCreationTime(false,todayDate);
      PGPSignatureSubpacketGenerator subHashedPacketsGen=new PGPSignatureSubpacketGenerator();
      subHashedPacketsGen.setSignatureCreationTime(false,todayDate);
      PGPContentSignerBuilder signerBuilder=new JcaPGPContentSignerBuilder(subPublicKey.getAlgorithm(),PGPUtil.SHA1).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME);
      PGPSignatureGenerator sGen=new PGPSignatureGenerator(signerBuilder);
      sGen.init(PGPSignature.PRIMARYKEY_BINDING,subPrivateKey);
      sGen.setHashedSubpackets(subHashedPacketsGen.generate());
      PGPSignature certification=sGen.generateCertification(masterPublicKey,subPublicKey);
      unhashedPacketsGen.setEmbeddedSignature(false,certification);
    }
    hashedPacketsGen.setKeyFlags(false,usageId);
    if (keysExpiryDates.get(i) != null) {
      GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
      creationDate.setTime(subPublicKey.getCreationTime());
      GregorianCalendar expiryDate=keysExpiryDates.get(i);
      long numDays=(expiryDate.getTimeInMillis() / 86400000) - (creationDate.getTimeInMillis() / 86400000);
      if (numDays <= 0)       throw new PgpGeneralException(mContext.getString(R.string.error_expiry_must_come_after_creation));
      hashedPacketsGen.setKeyExpirationTime(false,numDays * 86400);
    }
 else {
      hashedPacketsGen.setKeyExpirationTime(false,0);
    }
    keyGen.addSubKey(subKeyPair,hashedPacketsGen.generate(),unhashedPacketsGen.generate());
  }
  PGPSecretKeyRing secretKeyRing=keyGen.generateSecretKeyRing();
  PGPPublicKeyRing publicKeyRing=keyGen.generatePublicKeyRing();
  updateProgress(R.string.progress_saving_key_ring,90,100);
  ProviderHelper.saveKeyRing(mContext,secretKeyRing);
  ProviderHelper.saveKeyRing(mContext,publicKeyRing);
  updateProgress(R.string.progress_done,100,100);
}