Advertisement
Ciemny_Cygan

vigenere_homework

Nov 12th, 2020 (edited)
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. /*
  2.   Program for Encryption/Decryption/Cracking Vigenere Cipher
  3.   © Copyright 2020 Iwo Strzeboński
  4.   Published under WTFPLv2
  5.   Language: C++
  6.  */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <string>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. string * generate_tabula_recta(string alph) {
  16.     int n = alph.length();
  17.     string * arr = new string[n];
  18.  
  19.     for (int i = 0; i < n; i++) {
  20.         arr[i] = alph.substr(i, n) + alph.substr(0, i);
  21.     }
  22.  
  23.     return arr;
  24. }
  25.  
  26. string encode_vigenere(string mess, string alph, string arr[], string key) {
  27.     string crypt = "";
  28.     int count = 0, i = 0, n = 0;
  29.  
  30.     for (int a = 0; a < mess.length(); a++) {
  31.         if (n > key.length() - 1) {
  32.             n = 0;
  33.             count++;
  34.         }
  35.         i = alph.find(key[n]);
  36.  
  37.         bool boolean = false;
  38.  
  39.         for (int b = 0; b < alph.length(); b++) {
  40.             if (arr[b][0] == mess[a]) {
  41.                 crypt += arr[b][i];
  42.                 boolean = true;
  43.                 break;
  44.             }
  45.         }
  46.  
  47.         if (boolean) n++; else crypt += mess[a];
  48.     }
  49.  
  50.     return crypt + "\nkey repetitions: " + to_string(count);
  51. }
  52.  
  53. string decode_vigenere(string crypt, string alph, string arr[], string key) {
  54.     string mess = "";
  55.     int i = 0, n = 0;
  56.  
  57.     for (int a = 0; a < crypt.length(); a++) {
  58.         n = n > key.length() - 1 ? 0 : n;
  59.         i = alph.find(key[n]);
  60.  
  61.         bool boolean = false;
  62.  
  63.         for (int b = 0; b < alph.length(); b++) {
  64.             if (arr[b][i] == crypt[a]) {
  65.                 mess += arr[b][0];
  66.                 boolean = true;
  67.                 break;
  68.             }
  69.         }
  70.  
  71.         if (boolean) n++; else mess += crypt[a];
  72.     }
  73.  
  74.     return mess;
  75. }
  76.  
  77. string count_letters(string alph, string crypt) {
  78.     string out = "";
  79.  
  80.     for (int i = 0; i < alph.length(); i++) {
  81.         out += string(1, alph[i]) + ": " + to_string(count(crypt.begin(), crypt.end(), alph[i])) + "\n";
  82.     }
  83.    
  84.     return out;
  85. }
  86.  
  87. float crack_vigenere(string alph, string crypt) {
  88.     int n = alph.length();
  89.     int arr[n];
  90.  
  91.     int coincidence_index = 0, all_letters = 0;
  92.     float out = 0;
  93.  
  94.     for (int i = 0; i < n; i++) {
  95.         arr[i] = int(count(crypt.begin(), crypt.end(), alph[i]));
  96.         all_letters += arr[i];
  97.         coincidence_index += arr[i] * (arr[i] - 1);
  98.     }
  99.  
  100.     out = all_letters - 1;
  101.     out *= all_letters;
  102.     out = coincidence_index / out;
  103.     out -= 0.0385;
  104.     out =  0.0285 / out;
  105.  
  106.     out = (int)(out * 100 + 0.5);
  107.     return (float)out / 100;
  108. }
  109.  
  110. int main() {
  111.     string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  112.  
  113.     string * arr;
  114.     string array[alphabet.length()];
  115.     arr = generate_tabula_recta(alphabet);
  116.  
  117.     for (int i = 0; i < alphabet.length(); i++) {
  118.         array[i] = * (arr + i);
  119.     }
  120.  
  121.     delete[] arr;
  122.  
  123.     string mode = "", key = "", message = "", answer = "";
  124.  
  125.     ofstream output_file ("Vigenere_wyniki.txt", ofstream::out | ofstream::trunc);
  126.  
  127.     answer = "================|| Zadanie 77.1 ||================\n";
  128.     key = "LUBIMYCZYTAC";
  129.     ifstream file1 ("dokad.txt");
  130.     getline(file1, message);
  131.     file1.close();
  132.     answer += encode_vigenere(message, alphabet, array, key);
  133.     output_file << answer << '\n';
  134.    
  135.     answer = "================|| Zadanie 77.2 ||================\n";
  136.     ifstream file2 ("szyfr.txt");
  137.     getline(file2, message);
  138.     getline(file2, key);
  139.     file2.close();
  140.     answer += decode_vigenere(message, alphabet, array, key);
  141.     output_file << answer << '\n';
  142.    
  143.     answer = "================|| Zadanie 77.3 ||================\n";
  144.     answer += string(count_letters(alphabet, message));
  145.     answer += "key lenght: " + to_string(key.length()) + "\ncalculated key length: " + to_string(crack_vigenere(alphabet, message));
  146.     output_file << answer;
  147.    
  148.     output_file.close();
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement