undergrad-projects / MidtermProject / DictionaryAttack.java
DictionaryAttack.java
Raw
import java.util.Scanner;
import java.io.*;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;

class DictionaryAttack
{
    //change address as needed, contains all words in English from sjsu.edu
    File dict = new File("C:\\Users\\Matt Schreyer\\Downloads\\english.txt");
    boolean result = false;

    public DictionaryAttack(String s){
        try{
            //hashing password to crack
            String digest = DigestUtils.sha1Hex(s);
            byte[] byteArray = digest.getBytes();
            String hex = Hex.encodeHexString(byteArray);
            
            result = checkDict(hex);
        }
        catch(Exception e){
            System.out.println("File Error, ensure file is in correct folder.");
            System.out.println(e.toString());
        }
        finally{
            System.out.print("Process Complete. ");
        }
    }

    private boolean checkDict(String s)throws Exception{
        Scanner in = new Scanner(dict);
        while (in.hasNextLine())
        {
            String temp = in.nextLine();
            String rev = new StringBuilder(temp).reverse().toString();//substitute(s);
            
            //hashing possible dictionary entries + alterations
            String digestT = DigestUtils.sha1Hex(temp);
            byte[] byteArray = digestT.getBytes();
            String hexT = Hex.encodeHexString(byteArray);
            
            String digestR = DigestUtils.sha1Hex(rev);
            byte[] byteArray2 = digestR.getBytes();
            String hexR = Hex.encodeHexString(byteArray2);
            
            if (s.equals(hexT)){
                System.out.println("The entered password is the word "+temp+".");
                return true;
            }
            else if (s.equals(hexR)){
                System.out.println("The entered password is the word "+temp+" reversed.");
                return true;
            }
        }
        System.out.println("Match not found.");
        return false;
    }
}