Guest User

Untitled

a guest
Dec 13th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package assignmentfour;
  7.  
  8. import java.io.IOException;
  9. import java.nio.file.Files;
  10. import java.nio.file.Paths;
  11. import java.security.MessageDigest;
  12. import java.security.NoSuchAlgorithmException;
  13. import java.sql.Connection;
  14. import java.sql.DriverManager;
  15. import java.sql.ResultSet;
  16. import java.sql.ResultSetMetaData;
  17. import java.sql.SQLException;
  18. import java.sql.Statement;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. /**
  23. * @author Shane Nolan
  24. * Assignment:
  25. * This program is carrying out what is called a dictionary attack to crack passwords.
  26. */
  27. public class AssignmentFour {
  28.  
  29. /**
  30. * @param args the command line arguments
  31. */
  32. final static String ENCRYPTION_TYPE = "MD5";
  33. public static void main(String[] args) throws IOException {
  34. List<User> users = new ArrayList<>();
  35. List<String> words = Files.readAllLines(Paths.get("dictionary.txt"));
  36. Connection connection = null;
  37. Statement statement = null;
  38.  
  39. try {
  40. connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/sd3assignment4", "root", "");
  41. statement = connection.createStatement();
  42. ResultSet resultSet = statement.executeQuery("SELECT * FROM useraccounts");
  43.  
  44. while (resultSet.next()) {
  45. users.add(new User(resultSet.getObject(1).toString(),
  46. resultSet.getObject(2).toString()));
  47. }
  48. }
  49. catch (SQLException sqlex) {
  50. System.out.println("Comms error " + sqlex);
  51. }
  52. finally {
  53. try {
  54. statement.close();
  55. connection.close();
  56. } catch (SQLException sqlex) {
  57. System.out.println("Error cleaning up " + sqlex);
  58. }
  59. }
  60.  
  61. Thread t1 = new Thread(() -> {
  62. users.forEach((u) -> {
  63. words.stream().filter((word) ->
  64. (hashPassword(ENCRYPTION_TYPE, word).equals(u.getPassword()))).forEachOrdered((word) -> {
  65. System.out.println("Password for " + u.getUser() + " is " + word);
  66. });
  67. });
  68. });
  69. t1.start();
  70. }
  71.  
  72. private static String hashPassword(String algorithm, String password) {
  73. StringBuilder sb = new StringBuilder();
  74. try {
  75. MessageDigest messaged = MessageDigest.getInstance(algorithm);
  76. messaged.update(password.getBytes());
  77. byte[] mdArray = messaged.digest();
  78. sb = new StringBuilder(mdArray.length * 2);
  79. for (byte b : mdArray) {
  80. int v = b & 0xff;
  81. if (v < 16) {
  82. sb.append('0');
  83. }
  84. sb.append(Integer.toHexString(v));
  85. }
  86. }
  87. catch (NoSuchAlgorithmException nsae) {
  88. System.out.println(nsae);
  89. }
  90. return sb.toString();
  91. }
  92.  
  93. }
  94.  
  95. public class User {
  96.  
  97. private String user;
  98. private String password;
  99.  
  100. public User(String user, String password){
  101. this.user = user;
  102. this.password = password;
  103. }
  104.  
  105. /**
  106. * @return the user
  107. */
  108. public String getUser() {
  109. return user;
  110. }
  111.  
  112. /**
  113. * @param user the user to set
  114. */
  115. public void setUser(String user) {
  116. this.user = user;
  117. }
  118.  
  119. /**
  120. * @return the password
  121. */
  122. public String getPassword() {
  123. return password;
  124. }
  125.  
  126. /**
  127. * @param password the password to set
  128. */
  129. public void setPassword(String password) {
  130. this.password = password;
  131. }
  132. }
Add Comment
Please, Sign In to add comment