0
\$\begingroup\$

I want to crack the password hash. This is my data which I want to display the username and the passwords.

enter image description here

This is my code

import java.io.FileReader;
import java.io.Console; 
import java.util.Scanner; 
import java.security.MessageDigest; 
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.SecretKey;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter; 
import com.opencsv.CSVReader; 

public class LabSecurity {
    private static final char[] characters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
            'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    private static final int HASH_ITERS = 10000;
    private static final int SALT_LENGTH = 32;
    private static final int HASH_LENGTH = 512;

    private boolean found;
    private String match;

    private MessageDigest md5; 
    private byte[] byteRepresentation;
    private byte[] hash;
    private String strHexHash;
    private HexBinaryAdapter hba; 

    /**
     * Constructor for objects of class LabSecurity
     */
    public LabSecurity() {
        try {
            md5 = MessageDigest.getInstance("MD5");
            hba = new HexBinaryAdapter();
        } catch (NoSuchAlgorithmException nsae) {
        }
    }

    public void tryMD5() {
        try {
            String str;
            do {
                System.out.println("Input a string to generate its MD5 hash (press enter to quit)");
                Scanner terminalInput = new Scanner(System.in);
                str = terminalInput.nextLine();
                if (!str.equals("")) {
                    System.out.println(encodeMD5(str));
                }
            } while (!str.equals(""));
        } catch (Exception e) {
        }

    }

    private String encodeMD5(final String input) throws Exception {
        byteRepresentation = input.getBytes("UTF-8");
        hash = md5.digest(byteRepresentation);
        strHexHash = hba.marshal(hash);
        return strHexHash;
    }

    public void bruteForce(final String input) {
        try {
            CSVReader reader = new CSVReader(new FileReader(input));
            String[] nextLine;
            reader.readNext(); 
            System.out.println("Brute force");
            while ((nextLine = reader.readNext()) != null) {
                System.out.print(nextLine[0] + " " + nextLine[1] + " ");
                found = false;
                String password = "not found";
                if (found) {
                    password = match;
                }
                System.out.println(password);
            }
        } catch (Exception e) {
        }
    }

    /**
     * Recursively explore all passwords shorter or equal to parameter <length>,
     * made up of characters from the <characters> class variable
     * to find the password which has the same MD5 hash as parameter <hash>.
     * The <found> global variable needs to be initialised to false prior to calling the method.
     * Results are stored in the <found> and <match> global variables.
     * <p>
     * bruteForceRecursive(10, 0, "", <hash>) will try passwords
     * of 10 or fewer characters until it finds one that hashes to <hash> or
     * all passwords have been tried unsuccessfully.
     */
    private void bruteForceRecursive(final int length, final int position, final String baseString, final String hash) throws Exception {
        for (int i = 0; i < characters.length & !found; i++) {
            String attempt = baseString + characters[i];
            found = hash.equals(encodeMD5(attempt));
            if (found) {
                match = attempt;
            } else {

                if (position < length - 1) {
                    bruteForceRecursive(length, position + 1, attempt, hash);
                }
            }
        }
    }

    public String generateHash(String password) {
        String result = "";
        return result;
    }

    private byte[] hashPassword(final char[] password, final byte[] salt, final int iterations, final int keyLength) {

        try {
            SecretKeyFactory skf = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA512");
            PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, keyLength);
            SecretKey key = skf.generateSecret(spec);
            byte[] res = key.getEncoded();
            return res;
        } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
            throw new RuntimeException(e);
        }
    }


}
\$\endgroup\$
  • \$\begingroup\$ I want to crack the password hash find a feasible way to create a collision (break a cryptographic hash function/MD5) or for a given list of (IDs and) hash values, find at least one input giving one of the outputs? \$\endgroup\$ – greybeard Mar 17 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.