Password guessing program problems

时光怂恿深爱的人放手 提交于 2019-12-11 15:05:43

问题


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.

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.

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.

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."

Any helpful advice is greatly appreciated!

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<String> 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 < 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");
    }
  }
}

来源:https://stackoverflow.com/questions/22824821/password-guessing-program-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!