main.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <chrono> // fuer die Zeitmessung
  5. #include <stdint.h>
  6. using namespace std;
  7. const char first_char = '!';
  8. const char last_char = '~';
  9. char get_new_char(char prev_char) {
  10. if (prev_char == 0) {
  11. return first_char;
  12. } else if (prev_char < last_char) {
  13. return prev_char + 1;
  14. }
  15. return first_char;
  16. }
  17. int bruteforce(string passwd) {
  18. char list[200] = {'!',0};
  19. uint64_t counter = 0;
  20. int pos = 0;
  21. while (passwd.compare(list)) {
  22. if (list[pos] == last_char) {
  23. while (list[pos] == last_char) {
  24. list[pos] = get_new_char(list[pos]);
  25. pos += 1;
  26. list[pos] = get_new_char(list[pos]);
  27. }
  28. pos = 0;
  29. } else {
  30. list[pos] = get_new_char(list[pos]);
  31. }
  32. counter += 1;
  33. }
  34. cout << "Das Passwort lautet: " << list << endl;
  35. cout << "Es wurden " << counter << " Versuche benoetigt" << endl;
  36. return 1;
  37. }
  38. // Funktion zum überprüfen des Passwortes mit dem Dictionary
  39. int compare_passwd(string passwd, string dict_file) {
  40. // fstream zum Oeffnen der Datei
  41. fstream dict(dict_file, ios::in);
  42. // char zum einlesen einer Zeile max 200 Zeichen
  43. char list[200] = {0};
  44. // Statusvariable zum Erkennen, ob das Passwort gefunden wurde
  45. // Standard nein
  46. int a = 0;
  47. if(!dict.is_open()) {
  48. cout << "Dictionary konnte nicht geoeffnet werden!" << endl;
  49. return 0;
  50. }
  51. // Schleife, um alle Passworter zu ueberpruefen, oder bei gefundenem Passwort abbrechen
  52. while (dict.getline(list, 200)) {
  53. // Passwort vergleichen
  54. if (passwd.compare(list) == 0) {
  55. a = 1;
  56. // Wenn das Passwort gefunden wurde muss nicht weiter ueberprueft werden
  57. dict.close();
  58. break;
  59. }
  60. }
  61. // Datei wieder schliessen
  62. dict.close();
  63. // Rueckgabe, ob das Kennwort gefunden (1) wurde oder nicht (0)
  64. return a;
  65. }
  66. int main() {
  67. string passwd;
  68. // Name der Datei
  69. string dict = "passdict.txt";
  70. cout << " ***** Passwortueberpruefung ***** " << endl;
  71. cout << endl;
  72. cout << "Bitte ein Passwort eingeben, das ueberprueft werden soll." << endl;
  73. cout << "Passwort: ";
  74. cin >> passwd;
  75. // Ueberpruefung, ob das Password bekannt ist.
  76. // if (compare_passwd(passwd, dict)) {
  77. // cout << "Passwort gefunden" << endl;
  78. // } else {
  79. cout << "Passwort nicht gefunden" << endl;
  80. cout << "Beginne Bruteforce" << endl;
  81. auto start = chrono::high_resolution_clock::now();
  82. if (bruteforce(passwd)) {
  83. cout << "Passwort gefunden" << endl;
  84. }
  85. auto end = chrono::high_resolution_clock::now();
  86. chrono::duration<double, std::milli> duration = end - start;
  87. cout << "Das finden hat " << duration.count() << " Millisekunden gedauert." << endl;
  88. // }
  89. return 0;
  90. }