C++ program for
encrypting and
decrypting any file using
Caesar cipher and any key entered by the user.
You may even use this as an
assignment or
mini project in B. Tech. or network security subject by adding little gui and improving the
source code. Feel free to use, modify and share the
code... Knowledge is always free !!!
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class Caesar
{
public: void encrypt(char *inp,char *out,int key);
void decrypt(char *inp,char *out,int key);
void readText(char *inp);
};
void Caesar::encrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='a'&&buf<='z')
{
buf-='a';
buf+=key;
buf%=26;
buf+='A';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::decrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='A'&&buf<='Z')
{
buf-='A';
buf+=26-key;
buf%=26;
buf+='a';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::readText(char *inp)
{
ifstream input;
char buf;
input.open(inp);
cout<<"\n\n <--- "<<inp<<" --->\n";
buf=input.get();
while(!input.eof())
{
cout<<buf;
buf=input.get();
}
input.close();
}
void main()
{
Caesar a;
int choice,key;
char inp[30],out[30];
clrscr();
cout<<"\n Enter input file: ";
cin>>inp;
cout<<"\n Enter output file: ";
cin>>out;
cout<<"\n Enter key: ";
cin>>key;
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): ";
cin>>choice;
if(choice==1)
a.encrypt(inp,out,key);
else if(choice==2)
a.decrypt(inp,out,key);
else cout<<"\n\n Unknown choice";
getch();
}