title: Password guessing program problems
body: <p>I'm having a few issues with an extra credit assignment for my Java class. The objective is to decrypt a file without the password. It is encrypted with the PBEWithSHA1AndDESede algorithm and the password is a dictionary word with no numbers or special characters.</p>
  
  <p>The way I'm trying to solve this is by guessing the password over and over again until I get it right using the code below.</p>
  
  <p>The problem I'm running into is that the extra_out.txt file is being output after the first cycle of the for loop, when I want it to only be output if the correct word is guessed.</p>
  
  <p>So when it runs, I get the exception "Encryption Error" and then the extra_out.txt file is output (still encrypted) and then 9999 more "Encryption Errors."</p>
  
  <p>Any helpful advice is greatly appreciated!  </p>
  
  <pre><code>import java.io.File; 
  import java.io.FileNotFoundException; 
  import java.io.IOException;
  import java.util.ArrayList; 
  import java.util.Arrays;
  import java.util.Random; 
  import java.util.Scanner; 
  
  public class WordGuess { 
  
    public static void main(String[] args) {  
      ArrayList&lt;String&gt; words = new ArrayList();  
      Random numGen = new Random(); 
      String curWord = ""; 
  
      try { 
        File aFile = new File("english.txt"); 
        Scanner reader = new Scanner(aFile); 
  
        while (reader.hasNext()) { 
          curWord = reader.next(); 
  
          if (curWord.length() == 5) { 
            words.add(curWord); 
          }
        }    
      } 
  
      catch (FileNotFoundException e) { 
        System.out.println("Error: " + e); 
      }
  
      for(int i = 0; i &lt; 10000; i++){ 
        int rand = Math.abs(numGen.nextInt(words.size()));    
        File fileIn = new File("extracredit.enc");
        File fileOut = new File("extra_out.txt");
        String password = words.get(rand);
        crackFile(fileIn, fileOut, password); 
      }
    }
  
    public static void crackFile(File input, File output, String password) {
      try{
        Crypt c = new Crypt(password);
        byte[] bytes = FileIO.read(input);
        FileIO.write(output, c.decrypt(bytes));
      } 
      catch (IOException e) {
        System.out.println("Could not read/write file");  
      }
      catch (Exception e) {
        System.out.println("Encryption error");
      }
    }
  }
  </code></pre>