Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Program for Encryption/Decryption/Cracking Vigenere Cipher
- © Copyright 2020 Iwo Strzeboński
- Published under WTFPLv2
- Language: C++
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <algorithm>
- using namespace std;
- string * generate_tabula_recta(string alph) {
- int n = alph.length();
- string * arr = new string[n];
- for (int i = 0; i < n; i++) {
- arr[i] = alph.substr(i, n) + alph.substr(0, i);
- }
- return arr;
- }
- string encode_vigenere(string mess, string alph, string arr[], string key) {
- string crypt = "";
- int count = 0, i = 0, n = 0;
- for (int a = 0; a < mess.length(); a++) {
- if (n > key.length() - 1) {
- n = 0;
- count++;
- }
- i = alph.find(key[n]);
- bool boolean = false;
- for (int b = 0; b < alph.length(); b++) {
- if (arr[b][0] == mess[a]) {
- crypt += arr[b][i];
- boolean = true;
- break;
- }
- }
- if (boolean) n++; else crypt += mess[a];
- }
- return crypt + "\nkey repetitions: " + to_string(count);
- }
- string decode_vigenere(string crypt, string alph, string arr[], string key) {
- string mess = "";
- int i = 0, n = 0;
- for (int a = 0; a < crypt.length(); a++) {
- n = n > key.length() - 1 ? 0 : n;
- i = alph.find(key[n]);
- bool boolean = false;
- for (int b = 0; b < alph.length(); b++) {
- if (arr[b][i] == crypt[a]) {
- mess += arr[b][0];
- boolean = true;
- break;
- }
- }
- if (boolean) n++; else mess += crypt[a];
- }
- return mess;
- }
- string count_letters(string alph, string crypt) {
- string out = "";
- for (int i = 0; i < alph.length(); i++) {
- out += string(1, alph[i]) + ": " + to_string(count(crypt.begin(), crypt.end(), alph[i])) + "\n";
- }
- return out;
- }
- float crack_vigenere(string alph, string crypt) {
- int n = alph.length();
- int arr[n];
- int coincidence_index = 0, all_letters = 0;
- float out = 0;
- for (int i = 0; i < n; i++) {
- arr[i] = int(count(crypt.begin(), crypt.end(), alph[i]));
- all_letters += arr[i];
- coincidence_index += arr[i] * (arr[i] - 1);
- }
- out = all_letters - 1;
- out *= all_letters;
- out = coincidence_index / out;
- out -= 0.0385;
- out = 0.0285 / out;
- out = (int)(out * 100 + 0.5);
- return (float)out / 100;
- }
- int main() {
- string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
- string * arr;
- string array[alphabet.length()];
- arr = generate_tabula_recta(alphabet);
- for (int i = 0; i < alphabet.length(); i++) {
- array[i] = * (arr + i);
- }
- delete[] arr;
- string mode = "", key = "", message = "", answer = "";
- ofstream output_file ("Vigenere_wyniki.txt", ofstream::out | ofstream::trunc);
- answer = "================|| Zadanie 77.1 ||================\n";
- key = "LUBIMYCZYTAC";
- ifstream file1 ("dokad.txt");
- getline(file1, message);
- file1.close();
- answer += encode_vigenere(message, alphabet, array, key);
- output_file << answer << '\n';
- answer = "================|| Zadanie 77.2 ||================\n";
- ifstream file2 ("szyfr.txt");
- getline(file2, message);
- getline(file2, key);
- file2.close();
- answer += decode_vigenere(message, alphabet, array, key);
- output_file << answer << '\n';
- answer = "================|| Zadanie 77.3 ||================\n";
- answer += string(count_letters(alphabet, message));
- answer += "key lenght: " + to_string(key.length()) + "\ncalculated key length: " + to_string(crack_vigenere(alphabet, message));
- output_file << answer;
- output_file.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement