Advertisement
Alan468

vigenere

Jun 12th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <conio.h>
  4. #include <time.h>
  5. #include <string>
  6. #include <fstream>
  7. #include <WinSock.h>
  8. #include <locale.h>
  9.  
  10. #pragma comment (lib, "ws2_32.lib")
  11.  
  12. #define MIN_LEN 2 //zaczynomy generacja od tylu znakowych haseł
  13. #define MAX_LEN 5 //tyla - 1 jest maksymalnie znaków w haśle
  14.  
  15. using namespace std;
  16.  
  17. string znaki = "abcdefghijklmnopqrstuvwxyz";
  18. string zaszyfrowany_tekst;
  19. string szukana = "lorem"; //szukany fragment odszyfrowanego tekstu
  20. clock_t tStart;
  21.  
  22. //ta funkcja akurat nie jest używano w tym programie ale zrobiłch ja bo MOGA i UMIA!
  23. string vigenere_en(string m, string c) {
  24.     transform(m.begin(), m.end(), m.begin(), ::tolower);
  25.     string e = "";
  26.     string k = "";
  27.     int a, b, n = m.length();
  28.     int temp = 0;
  29.     int index = 0;
  30.  
  31.     for (int i = 0; i < n; ++i) {
  32.         if (m[i] > 96 && m[i] < 123) {
  33.             a = znaki.find_first_of(m[i]);
  34.             b = znaki.find_first_of(c[temp++]);
  35.             if (temp > c.length() - 1) temp = 0;
  36.             index = ((a + b) % 26);
  37.             e += znaki[index];
  38.         } else e += m[i];
  39.     }
  40.  
  41.     return e;
  42. }
  43.  
  44. //funkcja deszyfrująco vigenera
  45. string vigenere_de(string c, string k) {
  46.     string d = "";
  47.     int a, b, n = c.length();
  48.     int temp = 0;
  49.     int index = 0;
  50.  
  51.     for (int i = 0; i < n; ++i) {
  52.         if (c[i] > 96 && c[i] < 123) {
  53.             a = znaki.find_first_of(c[i]);
  54.             b = znaki.find_first_of(k[temp++]);
  55.             if (temp > k.length() - 1) temp = 0;
  56.             index = ((a - b) % 26);
  57.             if (index < 0) index = 26 + index;
  58.             d += znaki[index];
  59.         } else d += c[i];
  60.     }
  61.  
  62.     return d;
  63. }
  64.  
  65. //funkcja generująco wszystkie możliwe kombinacje haseł tak jak brute force
  66. void generate(unsigned int length, string s) {
  67.     if (length == 0) {
  68.         string odszyfrowany_tekst = vigenere_de(zaszyfrowany_tekst, s);
  69.         int pos = odszyfrowany_tekst.find(szukana);
  70.         if (pos > -1) {
  71.             system("cls");
  72.             cout << "Czas szukania hasła wyniósł: " << ((double)(clock() - tStart) / CLOCKS_PER_SEC) << endl;
  73.             cout << "Hasło to: " << s << endl;
  74.             cout << "Odszyfrowany tekst to: " << endl << endl << odszyfrowany_tekst << endl;
  75.             _getch();
  76.             exit(0);
  77.         }
  78.         return;
  79.     }
  80.     for (unsigned int i = 0; i < znaki.length(); ++i) {
  81.         string a = s + znaki[i];
  82.         generate(length - 1, a);
  83.     }
  84. }
  85.  
  86. int main() {
  87.     setlocale(LC_CTYPE, ".1250");
  88.     SetConsoleTitle(TEXT("Crackowanie Vigenera brute forcem!"));
  89.  
  90.     fstream plik;
  91.     plik.open("szyfr.txt");
  92.  
  93.     if (plik.is_open()) {
  94.         getline(plik, zaszyfrowany_tekst);
  95.         unsigned int stringlength = MIN_LEN;
  96.  
  97.         cout << "Kliknij aby zacząć szukać hasło metodą bruteforce!" << endl;
  98.         _getch();
  99.         tStart = clock();
  100.         cout << "Crackuje hasło... To może potrwać..." << endl;
  101.  
  102.         while (stringlength < MAX_LEN) {
  103.             generate(stringlength, "");
  104.             stringlength++;
  105.         }
  106.         plik.close();
  107.     }
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement