Friday, April 2, 2010

Text Encryption and Decryption (Cryptograph)

/*******************************************************
APPLICATION : Cryptograph
CODED BY : Ankit Pokhrel
COMPILED ON : Borland Turbo C++ Ver 3.0
DATE : 2009 - 12 - 17
********************************************************/

#include "iostream.h"
#include "conio.h"
#include "process.h"
#include "string.h"
#include "fstream.h"

class encrypt
{
private:
fstream source,target;
char filename[31],temp[31];

public:
encrypt()
{
strcpy(filename,"");
strcpy(temp,"");
}

int check_cndn();
void encode (void);
};

class decrypt
{
private:
fstream source,target;
char filename[31],temp[31];

public:
decrypt()
{
strcpy(filename,"");
strcpy(temp,"");
}

int check_cndn(void);
void decode (void);

};

// Class Encrypt Section

int show_menu()
{

cout << "\n\t\t **********************************" << endl;
cout << "\t\t * ---------------------- *" << endl;
cout << "\t\t * TEXT ENCRYPTION AND DECRYPTION *" << endl;
cout << "\t\t * ---------------------- *" << endl;
cout << "\t\t **********************************" << endl << endl;
cout << "\t ----------------------------------\n\n";
cout << "\t\t\t 1. ENCRYPT DATA\n" << endl;
cout << "\t\t\t 2. DECRYPT DATA\n" << endl;
cout << "\t\t\t 0. Exit\n" << endl;
cout << "\t\t ----------------------------------\n\n";
cout << "\t\t\t Enter Choice (0/1/2) : \n";

int choice;
gotoxy(51,18);
cin >> choice;

cout << endl << "\t ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n";

return choice;

}

int encrypt :: check_cndn()
{
int i;
gotoxy(23,30);
cout << "WARNING : Enter Filename With Extension.";
gotoxy(24,32);
cout << "File Will Be Saved as FILENAME(En)";
gotoxy(15,34);
cout << "If File is Located in another Location Give Full Path to File";
gotoxy(10,22);
cout << "\t\t Enter Filename to Encode : ";
cin >> filename;
source.open(filename,ios::in);
if(source.fail())
{
cout << "\n\t\t\t\tSourcefile Doesn't Exist...";
getch();
return 0;
}

i = 0;
while(filename[i] != '.' || filename[i] == '\0')
temp[i] = filename[i++];
temp[i] = '\0';


strcat(temp,"(En)");

char tempc[6];
i = 0;
int j = 0;
while(filename[i] != '\0')
{
if(filename[i] == '.')
while(filename[i] != '\0')
tempc[j++] = filename[i++];

if(filename[i] == '\0')
--i;
else
i++;
}

tempc[j] = '\0';

strcat(temp,tempc);

target.open(temp,ios::out);
if(target.fail())
{
target.open(temp,ios::out);
if(target.fail())
{
cout << endl << "\t\t\t\tCouldn't Create File...\n";
getch();
return 0;
}
}

return 1;
}


void encrypt :: encode()
{
char ch = ' ',ctemp;
while(ch != EOF)
{
source.get(ch);
if(ch != EOF)
{
ctemp = ch + 0160;
target.put(ctemp);
}
}

source.close();
target.close();

cout << "\n\t\t\t\tData Encoded Successfully.";
getch();
}

// Class Decrypt Section

int decrypt :: check_cndn()
{
int i;
gotoxy(15,30);
cout << "WARNING : Use Files Encoded From This Program to Decode";
gotoxy(10,22);
cout << "\t\t Enter Filename to Decode : ";
cin >> filename;

source.open(filename,ios::in);
if(source.fail())
{
cout << "\n\t\t\t\tSourcefile Doesn't Exist...";
getch();
return 0;
}

i = 0;
while(filename[i] != '.' || filename[i] == '\0')
temp[i] = filename[i++];
temp[i] = '\0';


strcat(temp,"(De)");
char tempc[6];
i = 0;
int j = 0;
while(filename[i] != '\0')
{
if(filename[i] == '.')
while(filename[i] != '\0')
tempc[j++] = filename[i++];

if(filename[i] == '\0')
--i;
else
i++;
}

tempc[j] = '\0';

strcat(temp,tempc);
target.open(temp,ios::out);
if(target.fail())
{
target.open(temp,ios::out);
if(target.fail())
{
cout << endl << "\n\t\t\t\tERROR : Couldn't Create File...\n";
getch();
return 0;
}
}

return 1;
}

void decrypt :: decode()
{
char ch,ctemp;
do
{
source.get(ch);
ctemp = ch - 0160;
if(ctemp != EOF)
target.put(ctemp);

}while(ch != EOF);

source.close();
target.close();

cout << "\n\t\t\t\tData Decoded Successfully.";
getch();
}

int main()
{

int success,choice;
encrypt e;
decrypt d;

do
{
clrscr();
choice = show_menu();
if(choice == 1)
{
success = e.check_cndn();
if(success)
e.encode();
}

else if(choice == 2)
{
success = d.check_cndn();

if(success)
d.decode();
}

else if(choice == 0)
{
cout << "\t\t\tThank You For Using This Program.";
cout << "\n\n\t\t\t Press any Key to halt...";
}


else
{
cout << "\t\t\tPlease Select Apporopriate Option.\n\n";
cout << "\t\t\t Press any key to continue...";
getch();
}

}while(choice != 0);

getch();
return 0;
}

No comments:

Post a Comment