Advertisement
AntonioVillanueva

Enigma + Codigo Cesar

Mar 26th, 2020
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. //Lo emplea la codificacion cesar para desplazar letras en las dos direcciones
  9. char letraCiclica (char letra,int offset){//Sube o baja una letra d
  10.     bool sentido(offset>0 ? true : false);
  11.    
  12.     while (offset !=0){    
  13.         sentido ? offset --: offset++ ;
  14.         sentido ? ++letra : --letra ;//Sube o baja en el abecedario
  15.  
  16.         if (letra<'A'){letra ='Z';}//La letra esta por debajo de A pasa a Z
  17.         if (letra>'Z'){letra ='A';}//La letra esta por encima de Z pasa a A
  18.     }
  19.    
  20. return letra;  
  21. }
  22.  
  23. //***********************************************************************************************
  24. //Codificacion cesar
  25. string cesar(string &mensaje,int shift,bool operacion=true){
  26.     //Rotacion ciclica
  27.     if (operacion){//ENCODE
  28.         for (unsigned index=0;index<mensaje.size();index++){//Recorre letras del mensaje           
  29.             mensaje[index]=letraCiclica(mensaje[index],shift+index);           
  30.            
  31.         }
  32.     }else{//DECODE
  33.  
  34.         for (int index=mensaje.size()-1;index>=0;index--){//Recorre letras del mensaje     
  35.             mensaje[index]=letraCiclica(mensaje[index],-(shift+index));        
  36.         }              
  37.     }
  38.     return mensaje;
  39. }
  40.  
  41. //***********************************************************************************************
  42. //Recibe un ROTOR numero rotor ,
  43. string rotorCode(vector<string> &rotores,int rotor,string alfabeto ,string &mensaje,bool operacion){
  44.     string tmp;
  45.  
  46.     //cout <<mensaje<<endl;
  47.     for (auto letra:mensaje){//Recorre las letras del mensaje
  48.                
  49.         for (unsigned index=0;index<alfabeto.size();index++){//Busca la letra en el rotor , ojo el sentido
  50.            
  51.             //ENCODE
  52.             if (operacion && alfabeto [index] ==letra ) {//Busca la letra a codificar en el alfabeto directo ...
  53.                 tmp+=rotores[rotor][index];//Anade la letra codificada a tmp
  54.             }
  55.            
  56.             //DECODE
  57.             if ( !operacion && rotores[rotor][index] ==letra ) {//Busca la letra a codificar en el alfabeto directo ...
  58.                 tmp+=alfabeto [index];//Anade la letra decodificada a tmp
  59.             }                      
  60.         }      
  61.     }
  62.     return tmp;
  63. }
  64. //***********************************************************************************************
  65. string enigma(vector<string> &rotores,string &alfabeto,string &mensaje,int shift ,string operacion){
  66.    
  67.     //Primera codificacion ENCODE o decodificacion codigo CESAR
  68.     if (operacion=="ENCODE"){ cesar(mensaje,shift); }
  69.  
  70.     // Codificacion por ROTOR - N
  71.    
  72.     if (operacion=="ENCODE"){
  73.         for (int rotor=0;rotor<3;rotor ++){//Recorre 3 rotores 0 1 2
  74.             mensaje=rotorCode(rotores,rotor,alfabeto ,mensaje, ( operacion=="ENCODE"? true :false) );//Cambio de rotor
  75.         }  
  76.     }else{
  77.         for (int rotor=2;rotor>=0;--rotor ){//Recorre 3 rotores 2 1 0
  78.             mensaje=rotorCode(rotores,rotor,alfabeto ,mensaje, ( operacion=="ENCODE"? true :false) );//Cambio de rotor
  79.         }          
  80.     }
  81.  
  82.     //ultima decodificacion DECODE si es decode
  83.     if (operacion!="ENCODE"){ cesar(mensaje,shift,false); } //DECODE
  84.     return mensaje;
  85. }
  86.  
  87. //***********************************************************************************************
  88. //***********************************************************************************************
  89. int main()
  90. {
  91.  
  92.     vector<string> rotores;
  93.     string alfabeto("ABCDEFGHIJKLMNOPQRSTUVWXYZ");//Alfabeto de base a combinar con ROTORES  
  94.    
  95.     string Operation;
  96.     getline(cin, Operation);//Encode or Decode
  97.    
  98.     int pseudoRandomNumber;//Starting shift
  99.     cin >> pseudoRandomNumber; cin.ignore();
  100.  
  101.     for (int i = 0; i < 3; i++) {//ROTORs 0 1 2
  102.         string rotor;
  103.         getline(cin, rotor);
  104.         rotores.push_back(rotor);
  105.     }
  106.    
  107.     string message;//Mensaje to Encode or Decode
  108.     getline(cin, message);
  109.  
  110.     cout <<enigma(rotores,alfabeto,message,pseudoRandomNumber ,Operation)<<endl;
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement