Guest User

Cracker

a guest
Aug 20th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.10 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4.  
  5. int incrementPassword(char*, int);
  6. void initPassword(char*);
  7. int indexOf(char, int);
  8.  
  9. const char* PASSWORD_CHARS = "abcdefghijklmnopqrstuvwxyz";
  10. const int PASSWORD_LENGTH = 8;
  11. const int DISPLAY = 0;
  12.  
  13. int main(){
  14.     char password[PASSWORD_LENGTH];
  15.     int passwordCharPoolSize = strlen(PASSWORD_CHARS);
  16.     clock_t start, stop;
  17.  
  18.     printf("Starting evaluation of %d-character length password, with %d characters\n",
  19.         PASSWORD_LENGTH, passwordCharPoolSize);
  20.     start = clock();
  21.  
  22.     while(incrementPassword(password, passwordCharPoolSize));
  23.  
  24.     stop = clock();
  25.     double executionTime = (double)(stop - start) / CLOCKS_PER_SEC;
  26.  
  27.     printf("Execution time: %lf seconds\n", executionTime);
  28.  
  29.     return 0;
  30. }
  31.  
  32. // The password keeps track of itself. In other words, the next
  33. // password can be derived from the previous one
  34. int incrementPassword(char* password, int PASSWORD_CHARS_LEN){
  35.     int i = 0;
  36.     while(i < PASSWORD_LENGTH){
  37.         if(i == PASSWORD_LENGTH && password[0] == PASSWORD_CHARS[PASSWORD_CHARS_LEN - 1]){
  38.             // The loop is over; the last character has been incremented
  39.             break;
  40.         }
  41.         char currentChar = password[PASSWORD_LENGTH - 1 - i];
  42.         if(currentChar == PASSWORD_CHARS[PASSWORD_CHARS_LEN - 1]){
  43.             password[PASSWORD_LENGTH - 1 - i] = PASSWORD_CHARS[0];
  44.             i++;
  45.         }
  46.         else{
  47.             char nextChar= PASSWORD_CHARS[indexOf(currentChar, PASSWORD_CHARS_LEN) + 1];
  48.             password[PASSWORD_LENGTH - 1 - i] = nextChar;
  49.             if(DISPLAY){
  50.                 printf("%s\n", password);
  51.             }
  52.             return 1; // Keep the main loop running
  53.         }
  54.     }
  55.     // Stops the main loop by returning false
  56.     return 0;
  57. }
  58.  
  59. // Initializes a password to the first character of the character pool
  60. void initPassword(char* password){
  61.     int i = 0;
  62.     while(i < PASSWORD_LENGTH){
  63.         password[i] = PASSWORD_CHARS[0];
  64.         i++;
  65.     }
  66. }
  67.  
  68. // Since the pool of characters isn't in order, this finds the
  69. // index of a given character
  70. int indexOf(char c, int PASSWORD_CHARS_LEN){
  71.     int i = 0;
  72.     while(i < PASSWORD_CHARS_LEN){
  73.         if(PASSWORD_CHARS[i] == c){
  74.             return i;
  75.         }
  76.         i++;
  77.     }
  78.     // Didn't find it
  79.     return -1;
  80. }
Add Comment
Please, Sign In to add comment