Underc0de

Programación General => C / C++ => Mensaje iniciado por: NERV0 en Julio 08, 2016, 04:39:27 AM

Título: Cifrado Atbash V2 [C++]
Publicado por: NERV0 en Julio 08, 2016, 04:39:27 AM
Buenas ! Para la facultad tuve que hacer el cifrado Atbash, y la verdad es que no quiero dejarlo "juntando polvo" en el ordenador, sabiendo que a alguien le puede servir...

EL CÓDIGO DE ABAJO TIENE MODIFICACIONES CON RESPECTO AL ORIGINAL, SI QUIEREN VER EL ORIGINAL, VOY A DEJARLO EN LOS COMENTARIOS.

Así que, sin más ni menos, el código:

Código (cpp) [Seleccionar]


#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>

using namespace std;

string texto;
string encriptado;

string alfabeto_normal =    "abcdefghijklmnopqrstuvwxyz";
string alfabeto_invertido = "zyxwvutsrqponmlkjihgfedcba";

string alfabeto_mayusculas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alfabeto_invertido_mayusculas = "ZYXWVUTSRQPONMLKJIHGFEDCBA";


void encriptar_desencriptar (string & texto, string & alfabeto_normal, string & alfabeto_invertido, string & alfabeto_mayusculas, string & alfabeto_invertido_mayusculas, string & encriptado){

for (int posicion_texto=0; posicion_texto < texto.length(); posicion_texto++){

for (int posicion=0; posicion < alfabeto_normal.length(); posicion++){

if (texto[posicion_texto] >= 'a' && texto[posicion_texto] <= 'z'){
if (texto[posicion_texto] == alfabeto_normal[posicion])
{encriptado += alfabeto_invertido[posicion];} }


   
if (texto[posicion_texto] >= 'A' && texto[posicion_texto] <= 'Z'){
if (texto[posicion_texto] == alfabeto_mayusculas[posicion])
{encriptado += alfabeto_invertido_mayusculas[posicion];} }

   }
   
if (texto[posicion_texto] == ' '){encriptado += ' ';}
if (texto[posicion_texto] == ','){encriptado += ',';}
if (texto[posicion_texto] == '.'){encriptado += '.';}}


}






int main(){


system("title Cifrado Atbash");

system("cls");

int eleccion_inicio;

cout << " ____________________________________________ "<<endl;
cout << "|                                            |"<<endl;
cout << "|              Cifrado Atbash                |"<<endl;
cout << "|____________________________________________|"<<endl;
cout << "|                                            |"<<endl;
cout << "|      1. Encriptar/Desencriptar Texto       |"<<endl;
cout << "|                 2. Salir                   |"<<endl;
cout << "|____________________________________________|"<<endl;

cin >>eleccion_inicio;
cin.sync();


if (eleccion_inicio == 2){return 0;}

if (eleccion_inicio == 1){

system("cls");

cout << " ____________________________________________ "<<endl;
cout << "|                                            |"<<endl;
cout << "|              Cifrado Atbash                |"<<endl;
cout << "|____________________________________________|"<<endl;
cout << "|                                            |"<<endl;
cout << "|  Ingrese el texto a encriptar-desencriptar |"<<endl;
cout << "|                                            |"<<endl;
cout << "|____________________________________________|"<<endl;
cout << " "<<endl;

getline(cin, texto);


encriptar_desencriptar (texto, alfabeto_normal, alfabeto_invertido, alfabeto_mayusculas, alfabeto_invertido_mayusculas, encriptado);


system("cls");
cout << " "<<endl;
cout << "Texto original:"<<endl;
cout << " "<<endl;
cout << texto << endl;

cout << " "<<endl;
cout << " --------------------------------------------- "<<endl;
cout << " --------------------------------------------- "<<endl;

cout << " "<<endl;
cout << "Texto encriptado:"<<endl;
cout << " "<<endl;                                    
    cout << encriptado << endl;
   
    cout << " "<<endl;
    cout << " "<<endl;
    cout << "  ________________________________________ "<<endl;
    cout << " |  Presione una tecla para continuar...  |"<<endl;
    cout << " |________________________________________|"<<endl;
   
getch();
   
main();

};

}





Espero que sirva de aporte, saludos ;)
Título: Cifrado Atbash V1 [C++]
Publicado por: NERV0 en Julio 08, 2016, 11:27:36 PM
V1 DEL CODIGO ATBASH:



Código (cpp) [Seleccionar]

#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>

using namespace std;

int encriptar_desencriptar();

string alfabeto_normal =    "abcdefghijklmnopqrstuvwxyz";
string alfabeto_invertido = "zyxwvutsrqponmlkjihgfedcba";

string alfabeto_mayusculas = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alfabeto_invertido_mayusculas = "ZYXWVUTSRQPONMLKJIHGFEDCBA";







