Code:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[])
{
string mykey;
string infile;
string outfile;
int choice;
cout << "1. Encrypt" << endl;
cout << "2. Decrypt" << endl;
cout << "Your Choice : ";
cin >> choice;
cin.ignore();
if(!cin)//Checks for bad input
{
cout << "I tried to figure out what you wanted" << endl;
cout << "but your want eludes me." << endl;
cout << "Program has been terminated!" << endl;
return 1;
}
if(choice == 1)//Load encrypt Section
{
cout << "Enter your key (Must be at least 8 characters): ";
getline(cin, mykey, '\n');
cout << "Location of file to encrypt, Ex \"c:/myfile.txt\" : ";
getline(cin, infile, '\n');
cout << "Location to save encrypted file, Ex \"c:/myoutfile.txt\" : ";
getline(cin, outfile, '\n');
int keysize = mykey.size();
if(keysize <= 7)
{
mykey.clear();
cout << endl << endl;
cout << "Your key was to short, here is a chance to fix that." << endl;
cout << "Enter a Key : ";
getline(cin, mykey, '\n');
keysize = mykey.size();
if(keysize <= 7)
{
cout << endl << endl;
cout << "You have failed to comply, encryption terminated!" << endl;
cin.get();
return 4;
}
}
if(infile.empty())
{
cout << endl << endl;
cout << "How can i encrypt this file \"" << infile << "\" if it does not exsist?" << endl;
cin.get();
return 5;
}
if(outfile.empty())
{
cout << endl << endl;
cout << "How can i save to this file \"" << outfile << "\" if you dont specify one?" << endl; cin.get();
return 5;
}
if(infile == outfile)
{
outfile+=".enc";
}
ifstream in(infile.c_str(), ios::binary);
if(in.fail())//Makes sure both file have been opened before continuing
{
cout << endl << endl;
cout << "I couldnt locate your file, I am sorry." << endl;
cout << "The error is related to this entry : ";
cout << infile << endl;
cin.get();
return 2;
}
ofstream out(outfile.c_str(), ios::binary);
if(out.fail())
{
cout << endl << endl;
cout << "I was unable to create a output file, I am sorry." << endl;
cout << "The error is related to this entry : ";
cout << outfile << endl;
cin.get();
return 2;
}
int i = 0;
int q = 0;
char letter;
char changed1;
char junkletter;
while( in.read( (char*)&letter, sizeof(letter) ))//read one character till end of the file
{
changed1 = letter;
q++;
if((i+3) > keysize)//make sure we wont be overstepping the bound of our keysize
{
i = 0;
}
changed1 -= mykey[i];
i++;
changed1 += mykey[i];
i++;
changed1 -= mykey[i];
i++;
out.write( (char*)&changed1, sizeof(changed1) );//output the character to the new file
if(q == mykey[i])//if q is equal to a character in the key, it time to put a junk character
{
q = 0;
junkletter = (changed1+mykey[i+2]);//create the junk character
out.write( (char*)&changed1, sizeof(changed1) );//out put the junk character
}
}
in.close();
out.close();
}
else //Load Decrypt Section
{
cout << "Enter your key : ";
getline(cin, mykey, '\n');
cout << "Location of the encrypted file, Ex \"c:/myfile.txt\" : ";
getline(cin, infile, '\n');
cout << "Location to save decrypted file, Ex \"c:/myoutfile.txt\" : ";
getline(cin, outfile, '\n');
int keysize2 = mykey.size();
if(keysize2 <= 7)
{
mykey.clear();
cout << endl << endl;
cout << "Your key was to short, here is a chance to fix that." << endl;
cout << "Enter a Key : ";
getline(cin, mykey, '\n');
keysize2 = mykey.size();
if(keysize2 <= 7)
{
cout << endl << endl;
cout << "You have failed to comply, encryption terminated!" << endl;
cin.get();
return 4;
}
}
if(infile.empty())
{
cout << endl << endl;
cout << "How can i decrypt this file \"" << infile << "\" if it does not exsist?" << endl;
cin.get();
return 5;
}
if(outfile.empty())
{
cout << endl << endl;
cout << "How can i save to this file \"" << outfile << "\" if you dont specify one?" << endl;
cin.get();
return 5;
}
ifstream in2(infile.c_str(), ios::binary);
if(in2.fail())//Makes sure both file have been opened before continuing
{
cout << endl << endl;
cout << "I couldnt locate your file, I am sorry." << endl;
cout << "The error is related to this entry : ";
cout << infile << endl;
cin.get();
return 2;
}
ofstream out2(outfile.c_str(), ios::binary);
if(out2.fail())
{
cout << endl << endl;
cout << "I was unable to create a output file, I am sorry." << endl;
cout << "The error is related to this entry : ";
cout << outfile << endl;
cin.get();
return 2;
}
int i2 = 0;
int q2 = 0;
char letter2;
char shanged1;
while( in2.read( (char*)&letter2, sizeof(letter2) ) )//read one character till end of the file
{
q2++;
shanged1 = letter2;
if((i2+3) > keysize2)//make sure we wont be overstepping the bound of our keysize
{
i2 = 0;
}
shanged1 += mykey[i2];
i2++;
shanged1 -= mykey[i2];
i2++;
shanged1 += mykey[i2];
i2++;
out2.write((char*)&shanged1, sizeof(shanged1));//output the character to the new file
if(q2 == mykey[i2])
{
q2 = 0;
in2.read( (char*)&letter2, sizeof(letter2) );//discard the junk character
}
}
in2.close();
out2.close();
}
cout << "HAHA I have completed your tasks, may I have another!" << endl;
cin.get();
return 0;
}