Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package assignmentfour;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * @author Shane Nolan
- * Assignment:
- * This program is carrying out what is called a dictionary attack to crack passwords.
- */
- public class AssignmentFour {
- /**
- * @param args the command line arguments
- */
- final static String ENCRYPTION_TYPE = "MD5";
- public static void main(String[] args) throws IOException {
- List<User> users = new ArrayList<>();
- List<String> words = Files.readAllLines(Paths.get("dictionary.txt"));
- Connection connection = null;
- Statement statement = null;
- try {
- connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sd3assignment4", "root", "");
- statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery("SELECT * FROM useraccounts");
- while (resultSet.next()) {
- users.add(new User(resultSet.getObject(1).toString(),
- resultSet.getObject(2).toString()));
- }
- }
- catch (SQLException sqlex) {
- System.out.println("Comms error " + sqlex);
- }
- finally {
- try {
- statement.close();
- connection.close();
- } catch (SQLException sqlex) {
- System.out.println("Error cleaning up " + sqlex);
- }
- }
- Thread t1 = new Thread(() -> {
- users.forEach((u) -> {
- words.stream().filter((word) ->
- (hashPassword(ENCRYPTION_TYPE, word).equals(u.getPassword()))).forEachOrdered((word) -> {
- System.out.println("Password for " + u.getUser() + " is " + word);
- });
- });
- });
- t1.start();
- }
- private static String hashPassword(String algorithm, String password) {
- StringBuilder sb = new StringBuilder();
- try {
- MessageDigest messaged = MessageDigest.getInstance(algorithm);
- messaged.update(password.getBytes());
- byte[] mdArray = messaged.digest();
- sb = new StringBuilder(mdArray.length * 2);
- for (byte b : mdArray) {
- int v = b & 0xff;
- if (v < 16) {
- sb.append('0');
- }
- sb.append(Integer.toHexString(v));
- }
- }
- catch (NoSuchAlgorithmException nsae) {
- System.out.println(nsae);
- }
- return sb.toString();
- }
- }
- public class User {
- private String user;
- private String password;
- public User(String user, String password){
- this.user = user;
- this.password = password;
- }
- /**
- * @return the user
- */
- public String getUser() {
- return user;
- }
- /**
- * @param user the user to set
- */
- public void setUser(String user) {
- this.user = user;
- }
- /**
- * @return the password
- */
- public String getPassword() {
- return password;
- }
- /**
- * @param password the password to set
- */
- public void setPassword(String password) {
- this.password = password;
- }
- }
Add Comment
Please, Sign In to add comment