Advertisement
Ciemny_Cygan

Vigenere

Oct 30th, 2020 (edited)
2,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. /*
  2.     Program for Encryption/Decryption in Vigenere Cipher
  3.     © Copyright 2020 Iwo Strzeboński
  4.     Published under WTFPLv2
  5.     Language: C++
  6.  */
  7.  
  8. /*
  9.   Files:
  10.     vigenere.key: file with encryption.decryption key
  11.     message.txt: file with message to be encrypted
  12.     message.enc: file with message to be decrypted (known also as cryptograph)
  13.  */
  14.  
  15. #include <iostream>
  16. #include <fstream>
  17. #include <string>
  18.  
  19. using namespace std;
  20.  
  21. string * generate_tabula_recta(string alph) {
  22.     int n = alph.length();
  23.     string * arr = new string[n];
  24.  
  25.     for (int i = 0; i < n; i++) {
  26.         arr[i] = alph.substr(i, n) + alph.substr(0, i);
  27.     }
  28.  
  29.     return arr;
  30. }
  31.  
  32. string decode_vigenere(string crypt, string alph, string arr[], string key) {
  33.     string mess = "";
  34.     int i = 0, n = 0;
  35.  
  36.     for (int a = 0; a < crypt.length(); a++) {
  37.         n = n > key.length() - 1 ? 0 : n;
  38.         i = alph.find(key[n]);
  39.  
  40.         for (int b = 0; b < alph.length(); b++) {
  41.             if (arr[b][i] == crypt[a]) {
  42.                 mess += arr[b][0];
  43.                 break;
  44.             }
  45.         }
  46.  
  47.         n++;
  48.     }
  49.  
  50.     return mess;
  51. }
  52.  
  53. string encode_vigenere(string mess, string alph, string arr[], string key) {
  54.     string crypt = "";
  55.     int i = 0, n = 0;
  56.  
  57.     for (int a = 0; a < mess.length(); a++) {
  58.         n = n > key.length() - 1 ? 0 : n;
  59.         i = alph.find(key[n]);
  60.  
  61.         for (int b = 0; b < alph.length(); b++) {
  62.             if (arr[b][0] == mess[a]) {
  63.                 crypt += arr[b][i];
  64.                 break;
  65.             }
  66.         }
  67.  
  68.         n++;
  69.     }
  70.  
  71.     return crypt;
  72. }
  73.  
  74. int main() {
  75.     ifstream keyfile ("vigenere.key");
  76.    
  77.     string alphabet = "";
  78.    
  79.     for(int a = 0; a < 128; a++) {
  80.         if(isprint(a)) alphabet += a;
  81.     }
  82.  
  83.     string * arr;
  84.     string array[alphabet.length()];
  85.     arr = generate_tabula_recta(alphabet);
  86.  
  87.     for (int i = 0; i < alphabet.length(); i++) {
  88.         array[i] = * (arr + i);
  89.     }
  90.  
  91.     delete[] arr;
  92.  
  93.     string mode = "", key = "", cryptograph = "", message = "";
  94.  
  95.     getline(keyfile, key, (char)keyfile.eof());
  96.  
  97.     while (true) {
  98.         cout << "[D]ecode or [E]ncode? ";
  99.         getline(cin, mode);
  100.        
  101.         if (mode == "D") {
  102.             ifstream cryptographfile ("message.enc");
  103.             ofstream messagefile ("message.txt");
  104.  
  105.             getline(cryptographfile, cryptograph, (char)cryptographfile.eof());
  106.             messagefile << decode_vigenere(cryptograph, alphabet, array, key);
  107.  
  108.             break;
  109.         } else if (mode == "E") {
  110.             ifstream messagefile ("message.txt");
  111.             ofstream cryptographfile ("message.enc");
  112.  
  113.             getline(messagefile, message, (char)messagefile.eof());
  114.             cryptographfile << encode_vigenere(message, alphabet, array, key);
  115.  
  116.             break;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement