Advertisement
Guest User

Untitled

a guest
May 15th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Cesar {
  5.   int shift;
  6.   public:
  7.     void setShift(int x) { shift = x; }
  8.     Cesar (int x) : shift(x) { }
  9.     string cifrar(string input, int cifrar){
  10.       string output;
  11.       // C++ 11
  12.       if(cifrar)
  13.         for(auto x : input) output += char((x - 'A' + shift)%26 + 'A');
  14.       else
  15.         for(auto x : input) output += char((x - 'A' - shift + 26)%26 + 'A');
  16.       return output;
  17.     }
  18. };
  19.  
  20. class Vigenere : public Cesar {
  21.   string chave;
  22.   public:
  23.     Vigenere(string chave) : Cesar(0), chave(chave) { }
  24.     string cifrar(string input, int cifrar){
  25.       int idx = 0;
  26.       while(chave.size() != input.size()) chave += chave[idx], idx++;
  27.       string output;
  28.       Cesar cif(0);
  29.       for(int i = 0; i < input.size(); i++) {
  30.         string aux;
  31.         aux += input[i];
  32.         cif.setShift(int(chave[i] - 'A'));
  33.         output += cif.cifrar(aux, cifrar)[0];
  34.       }
  35.       return output;
  36.     }
  37. };
  38.  
  39. int main(){
  40.   Cesar A(1);
  41.   Vigenere B("LIMAO");
  42.   cout << A.cifrar("ABCDE", 1) << endl;
  43.   cout << A.cifrar(A.cifrar("ABCDE", 1), 0) << endl;
  44.   cout << B.cifrar("ATACARBASESUL", 1) << endl;
  45.   cout << B.cifrar(B.cifrar("ATACARBASESUL", 1), 0) << endl;
  46.   cout << A.cifrar("NHLDTMNLDDGFNJT", 1) << endl;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement