Program: 8447
Project: 8447
Buggy source:
public static void buildSecretKey(Activity context,SectionView userIdsView,SectionView keysView,String oldPassPhrase,String newPassPhrase,ProgressDialogUpdater progress) throws Apg.GeneralException, NoSuchProviderException, PGPException, NoSuchAlgorithmException, SignatureException {
  progress.setProgress("String_Node_Str",0,100);
  Security.addProvider(new BouncyCastleProvider());
  if (oldPassPhrase == null || oldPassPhrase.equals("String_Node_Str")) {
    oldPassPhrase="String_Node_Str";
  }
  if (newPassPhrase == null || newPassPhrase.equals("String_Node_Str")) {
    newPassPhrase="String_Node_Str";
  }
  Vector<String> userIds=new Vector<String>();
  Vector<PGPSecretKey> keys=new Vector<PGPSecretKey>();
  ViewGroup userIdEditors=userIdsView.getEditors();
  ViewGroup keyEditors=keysView.getEditors();
  boolean gotMainUserId=false;
  for (int i=0; i < userIdEditors.getChildCount(); ++i) {
    UserIdEditor editor=(UserIdEditor)userIdEditors.getChildAt(i);
    String userId=null;
    try {
      userId=editor.getValue();
    }
 catch (    UserIdEditor.NoNameException e) {
      throw new Apg.GeneralException("String_Node_Str");
    }
catch (    UserIdEditor.NoEmailException e) {
      throw new Apg.GeneralException("String_Node_Str");
    }
catch (    UserIdEditor.InvalidEmailException e) {
      throw new Apg.GeneralException(e.getMessage());
    }
    if (userId.equals("String_Node_Str")) {
      continue;
    }
    if (editor.isMainUserId()) {
      userIds.insertElementAt(userId,0);
      gotMainUserId=true;
    }
 else {
      userIds.add(userId);
    }
  }
  if (userIds.size() == 0) {
    throw new Apg.GeneralException("String_Node_Str");
  }
  if (!gotMainUserId) {
    throw new Apg.GeneralException("String_Node_Str");
  }
  if (keyEditors.getChildCount() == 0) {
    throw new Apg.GeneralException("String_Node_Str");
  }
  for (int i=0; i < keyEditors.getChildCount(); ++i) {
    KeyEditor editor=(KeyEditor)keyEditors.getChildAt(i);
    keys.add(editor.getValue());
  }
  progress.setProgress("String_Node_Str",10,100);
  KeyEditor keyEditor=(KeyEditor)keyEditors.getChildAt(0);
  int usageId=keyEditor.getUsage();
  boolean canSign=(usageId == Id.choice.usage.sign_only || usageId == Id.choice.usage.sign_and_encrypt);
  boolean canEncrypt=(usageId == Id.choice.usage.encrypt_only || usageId == Id.choice.usage.sign_and_encrypt);
  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());
  PGPPrivateKey masterPrivateKey=masterKey.extractPrivateKey(oldPassPhrase.toCharArray(),new BouncyCastleProvider());
  progress.setProgress("String_Node_Str",20,100);
  for (int i=0; i < userIds.size(); ++i) {
    String userId=userIds.get(i);
    PGPSignatureGenerator sGen=new PGPSignatureGenerator(masterPublicKey.getAlgorithm(),HashAlgorithmTags.SHA1,new BouncyCastleProvider());
    sGen.initSign(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();
  int keyFlags=KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA;
  if (canEncrypt) {
    keyFlags|=KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE;
  }
  hashedPacketsGen.setKeyFlags(true,keyFlags);
  hashedPacketsGen.setPreferredSymmetricAlgorithms(true,PREFERRED_SYMMETRIC_ALGORITHMS);
  hashedPacketsGen.setPreferredHashAlgorithms(true,PREFERRED_HASH_ALGORITHMS);
  hashedPacketsGen.setPreferredCompressionAlgorithms(true,PREFERRED_COMPRESSION_ALGORITHMS);
  if (keyEditor.getExpiryDate() != null) {
    GregorianCalendar creationDate=new GregorianCalendar();
    creationDate.setTime(getCreationDate(masterKey));
    GregorianCalendar expiryDate=keyEditor.getExpiryDate();
    long numDays=getNumDatesBetween(creationDate,expiryDate);
    if (numDays <= 0) {
      throw new GeneralException("String_Node_Str");
    }
    hashedPacketsGen.setKeyExpirationTime(true,numDays * 86400);
  }
  progress.setProgress("String_Node_Str",30,100);
  PGPKeyRingGenerator keyGen=new PGPKeyRingGenerator(PGPSignature.DEFAULT_CERTIFICATION,masterKeyPair,mainUserId,PGPEncryptedData.CAST5,newPassPhrase.toCharArray(),hashedPacketsGen.generate(),unhashedPacketsGen.generate(),new SecureRandom(),new BouncyCastleProvider().getName());
  progress.setProgress("String_Node_Str",40,100);
  for (int i=1; i < keys.size(); ++i) {
    progress.setProgress(40 + 50 * (i - 1) / (keys.size() - 1),100);
    PGPSecretKey subKey=keys.get(i);
    keyEditor=(KeyEditor)keyEditors.getChildAt(i);
    PGPPublicKey subPublicKey=subKey.getPublicKey();
    PGPPrivateKey subPrivateKey=subKey.extractPrivateKey(oldPassPhrase.toCharArray(),new BouncyCastleProvider());
    PGPKeyPair subKeyPair=new PGPKeyPair(subPublicKey.getAlgorithm(),subPublicKey.getKey(new BouncyCastleProvider()),subPrivateKey.getKey(),subPublicKey.getCreationTime());
    hashedPacketsGen=new PGPSignatureSubpacketGenerator();
    unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
    keyFlags=0;
    usageId=keyEditor.getUsage();
    canSign=(usageId == Id.choice.usage.sign_only || usageId == Id.choice.usage.sign_and_encrypt);
    canEncrypt=(usageId == Id.choice.usage.encrypt_only || usageId == Id.choice.usage.sign_and_encrypt);
    if (canSign) {
      keyFlags|=KeyFlags.SIGN_DATA;
    }
    if (canEncrypt) {
      keyFlags|=KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE;
    }
    hashedPacketsGen.setKeyFlags(true,keyFlags);
    if (keyEditor.getExpiryDate() != null) {
      GregorianCalendar creationDate=new GregorianCalendar();
      creationDate.setTime(getCreationDate(masterKey));
      GregorianCalendar expiryDate=keyEditor.getExpiryDate();
      long numDays=getNumDatesBetween(creationDate,expiryDate);
      if (numDays <= 0) {
        throw new GeneralException("String_Node_Str");
      }
      hashedPacketsGen.setKeyExpirationTime(true,numDays * 86400);
    }
    keyGen.addSubKey(subKeyPair,hashedPacketsGen.generate(),unhashedPacketsGen.generate());
  }
  PGPSecretKeyRing secretKeyRing=keyGen.generateSecretKeyRing();
  PGPPublicKeyRing publicKeyRing=keyGen.generatePublicKeyRing();
  progress.setProgress("String_Node_Str",90,100);
  saveKeyRing(context,secretKeyRing);
  saveKeyRing(context,publicKeyRing);
  loadKeyRings(context,Id.type.public_key);
  loadKeyRings(context,Id.type.secret_key);
  progress.setProgress("String_Node_Str",100,100);
}

Fixed version (ground truth):
public static void buildSecretKey(Activity context,SectionView userIdsView,SectionView keysView,String oldPassPhrase,String newPassPhrase,ProgressDialogUpdater progress) throws Apg.GeneralException, NoSuchProviderException, PGPException, NoSuchAlgorithmException, SignatureException {
  progress.setProgress("String_Node_Str",0,100);
  Security.addProvider(new BouncyCastleProvider());
  if (oldPassPhrase == null || oldPassPhrase.equals("String_Node_Str")) {
    oldPassPhrase="String_Node_Str";
  }
  if (newPassPhrase == null || newPassPhrase.equals("String_Node_Str")) {
    newPassPhrase="String_Node_Str";
  }
  Vector<String> userIds=new Vector<String>();
  Vector<PGPSecretKey> keys=new Vector<PGPSecretKey>();
  ViewGroup userIdEditors=userIdsView.getEditors();
  ViewGroup keyEditors=keysView.getEditors();
  boolean gotMainUserId=false;
  for (int i=0; i < userIdEditors.getChildCount(); ++i) {
    UserIdEditor editor=(UserIdEditor)userIdEditors.getChildAt(i);
    String userId=null;
    try {
      userId=editor.getValue();
    }
 catch (    UserIdEditor.NoNameException e) {
      throw new Apg.GeneralException("String_Node_Str");
    }
catch (    UserIdEditor.NoEmailException e) {
      throw new Apg.GeneralException("String_Node_Str");
    }
catch (    UserIdEditor.InvalidEmailException e) {
      throw new Apg.GeneralException(e.getMessage());
    }
    if (userId.equals("String_Node_Str")) {
      continue;
    }
    if (editor.isMainUserId()) {
      userIds.insertElementAt(userId,0);
      gotMainUserId=true;
    }
 else {
      userIds.add(userId);
    }
  }
  if (userIds.size() == 0) {
    throw new Apg.GeneralException("String_Node_Str");
  }
  if (!gotMainUserId) {
    throw new Apg.GeneralException("String_Node_Str");
  }
  if (keyEditors.getChildCount() == 0) {
    throw new Apg.GeneralException("String_Node_Str");
  }
  for (int i=0; i < keyEditors.getChildCount(); ++i) {
    KeyEditor editor=(KeyEditor)keyEditors.getChildAt(i);
    keys.add(editor.getValue());
  }
  progress.setProgress("String_Node_Str",10,100);
  KeyEditor keyEditor=(KeyEditor)keyEditors.getChildAt(0);
  int usageId=keyEditor.getUsage();
  boolean canSign=(usageId == Id.choice.usage.sign_only || usageId == Id.choice.usage.sign_and_encrypt);
  boolean canEncrypt=(usageId == Id.choice.usage.encrypt_only || usageId == Id.choice.usage.sign_and_encrypt);
  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());
  PGPPrivateKey masterPrivateKey=masterKey.extractPrivateKey(oldPassPhrase.toCharArray(),new BouncyCastleProvider());
  progress.setProgress("String_Node_Str",20,100);
  for (int i=0; i < userIds.size(); ++i) {
    String userId=userIds.get(i);
    PGPSignatureGenerator sGen=new PGPSignatureGenerator(masterPublicKey.getAlgorithm(),HashAlgorithmTags.SHA1,new BouncyCastleProvider());
    sGen.initSign(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();
  int keyFlags=KeyFlags.CERTIFY_OTHER | KeyFlags.SIGN_DATA;
  if (canEncrypt) {
    keyFlags|=KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE;
  }
  hashedPacketsGen.setKeyFlags(true,keyFlags);
  hashedPacketsGen.setPreferredSymmetricAlgorithms(true,PREFERRED_SYMMETRIC_ALGORITHMS);
  hashedPacketsGen.setPreferredHashAlgorithms(true,PREFERRED_HASH_ALGORITHMS);
  hashedPacketsGen.setPreferredCompressionAlgorithms(true,PREFERRED_COMPRESSION_ALGORITHMS);
  if (keyEditor.getExpiryDate() != null) {
    GregorianCalendar creationDate=new GregorianCalendar();
    creationDate.setTime(getCreationDate(masterKey));
    GregorianCalendar expiryDate=keyEditor.getExpiryDate();
    long numDays=getNumDatesBetween(creationDate,expiryDate);
    if (numDays <= 0) {
      throw new GeneralException("String_Node_Str");
    }
    hashedPacketsGen.setKeyExpirationTime(true,numDays * 86400);
  }
  progress.setProgress("String_Node_Str",30,100);
  PGPKeyRingGenerator keyGen=new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION,masterKeyPair,mainUserId,PGPEncryptedData.CAST5,newPassPhrase.toCharArray(),hashedPacketsGen.generate(),unhashedPacketsGen.generate(),new SecureRandom(),new BouncyCastleProvider().getName());
  progress.setProgress("String_Node_Str",40,100);
  for (int i=1; i < keys.size(); ++i) {
    progress.setProgress(40 + 50 * (i - 1) / (keys.size() - 1),100);
    PGPSecretKey subKey=keys.get(i);
    keyEditor=(KeyEditor)keyEditors.getChildAt(i);
    PGPPublicKey subPublicKey=subKey.getPublicKey();
    PGPPrivateKey subPrivateKey=subKey.extractPrivateKey(oldPassPhrase.toCharArray(),new BouncyCastleProvider());
    PGPKeyPair subKeyPair=new PGPKeyPair(subPublicKey.getAlgorithm(),subPublicKey.getKey(new BouncyCastleProvider()),subPrivateKey.getKey(),subPublicKey.getCreationTime());
    hashedPacketsGen=new PGPSignatureSubpacketGenerator();
    unhashedPacketsGen=new PGPSignatureSubpacketGenerator();
    keyFlags=0;
    usageId=keyEditor.getUsage();
    canSign=(usageId == Id.choice.usage.sign_only || usageId == Id.choice.usage.sign_and_encrypt);
    canEncrypt=(usageId == Id.choice.usage.encrypt_only || usageId == Id.choice.usage.sign_and_encrypt);
    if (canSign) {
      keyFlags|=KeyFlags.SIGN_DATA;
    }
    if (canEncrypt) {
      keyFlags|=KeyFlags.ENCRYPT_COMMS | KeyFlags.ENCRYPT_STORAGE;
    }
    hashedPacketsGen.setKeyFlags(true,keyFlags);
    if (keyEditor.getExpiryDate() != null) {
      GregorianCalendar creationDate=new GregorianCalendar();
      creationDate.setTime(getCreationDate(masterKey));
      GregorianCalendar expiryDate=keyEditor.getExpiryDate();
      long numDays=getNumDatesBetween(creationDate,expiryDate);
      if (numDays <= 0) {
        throw new GeneralException("String_Node_Str");
      }
      hashedPacketsGen.setKeyExpirationTime(true,numDays * 86400);
    }
    keyGen.addSubKey(subKeyPair,hashedPacketsGen.generate(),unhashedPacketsGen.generate());
  }
  PGPSecretKeyRing secretKeyRing=keyGen.generateSecretKeyRing();
  PGPPublicKeyRing publicKeyRing=keyGen.generatePublicKeyRing();
  progress.setProgress("String_Node_Str",90,100);
  saveKeyRing(context,secretKeyRing);
  saveKeyRing(context,publicKeyRing);
  loadKeyRings(context,Id.type.public_key);
  loadKeyRings(context,Id.type.secret_key);
  progress.setProgress("String_Node_Str",100,100);
}