Code:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>
#include <ctype.h>
#define tableSize 80
#define textSize 1000
class Secret
{
int length;
char actual;
char cipher;
char text[textSize];
char temp[textSize];
public:
Secret(){};
Secret(char a, char c){actual=a; cipher=c;}
~Secret(){}
int menu();
void encryptMSG(Secret s[]);
};
void Secret::encryptMSG(Secret s[])
{
char filename[15], ans;
float count=0, time=0.0;
system("cls");
//Prompt to enter message for encryption
cout << "\n";
cout << "\tEncrypt Message" << endl;;
cout << "\t---------------" << endl;
cout << "\n\tEnter Message:" << endl;
cout << "\n\t";
cin.ignore();
cin.getline(text, textSize);
length=strlen(text);
//Encryption process start
for (int x=0; x<length; x++)
{
for (int y=0; y<tableSize; y++)
{
if (text[x]==s[y].actual)
{
temp[x]=s[y].cipher;
count++;
}
}
}
//Display encrypted message
cout << "\n\n";
cout << "\n\tEncrypted Message:" << endl;
cout << "\n\t";
for (int z=0; z<length; z++)
{
cout << temp[z];
}
time=count/10000;
cout << "\n\n\n\n";
cout << "\tTime used for encryption: " << time << " seconds" << endl << endl;
cout << endl << endl;
cout << "\tSave the encrypted message into a file? [Y/N] ";
cin >> ans;
//Save the encrypted message to a file
if (ans=='Y'|| ans=='y')
{
cout << endl;
cout << "\tEnter file name [end with .txt]: ";
cin >> filename;
ofstream ofile(filename);
for (int z=0; z<length; z++)
{
ofile << temp[z];
}
ofile.close();
}
}
int Secret::menu()
{
int choice;
cout << "\n\n";
cout << "\t---------" << endl;
cout << "\tMain Menu" << endl;
cout << "\t---------" << endl;
cout << "\n";
cout << "\t1. Encrypt Message" << endl;
cout << "\t2. Exit" << endl;
cout <<"\n\tYour Choice: ";
cin >> choice;
return choice;
}
void main()
{
int choice;
Secret sc[tableSize]={
Secret('A','z'), Secret('B','a'), Secret('C','q'), Secret('D','w'), Secret('E','s'),
Secret('F','x'), Secret('G','c'), Secret('H','d'), Secret('I','e'), Secret('J','r'), //10
Secret('K','f'), Secret('L','v'), Secret('M','b'), Secret('N','g'), Secret('O','t'),
Secret('P','y'), Secret('Q','h'), Secret('R','n'), Secret('S','m'), Secret('T','j'),
Secret('U','u'), Secret('V','i'), Secret('W','k'), Secret('X','l'), Secret('Y','o'),
Secret('Z','p'), Secret('a','1'), Secret('b','!'), Secret('c','@'), Secret('d','2'), //30
Secret('e','#'), Secret('f','3'), Secret('g','$'), Secret('h','4'), Secret('i','%'),
Secret('j','5'), Secret('k','^'), Secret('l','6'), Secret('m','&'), Secret('n','7'),
Secret('o','*'), Secret('p','8'), Secret('q','9'), Secret('r','0'), Secret('s','/'),
Secret('t','?'), Secret('u','.'), Secret('v',','), Secret('w','+'), Secret('x','-'), //50
Secret('y','='), Secret('z','_'), Secret('+','Y'), Secret('-','Q'), Secret('*','A'),
Secret('/','Z'), Secret(' ','X'), Secret('!','S'), Secret('@','W'), Secret('#','E'),
Secret('$','D'), Secret('%','C'), Secret('^','V'), Secret('&','F'), Secret('_','R'),
Secret('=','T'), Secret('?','G'), Secret('.','B'), Secret(',','N'), Secret('|','H'), //70
Secret('1','~'), Secret('2',':'), Secret('3','<'), Secret('4','>'), Secret('5','{'),
Secret('6','`'), Secret('7','}'), Secret('8','['), Secret('9',']'), Secret('0','U') //80
};
Secret sd;
do
{
choice=sd.menu();
switch(choice)
{
case 1: sd.encryptMSG(sc);
system("cls");
break;
case 2: cout << "\n\t";
exit(2);
default: cout << "\n\tInvalid Choice!" << endl;
break;
}
} while (choice>0 && choice<2);
}