Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <omp.h>
  6.  
  7. using namespace std;
  8.  
  9. string Numbers = "0123456789";
  10. string AlphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  11. string AlphabetLower = "abcdefghijklmnopqrstuvwxyz";
  12.  
  13. string XOR(string data, string key)
  14. {
  15.     string xorstring = data;
  16.     for (int i = 0; i < xorstring.length(); i++)
  17.     {
  18.         xorstring[i] = data[i] ^ key[i % key.length()];
  19.     }
  20.     return xorstring;
  21. }
  22.  
  23. void BruteForce(int length, string plaintext, string ciphertext, bool EnableAlphabetLower, bool EnableAlphabetUpper, bool EnableNumerals, string tempKey, time_t tstart)
  24. {
  25.     string decipher;
  26.     if (length == 0)
  27.     {
  28.         decipher = XOR(ciphertext, tempKey);
  29.  
  30.         //cout << "TempKey: " << tempKey << " Decipher: " << decipher << endl;
  31.  
  32.         if (decipher == plaintext)
  33.         {
  34.             cout << "Key found! It was: '" << tempKey << "'" << endl;
  35.             time_t tstop = clock();
  36.             float time = ((float)tstop - (float)tstart) / CLOCKS_PER_SEC;
  37.             printf("Czas wykonania: %f s \n", time);
  38.             getchar();
  39.         }
  40.         return;
  41.     }
  42.  
  43.  
  44.     if (EnableAlphabetLower)
  45.     {
  46.         for (int i = 0; i < 26; i++)
  47.         {
  48.             string appended = tempKey + AlphabetLower[i];
  49.             BruteForce(length - 1, plaintext, ciphertext, EnableAlphabetLower, EnableAlphabetUpper, EnableNumerals, appended.c_str(), tstart);
  50.         }
  51.     }
  52.  
  53.     if (EnableAlphabetUpper)
  54.     {
  55.         for (int i = 0; i < 26; i++)
  56.         {
  57.             string appended = tempKey + AlphabetUpper[i];
  58.             BruteForce(length - 1, plaintext, ciphertext, EnableAlphabetLower, EnableAlphabetUpper, EnableNumerals, appended.c_str(), tstart);
  59.         }
  60.     }
  61.  
  62.     if (EnableNumerals)
  63.     {
  64.         for (int i = 0; i < 10; i++)
  65.         {
  66.             string appended = tempKey + Numbers[i];
  67.             BruteForce(length - 1, plaintext, ciphertext, EnableAlphabetLower, EnableAlphabetUpper, EnableNumerals, appended.c_str(), tstart);
  68.         }
  69.         //getchar();
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.  
  76.     string cipher, plain;
  77.     plain = "testowanie";
  78.     string key = "";
  79.     cipher = XOR(plain, "12");
  80.     string test = XOR(cipher, "12");
  81.  
  82.     cout << plain << endl;
  83.     cout << cipher << endl;
  84.     cout << test << endl;
  85.  
  86.     cout << "Po wcisnieciu Enter, czas zacznie byc mierzony" << endl;
  87.     getchar();
  88.  
  89.     time_t tstart = clock();
  90.  
  91.     while (true)
  92.     {
  93.         static int stringlength = 1;
  94.         BruteForce(stringlength, plain, cipher, 1, 0, 1, key, tstart);
  95.         stringlength++;
  96.         cout << endl << endl << endl;
  97.     }
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement