Guest User

Untitled

a guest
Oct 13th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. class Program
  2. {
  3. #region Private variables
  4.  
  5. // the secret password which we will try to find via brute force
  6. private static string password = "p123";
  7. private static string result;
  8.  
  9. private static bool isMatched = false;
  10.  
  11. /* The length of the charactersToTest Array is stored in a
  12. * additional variable to increase performance */
  13. private static int charactersToTestLength = 0;
  14. private static long computedKeys = 0;
  15.  
  16. /* An array containing the characters which will be used to create the brute force keys,
  17. * if less characters are used (e.g. only lower case chars) the faster the password is matched */
  18. private static char[] charactersToTest =
  19. {
  20. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  21. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  22. 'u', 'v', 'w', 'x', 'y', 'z','A','B','C','D','E',
  23. 'F','G','H','I','J','K','L','M','N','O','P','Q','R',
  24. 'S','T','U','V','W','X','Y','Z','1','2','3','4','5',
  25. '6','7','8','9','0','!','$','#','@','-'
  26. };
  27.  
  28. #endregion
  29.  
  30. static void Main(string[] args)
  31. {
  32. var timeStarted = DateTime.Now;
  33. Console.WriteLine("Start BruteForce - {0}", timeStarted.ToString());
  34.  
  35. // The length of the array is stored permanently during runtime
  36. charactersToTestLength = charactersToTest.Length;
  37.  
  38. // The length of the password is unknown, so we have to run trough the full search space
  39. var estimatedPasswordLength = 0;
  40.  
  41. while (!isMatched)
  42. {
  43. /* The estimated length of the password will be increased and every possible key for this
  44. * key length will be created and compared against the password */
  45. estimatedPasswordLength++;
  46. startBruteForce(estimatedPasswordLength);
  47. }
  48.  
  49. Console.WriteLine("Password matched. - {0}", DateTime.Now.ToString());
  50. Console.WriteLine("Time passed: {0}s", DateTime.Now.Subtract(timeStarted).TotalSeconds);
  51. Console.WriteLine("Resolved password: {0}", result);
  52. Console.WriteLine("Computed keys: {0}", computedKeys);
  53.  
  54. Console.ReadLine();
  55. }
  56.  
  57. #region Private methods
  58.  
  59. /// <summary>
  60. /// Starts the recursive method which will create the keys via brute force
  61. /// </summary>
  62. /// <param name="keyLength">The length of the key</param>
  63. private static void startBruteForce(int keyLength)
  64. {
  65. var keyChars = createCharArray(keyLength, charactersToTest[0]);
  66. // The index of the last character will be stored for slight perfomance improvement
  67. var indexOfLastChar = keyLength - 1;
  68. createNewKey(0, keyChars, keyLength, indexOfLastChar);
  69. }
  70.  
  71. /// <summary>
  72. /// Creates a new char array of a specific length filled with the defaultChar
  73. /// </summary>
  74. /// <param name="length">The length of the array</param>
  75. /// <param name="defaultChar">The char with whom the array will be filled</param>
  76. /// <returns></returns>
  77. private static char[] createCharArray(int length, char defaultChar)
  78. {
  79. return (from c in new char[length] select defaultChar).ToArray();
  80. }
  81.  
  82. /// <summary>
  83. /// This is the main workhorse, it creates new keys and compares them to the password until the password
  84. /// is matched or all keys of the current key length have been checked
  85. /// </summary>
  86. /// <param name="currentCharPosition">The position of the char which is replaced by new characters currently</param>
  87. /// <param name="keyChars">The current key represented as char array</param>
  88. /// <param name="keyLength">The length of the key</param>
  89. /// <param name="indexOfLastChar">The index of the last character of the key</param>
  90. private static void createNewKey(int currentCharPosition, char[] keyChars, int keyLength, int indexOfLastChar)
  91. {
  92. var nextCharPosition = currentCharPosition + 1;
  93. // We are looping trough the full length of our charactersToTest array
  94. for (int i = 0; i < charactersToTestLength; i++)
  95. {
  96. /* The character at the currentCharPosition will be replaced by a
  97. * new character from the charactersToTest array => a new key combination will be created */
  98. keyChars[currentCharPosition] = charactersToTest[i];
  99.  
  100. // The method calls itself recursively until all positions of the key char array have been replaced
  101. if (currentCharPosition < indexOfLastChar)
  102. {
  103. createNewKey(nextCharPosition, keyChars, keyLength, indexOfLastChar);
  104. }
  105. else
  106. {
  107. // A new key has been created, remove this counter to improve performance
  108. computedKeys++;
  109.  
  110. /* The char array will be converted to a string and compared to the password. If the password
  111. * is matched the loop breaks and the password is stored as result. */
  112. if ((new String(keyChars)) == password)
  113. {
  114. if (!isMatched)
  115. {
  116. isMatched = true;
  117. result = new String(keyChars);
  118. }
  119. return;
  120. }
  121. }
  122. }
  123. }
  124.  
  125. #endregion
  126. }
Add Comment
Please, Sign In to add comment