#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iostream>
using namespace std;
void Escribir();
void Leer();
int main(){
system("cls");
char opcion, finalizar='0';
while(finalizar=='0'){
cout<<"Bienvenido, seleccione una opcion: "<<endl
<<"1.....................cifrar y guadar una frase."<<endl
<<"2.....................Leer y descifrar una frase."<<endl
<<"3.....................Salir del programa."<<endl
<<"Opcion a seleccionar: ";
cin>>opcion;
system("cls");
switch (opcion){
case '1': Escribir(); break;
case '2': Leer(); break;
case '3': finalizar='1'; break;
}
}
return 0;
}
void Escribir(){
FILE* file;
file=fopen("encrypted.txt", "ab");
char Alphabet[]= {"abcdefghijklmnopqrstuvwxyz 123456789"};
char Decrypter[]={"kw9svn8y7od6bmxtz4fl3gu2ahp10jcqire5"};
char Entrada[5000];
char Salida[5000];
bool espacio=true;
cout<<"Digite la frase que desea cifrar:\n";
cin.ignore();
cin.getline(Entrada, 5000);
for(int x=0;x<=strlen(Entrada); x++)
Entrada[x]=tolower(Entrada[x]);
for(int x=0;x<=strlen(Entrada); x++){
for(int y=0;y<=strlen(Decrypter); y++)
if(Entrada[x]==Alphabet[y]){
Salida[x]=Decrypter[y];
espacio=false;
break;
};
if(espacio==true)
Salida[x]=' ';
espacio=true;
};
cout<<endl<<"La palabra cifrada:\n"<<Salida<<", ha sido guardada."<<endl<<endl;
fwrite(&Salida, sizeof(Salida), 1, file);
fclose(file);
system("pause");
system("cls");
}
void Leer()
{
FILE* file;
file=fopen("encrypted.txt", "rb");
if(file==NULL)
{cout<<"No se ha cifrado ninguna frase anteriormente. Por favor realizarlo y"<<endl
<<"vuelva a intentarlo."<<endl
<<"-------------------------------------------------------------------"<<endl;
system("cls");
return;};
char Alphabet[]= {"abcdefghijklmnopqrstuvwxyz 123456789"};
char Decrypter[]={"kw9svn8y7od6bmxtz4fl3gu2ahp10jcqire5"};
char Entrada[5000];
char Salida[5000];
bool espacio=true;
fread(&Entrada, sizeof(Entrada), 1, file);
cout<<"La frase cifrada es:\n"<<Entrada<<endl;
for(int x=0;x<=strlen(Entrada); x++)
{for(int y=0;y<=strlen(Decrypter); y++)
if(Entrada[x]==Decrypter[y])
{Salida[x]=Alphabet[y];
espacio=false;
break;};
if(espacio==true)
Salida[x]=' ';
espacio=true;};
cout<<endl<<"La frase descifrada es:\n"<<Salida<<endl<<endl;
fclose(file);
remove("encrypter.txt");
system("pause");
system("cls");
}