Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- class Cesar {
- int shift;
- public:
- void setShift(int x) { shift = x; }
- Cesar (int x) : shift(x) { }
- string cifrar(string input, int cifrar){
- string output;
- // C++ 11
- if(cifrar)
- for(auto x : input) output += char((x - 'A' + shift)%26 + 'A');
- else
- for(auto x : input) output += char((x - 'A' - shift + 26)%26 + 'A');
- return output;
- }
- };
- class Vigenere : public Cesar {
- string chave;
- public:
- Vigenere(string chave) : Cesar(0), chave(chave) { }
- string cifrar(string input, int cifrar){
- int idx = 0;
- while(chave.size() != input.size()) chave += chave[idx], idx++;
- string output;
- Cesar cif(0);
- for(int i = 0; i < input.size(); i++) {
- string aux;
- aux += input[i];
- cif.setShift(int(chave[i] - 'A'));
- output += cif.cifrar(aux, cifrar)[0];
- }
- return output;
- }
- };
- int main(){
- Cesar A(1);
- Vigenere B("LIMAO");
- cout << A.cifrar("ABCDE", 1) << endl;
- cout << A.cifrar(A.cifrar("ABCDE", 1), 0) << endl;
- cout << B.cifrar("ATACARBASESUL", 1) << endl;
- cout << B.cifrar(B.cifrar("ATACARBASESUL", 1), 0) << endl;
- cout << A.cifrar("NHLDTMNLDDGFNJT", 1) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement