Program: 8373
Project: 8373
Buggy source:
public void buildSecretKey(SaveKeyringParcel saveParcel) throws PgpGeneralException, PGPException, SignatureException, IOException {
  updateProgress(R.string.progress_building_key,0,100);
  PGPSecretKey masterKey=saveParcel.keys.get(0);
  PGPSecretKeyRing mKR=ProviderHelper.getPGPSecretKeyRingByKeyId(mContext,masterKey.getKeyID());
  PGPPublicKeyRing pKR=ProviderHelper.getPGPPublicKeyRingByKeyId(mContext,masterKey.getKeyID());
  if (saveParcel.oldPassPhrase == null) {
    saveParcel.oldPassPhrase="String_Node_Str";
  }
  if (saveParcel.newPassPhrase == null) {
    saveParcel.newPassPhrase="String_Node_Str";
  }
  if (mKR == null) {
    buildNewSecretKey(saveParcel.userIDs,saveParcel.keys,saveParcel.keysExpiryDates,saveParcel.keysUsages,saveParcel.newPassPhrase,saveParcel.oldPassPhrase);
    return;
  }
  for (  PGPSecretKey dKey : saveParcel.deletedKeys) {
    mKR=PGPSecretKeyRing.removeSecretKey(mKR,dKey);
  }
  masterKey=mKR.getSecretKey();
  PGPPublicKey masterPublicKey=masterKey.getPublicKey();
  int usageId=saveParcel.keysUsages.get(0);
  boolean canSign;
  String mainUserId=saveParcel.userIDs.get(0);
  PBESecretKeyDecryptor keyDecryptor=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(saveParcel.oldPassPhrase.toCharArray());
  PGPPrivateKey masterPrivateKey=masterKey.extractPrivateKey(keyDecryptor);
  updateProgress(R.string.progress_certifying_master_key,20,100);
  for (  String delID : saveParcel.deletedIDs) {
    masterPublicKey=PGPPublicKey.removeCertification(masterPublicKey,delID);
  }
  int user_id_index=0;
  boolean anyIDChanged=false;
  if (saveParcel.primaryIDChanged) {
    anyIDChanged=true;
    ArrayList<Pair<String,PGPSignature>> sigList=new ArrayList<Pair<String,PGPSignature>>();
    for (    String userId : saveParcel.userIDs) {
      String orig_id=saveParcel.originalIDs.get(user_id_index);
      if (orig_id.equals(userId)) {
        Iterator<PGPSignature> orig_sigs=masterPublicKey.getSignaturesForID(orig_id);
        while (orig_sigs.hasNext()) {
          PGPSignature orig_sig=orig_sigs.next();
          sigList.add(new Pair<String,PGPSignature>(orig_id,orig_sig));
        }
      }
 else {
        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);
        sigList.add(new Pair<String,PGPSignature>(userId,certification));
      }
      masterPublicKey=PGPPublicKey.removeCertification(masterPublicKey,orig_id);
      user_id_index++;
    }
    for (    Pair<String,PGPSignature> to_add : sigList) {
      masterPublicKey=PGPPublicKey.addCertification(masterPublicKey,to_add.first,to_add.second);
    }
  }
 else {
    for (    String userId : saveParcel.userIDs) {
      String orig_id=saveParcel.originalIDs.get(user_id_index);
      if (!orig_id.equals(userId)) {
        anyIDChanged=true;
        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.removeCertification(masterPublicKey,orig_id);
        masterPublicKey=PGPPublicKey.addCertification(masterPublicKey,userId,certification);
      }
      user_id_index++;
    }
  }
  if (anyIDChanged) {
    pKR=PGPPublicKeyRing.insertPublicKey(pKR,masterPublicKey);
    mKR=PGPSecretKeyRing.replacePublicKeys(mKR,pKR);
  }
  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 (saveParcel.keysExpiryDates.get(0) != null) {
    GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
    creationDate.setTime(masterPublicKey.getCreationTime());
    GregorianCalendar expiryDate=saveParcel.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(saveParcel.oldPassPhrase.toCharArray());
  PGPKeyRingGenerator keyGen=new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,masterKeyPair,mainUserId,sha1Calc,hashedPacketsGen.generate(),unhashedPacketsGen.generate(),certificationSignerBuilder,keyEncryptor);
  for (int i=0; i < saveParcel.keys.size(); ++i) {
    updateProgress(40 + 50 * i / saveParcel.keys.size(),100);
    if (saveParcel.moddedKeys[i]) {
      PGPSecretKey subKey=saveParcel.keys.get(i);
      PGPPublicKey subPublicKey=subKey.getPublicKey();
      PBESecretKeyDecryptor keyDecryptor2;
      if (saveParcel.newKeys[i]) {
        keyDecryptor2=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("String_Node_Str".toCharArray());
      }
 else {
        keyDecryptor2=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(saveParcel.oldPassPhrase.toCharArray());
      }
      PGPPrivateKey subPrivateKey=subKey.extractPrivateKey(keyDecryptor2);
      PGPKeyPair subKeyPair=new PGPKeyPair(subPublicKey,subPrivateKey);
      hashedPacketsGen=new PGPSignatureSubpacketGenerator();
      unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
      usageId=saveParcel.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 (saveParcel.keysExpiryDates.get(i) != null) {
        GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
        creationDate.setTime(subPublicKey.getCreationTime());
        GregorianCalendar expiryDate=saveParcel.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 updatedSecretKeyRing=keyGen.generateSecretKeyRing();
  Iterator<PGPSecretKey> itr=updatedSecretKeyRing.getSecretKeys();
  while (itr.hasNext()) {
    PGPSecretKey theNextKey=itr.next();
    if ((theNextKey.isMasterKey() && saveParcel.moddedKeys[0]) || !theNextKey.isMasterKey()) {
      mKR=PGPSecretKeyRing.insertSecretKey(mKR,theNextKey);
      pKR=PGPPublicKeyRing.insertPublicKey(pKR,theNextKey.getPublicKey());
    }
  }
  PBESecretKeyEncryptor keyEncryptorNew=new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5,sha1Calc).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(saveParcel.newPassPhrase.toCharArray());
  mKR=PGPSecretKeyRing.copyWithNewPassword(mKR,keyDecryptor,keyEncryptorNew);
  updateProgress(R.string.progress_saving_key_ring,90,100);
  ProviderHelper.saveKeyRing(mContext,mKR);
  ProviderHelper.saveKeyRing(mContext,pKR);
  updateProgress(R.string.progress_done,100,100);
}

Fixed version (ground truth):
public void buildSecretKey(SaveKeyringParcel saveParcel) throws PgpGeneralException, PGPException, SignatureException, IOException {
  updateProgress(R.string.progress_building_key,0,100);
  PGPSecretKey masterKey=saveParcel.keys.get(0);
  PGPSecretKeyRing mKR=ProviderHelper.getPGPSecretKeyRingByKeyId(mContext,masterKey.getKeyID());
  PGPPublicKeyRing pKR=ProviderHelper.getPGPPublicKeyRingByKeyId(mContext,masterKey.getKeyID());
  if (saveParcel.oldPassPhrase == null) {
    saveParcel.oldPassPhrase="String_Node_Str";
  }
  if (saveParcel.newPassPhrase == null) {
    saveParcel.newPassPhrase="String_Node_Str";
  }
  if (mKR == null) {
    buildNewSecretKey(saveParcel.userIDs,saveParcel.keys,saveParcel.keysExpiryDates,saveParcel.keysUsages,saveParcel.newPassPhrase,saveParcel.oldPassPhrase);
    return;
  }
  if (saveParcel.deletedKeys != null) {
    for (    PGPSecretKey dKey : saveParcel.deletedKeys) {
      mKR=PGPSecretKeyRing.removeSecretKey(mKR,dKey);
    }
  }
  masterKey=mKR.getSecretKey();
  PGPPublicKey masterPublicKey=masterKey.getPublicKey();
  int usageId=saveParcel.keysUsages.get(0);
  boolean canSign;
  String mainUserId=saveParcel.userIDs.get(0);
  PBESecretKeyDecryptor keyDecryptor=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(saveParcel.oldPassPhrase.toCharArray());
  PGPPrivateKey masterPrivateKey=masterKey.extractPrivateKey(keyDecryptor);
  updateProgress(R.string.progress_certifying_master_key,20,100);
  for (  String delID : saveParcel.deletedIDs) {
    masterPublicKey=PGPPublicKey.removeCertification(masterPublicKey,delID);
  }
  int user_id_index=0;
  boolean anyIDChanged=false;
  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 (saveParcel.keysExpiryDates.get(0) != null) {
    GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
    creationDate.setTime(masterPublicKey.getCreationTime());
    GregorianCalendar expiryDate=saveParcel.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);
  }
  if (saveParcel.primaryIDChanged) {
    anyIDChanged=true;
    ArrayList<Pair<String,PGPSignature>> sigList=new ArrayList<Pair<String,PGPSignature>>();
    for (    String userId : saveParcel.userIDs) {
      String orig_id=saveParcel.originalIDs.get(user_id_index);
      if (orig_id.equals(userId)) {
        Iterator<PGPSignature> orig_sigs=masterPublicKey.getSignaturesForID(orig_id);
        while (orig_sigs.hasNext()) {
          PGPSignature orig_sig=orig_sigs.next();
          sigList.add(new Pair<String,PGPSignature>(orig_id,orig_sig));
        }
      }
 else {
        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);
        if (user_id_index == 0) {
          sGen.setHashedSubpackets(hashedPacketsGen.generate());
          sGen.setUnhashedSubpackets(unhashedPacketsGen.generate());
        }
        PGPSignature certification=sGen.generateCertification(userId,masterPublicKey);
        sigList.add(new Pair<String,PGPSignature>(userId,certification));
      }
      if (!orig_id.equals("String_Node_Str")) {
        masterPublicKey=PGPPublicKey.removeCertification(masterPublicKey,orig_id);
      }
      user_id_index++;
    }
    for (    Pair<String,PGPSignature> to_add : sigList) {
      masterPublicKey=PGPPublicKey.addCertification(masterPublicKey,to_add.first,to_add.second);
    }
  }
 else {
    for (    String userId : saveParcel.userIDs) {
      String orig_id=saveParcel.originalIDs.get(user_id_index);
      if (!orig_id.equals(userId)) {
        anyIDChanged=true;
        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);
        if (user_id_index == 0) {
          sGen.setHashedSubpackets(hashedPacketsGen.generate());
          sGen.setUnhashedSubpackets(unhashedPacketsGen.generate());
        }
        PGPSignature certification=sGen.generateCertification(userId,masterPublicKey);
        if (!orig_id.equals("String_Node_Str")) {
          masterPublicKey=PGPPublicKey.removeCertification(masterPublicKey,orig_id);
        }
        masterPublicKey=PGPPublicKey.addCertification(masterPublicKey,userId,certification);
      }
      user_id_index++;
    }
  }
  if (anyIDChanged) {
    pKR=PGPPublicKeyRing.insertPublicKey(pKR,masterPublicKey);
    mKR=PGPSecretKeyRing.replacePublicKeys(mKR,pKR);
  }
  PGPKeyPair masterKeyPair=new PGPKeyPair(masterPublicKey,masterPrivateKey);
  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(saveParcel.oldPassPhrase.toCharArray());
  PGPKeyRingGenerator keyGen=new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,masterKeyPair,mainUserId,sha1Calc,hashedPacketsGen.generate(),unhashedPacketsGen.generate(),certificationSignerBuilder,keyEncryptor);
  for (int i=1; i < saveParcel.keys.size(); ++i) {
    updateProgress(40 + 50 * i / saveParcel.keys.size(),100);
    if (saveParcel.moddedKeys[i]) {
      PGPSecretKey subKey=saveParcel.keys.get(i);
      PGPPublicKey subPublicKey=subKey.getPublicKey();
      PBESecretKeyDecryptor keyDecryptor2;
      if (saveParcel.newKeys[i]) {
        keyDecryptor2=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build("String_Node_Str".toCharArray());
      }
 else {
        keyDecryptor2=new JcePBESecretKeyDecryptorBuilder().setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(saveParcel.oldPassPhrase.toCharArray());
      }
      PGPPrivateKey subPrivateKey=subKey.extractPrivateKey(keyDecryptor2);
      PGPKeyPair subKeyPair=new PGPKeyPair(subPublicKey,subPrivateKey);
      hashedPacketsGen=new PGPSignatureSubpacketGenerator();
      unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
      usageId=saveParcel.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 (saveParcel.keysExpiryDates.get(i) != null) {
        GregorianCalendar creationDate=new GregorianCalendar(TimeZone.getTimeZone("String_Node_Str"));
        creationDate.setTime(subPublicKey.getCreationTime());
        GregorianCalendar expiryDate=saveParcel.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 updatedSecretKeyRing=keyGen.generateSecretKeyRing();
  Iterator<PGPSecretKey> itr=updatedSecretKeyRing.getSecretKeys();
  while (itr.hasNext()) {
    PGPSecretKey theNextKey=itr.next();
    if ((theNextKey.isMasterKey() && saveParcel.moddedKeys[0]) || !theNextKey.isMasterKey()) {
      mKR=PGPSecretKeyRing.insertSecretKey(mKR,theNextKey);
      pKR=PGPPublicKeyRing.insertPublicKey(pKR,theNextKey.getPublicKey());
    }
  }
  PBESecretKeyEncryptor keyEncryptorNew=new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.CAST5,sha1Calc).setProvider(Constants.BOUNCY_CASTLE_PROVIDER_NAME).build(saveParcel.newPassPhrase.toCharArray());
  mKR=PGPSecretKeyRing.copyWithNewPassword(mKR,keyDecryptor,keyEncryptorNew);
  updateProgress(R.string.progress_saving_key_ring,90,100);
  ProviderHelper.saveKeyRing(mContext,mKR);
  ProviderHelper.saveKeyRing(mContext,pKR);
  updateProgress(R.string.progress_done,100,100);
}