/*****************************************************************
*              FUNCION ENCRIPTAR / DESENCRIPTAR                  *
*****************************************************************/

void encriptar_desencriptar_texto(string & texto, string & alfabeto_normal, string & alfabeto_invertido,
string & alfabeto_mayusculas, string & alfabeto_invertido_mayusculas,  string & encriptado){

for (int posicion_texto=0; posicion_texto < texto.length(); posicion_texto++){

for (int posicion=0; posicion < alfabeto_normal.length(); posicion++){

if (texto[posicion_texto] >= 'a' && texto[posicion_texto] <= 'z'){
if (texto[posicion_texto] == alfabeto_normal[posicion])
{encriptado += alfabeto_invertido[posicion];} }



if (texto[posicion_texto] >= 'A' && texto[posicion_texto] <= 'Z'){
if (texto[posicion_texto] == alfabeto_mayusculas[posicion])

{encriptado += alfabeto_invertido_mayusculas[posicion];} }

              }
   
if (texto[posicion_texto] == ' '){encriptado += ' ';}

if (texto[posicion_texto] == ','){encriptado += ',';}

if (texto[posicion_texto] == '.'){encriptado += '.';}

}

};





int main(){

system("title Cifrado Atbash");

int eleccion_inicio;

cout << " ____________________________________________ "<<endl;
cout << "|                                            |"<<endl;
cout << "|              Cifrado Atbash                |"<<endl;
cout << "|____________________________________________|"<<endl;
cout << "|                                            |"<<endl;
cout << "|      1. Encriptar/Desencriptar Texto       |"<<endl;
cout << "|                 2. Salir                   |"<<endl;
cout << "|____________________________________________|"<<endl;

cin >>eleccion_inicio;
cin.sync();




if (eleccion_inicio == 1 || eleccion_inicio ==2){
switch(eleccion_inicio){
case 1:encriptar_desencriptar();break;
case 2:return 0;break;}
} else { main();}
}

int encriptar_desencriptar(){

system("cls");

string texto;
string encriptado;

cout << " ____________________________________________ "<<endl;
cout << "|                                            |"<<endl;
cout << "|              Cifrado Atbash                |"<<endl;
cout << "|____________________________________________|"<<endl;
cout << "|                                            |"<<endl;
cout << "|  Ingrese el texto a encriptar-desencriptar |"<<endl;
cout << "|                                            |"<<endl;
cout << "|____________________________________________|"<<endl;

getline(cin, texto);

encriptar_desencriptar_texto(texto, alfabeto_normal, alfabeto_invertido, alfabeto_mayusculas, alfabeto_invertido_mayusculas, encriptado);


cout << " " <<endl;

cout << " Texto encriptado: "<<endl;
cout << " " <<endl;                                    
    cout << encriptado << endl;
   
    getch();
}


Título: Re:Cifrado Atbash V2 [C++]
Publicado por: ANTRAX en Febrero 15, 2018, 03:16:53 PM
Se ve muy bueno!
Ya lo voy a probar.
Solo lo tenes en C++ o lo tenes porteado a algún otro lenguaje? (Como PHP)

Saludos,
ANTRAX
Título: Re:Cifrado Atbash V2 [C++]
Publicado por: AAAAAAAAAAA en Marzo 10, 2018, 07:24:01 AM
Buen codigo amigo, lo unico mejorable es que el codigo Atbash tiene la caracteristica de "invertir" el alfabeto usado(si tenemos un alfabeto de n letras y la letra a ocupa la posicion i, su nueva posicion seria n-i+1, en el alfabeto ingles la a es z,b es y,c es x...), asi que seria mejor que permitieses al usuario elegir su propio alfabeto ;D
Título: Re:Cifrado Atbash V2 [C++]
Publicado por: NERV0 en Abril 05, 2018, 08:58:08 PM
No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
Se ve muy bueno!
Ya lo voy a probar.
Solo lo tenes en C++ o lo tenes porteado a algún otro lenguaje? (Como PHP)

Saludos,
ANTRAX
Gracias ANTRAX ! No hice port a otro lenguaje, pero podría hacerlo. En unos de estos días lo estaré subiendo en PHP.

No tienes permitido ver los links. Registrarse o Entrar a mi cuenta
Buen codigo amigo, lo unico mejorable es que el codigo Atbash tiene la caracteristica de "invertir" el alfabeto usado(si tenemos un alfabeto de n letras y la letra a ocupa la posicion i, su nueva posicion seria n-i+1, en el alfabeto ingles la a es z,b es y,c es x...), asi que seria mejor que permitieses al usuario elegir su propio alfabeto ;D
Gracias greyocelot, lo que vos estás diciendo es de crear un especie de One Time Pad. Estoy trabajando en eso, pero con un alfabeto extendido que soporte caracteres especiales. Apenas lo termine y vea que funcione correctamente, lo voy a subir.

Saludos a los dos, NERV0.