I wrote this simple program for my little cousin who wanted to see something useful done with a computer.
The bug it has is not much problem when 'encrypting' (sort of) text files.
The problem it has is that every time I encrypt a file and decrypt it, an extra character is introduced in the last line.Not a single of the individual functions (apparently) show this behaviour when tested individually .
I'm pasting the whole code for convenience.
[Note that you'd have to apply a c_str() to strings when used with the fstream open functions , if your compiler does not support c++0x/11 features. If it does, remember to tell it to explicitly do so...by something like gcc's -std=c++0x flag]
Code:
#include<fstream>
#include<iostream>
using namespace std;
void encryption(string& data,string passkey);
void decryption(string& data,string passkey);
string get_file_input(string filename);
void output_to_file(const string& data, string filename);
bool quit_condition();
void get_input(string&,string&,string&,string&);
int main(int argc,char** argv)
{
string in_file_name,out_file_name,data,passkey;
string enc_or_dec;
bool input_ready;
if(argc==1)input_ready=false;
else
{
in_file_name = string(argv[1]);
out_file_name = string(argv[2]);
passkey = string(argv[3]);
enc_or_dec = string(argv[4]);
input_ready = true;
}
while(true)
{
if(input_ready);
else
get_input(in_file_name,out_file_name,passkey,enc_or_dec);
data = get_file_input(in_file_name);
if(enc_or_dec[0]=='e'||enc_or_dec[0]=='E')
encryption(data,passkey);
else if(enc_or_dec[0]=='d'||enc_or_dec[0]=='D')
decryption(data,passkey);
else
{
cout<<"Enter ( E or D )! .*****."<<endl;
if(!quit_condition())break;
else continue;
}
output_to_file(data,out_file_name);
if(input_ready)break;
//^The Program doesn't allow the user to continue after supplying cmdline args
if(!quit_condition())break;
}
return 0;
}
string get_file_input(string filename)
{
ifstream is(filename);
if(!is)
{
cout<<"File "<<filename<<" not found. Please try again.\n";
return "";
}
else if(is.fail())
{
cout<<"Opening "<<filename<<" failed for some reason. Please try again.\n";
return "";
}
string line,data("");
do
{
getline(is,line);
data = data+line+"\n";
}while(!is.eof());
return data;
}
void output_to_file(const string& data, string filename)
{
ofstream os(filename);
os<<data;
}
bool quit_condition()
{
string q;
cout<<"Quit?\n$ ";
//q=getchar();
cin>>q;
if(q[0]=='y'||q[0]=='Y')
return false;
else return true;
}
void encryption(string& data,string passkey)
{
int data_end(data.size()),passkey_bound(passkey.size());
for(int counter=0;counter<data_end;counter++)
{
data[counter]+=passkey[counter%passkey_bound];
}
}
void decryption(string& data,string passkey)
{
int data_end(data.size()),passkey_bound(passkey.size());
for(int counter=0;counter<data_end;counter++)
{
data[counter]-=passkey[counter%passkey_bound];
}
}
void get_input(string& in_file_name,string& out_file_name,string& passkey,string& enc_or_dec)
{
cout<<"Enter Input filename:\n$ ";
cin>>in_file_name;
cout<<"Enter Output filename:\n$ ";
cin>>out_file_name;
cout<<"Enter Passkey:\n$ ";
cin>>passkey;
cout<<"Enter ( E or D ) for enc/dec:\n$ ";
cin>>enc_or_dec;
}
There is also a problem with an extra newline in the get_file_input function. I am also looking for a way to remove the last newline.