Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string>
- #include <time.h>
- #include <iostream>
- #include <omp.h>
- using namespace std;
- string Numbers = "0123456789";
- string AlphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string AlphabetLower = "abcdefghijklmnopqrstuvwxyz";
- string XOR(string data, string key)
- {
- string xorstring = data;
- for (int i = 0; i < xorstring.length(); i++)
- {
- xorstring[i] = data[i] ^ key[i % key.length()];
- }
- return xorstring;
- }
- void BruteForce(int length, string plaintext, string ciphertext, bool EnableAlphabetLower, bool EnableAlphabetUpper, bool EnableNumerals, string tempKey, time_t tstart)
- {
- string decipher;
- if (length == 0)
- {
- decipher = XOR(ciphertext, tempKey);
- //cout << "TempKey: " << tempKey << " Decipher: " << decipher << endl;
- if (decipher == plaintext)
- {
- cout << "Key found! It was: '" << tempKey << "'" << endl;
- time_t tstop = clock();
- float time = ((float)tstop - (float)tstart) / CLOCKS_PER_SEC;
- printf("Czas wykonania: %f s \n", time);
- getchar();
- }
- return;
- }
- if (EnableAlphabetLower)
- {
- for (int i = 0; i < 26; i++)
- {
- string appended = tempKey + AlphabetLower[i];
- BruteForce(length - 1, plaintext, ciphertext, EnableAlphabetLower, EnableAlphabetUpper, EnableNumerals, appended.c_str(), tstart);
- }
- }
- if (EnableAlphabetUpper)
- {
- for (int i = 0; i < 26; i++)
- {
- string appended = tempKey + AlphabetUpper[i];
- BruteForce(length - 1, plaintext, ciphertext, EnableAlphabetLower, EnableAlphabetUpper, EnableNumerals, appended.c_str(), tstart);
- }
- }
- if (EnableNumerals)
- {
- for (int i = 0; i < 10; i++)
- {
- string appended = tempKey + Numbers[i];
- BruteForce(length - 1, plaintext, ciphertext, EnableAlphabetLower, EnableAlphabetUpper, EnableNumerals, appended.c_str(), tstart);
- }
- //getchar();
- }
- }
- int main()
- {
- string cipher, plain;
- plain = "testowanie";
- string key = "";
- cipher = XOR(plain, "12");
- string test = XOR(cipher, "12");
- cout << plain << endl;
- cout << cipher << endl;
- cout << test << endl;
- cout << "Po wcisnieciu Enter, czas zacznie byc mierzony" << endl;
- getchar();
- time_t tstart = clock();
- while (true)
- {
- static int stringlength = 1;
- BruteForce(stringlength, plain, cipher, 1, 0, 1, key, tstart);
- stringlength++;
- cout << endl << endl << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement