1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
#include <fstream>
#include <iostream>
using namespace std;
class Encryption
{
protected:
int key;
ifstream inFile;
ofstream outFile;
public:
Encryption(char *inFileName, char *outFileName);
~Encryption();
//Pure virtual funtion
virtual char transform(char ch, int key) = 0;
// virtual char unTransform(char ch, int key) = 0;
// do the encryption work
void encrypt(int key);
void decrypt(int key);
int setEncKey(int key);
};
Encryption::Encryption(char *inFileName, char *outFileName)
{
inFile.open(inFileName);
outFile.open(outFileName);
if(!inFile)
{
cout << "The file " << inFileName << " can not be opened." << endl;
exit(1);
}
if(!outFile)
{
cout << "The file " << outFileName << " can not be opened." << endl;
exit(1);
}
}
/***********************************
| Desructor Closes the files |
\***********************************/
Encryption::~Encryption()
{
inFile.close();
outFile.close();
}
/***********************************
| Set the encryption key for usage |
\**********************************/
int Encryption::setEncKey(int key)
{
this -> key = key;
return key;
}
/***********************************
| Encrypt Function uses the virtual transform |
| member function to transform individual |
| characters. |
\**********************************/
void Encryption::encrypt(int key)
{
char ch;
char transCh;
inFile.get(ch);
while(!inFile.fail())
{
transCh = transform(ch, key);
outFile.put(transCh);
inFile.get(ch);
}
}
/*********************************
| Unencryption, takes chars and subtracts |
| the key from the char to get original value |
\*********************************/
void Encryption::decrypt(int key)
{
char ch;
char transCh;
inFile.get(ch);
while(!inFile.fail())
{
transCh = transform(ch, key);
outFile.put(transCh);
inFile.get(ch);
}
}
//The subclass simply overrides the virtual transformation function
class SimpleEncryption : public Encryption
{
public:
char transform(char ch, int key)
{
return ch + key;
}
SimpleEncryption(char *inFileName, char *outFileName) : Encryption(inFileName, outFileName) { };
};
class simpleDecryption : public Encryption
{
public:
char transform(char ch, int key)
{
return ch - key;
}
simpleDecryption(char *inFileName, char *outFileName) : Encryption(inFileName, outFileName) { };
};
int main()
{
int key = 0;
char inFileName[80], outFileName[80];
cout << "\t\t\t-=[ File Encryption ]=-\n" << endl;
cout << "Enter name of file to encrypt: ";
cin >> inFileName;
cout << "Enter name of file to recieve encrypted text: ";
cin >> outFileName;
cout << "Enter encryption key: ";
cin >> key;
SimpleEncryption obfuscate(inFileName, outFileName);
obfuscate.encrypt(obfuscate.setEncKey(key));
system("cls");
cout << "\t\t\t-=[ Encryption Completed ]=-\n" << endl;
cout << "\t\t\t-=[ File Decryption ]=-\n" << endl;
cout << "Enter name of file for decryption: ";
cin >> inFileName;
cout << "Enter name of file for decrypted output: ";
cin >> outFileName;
key = 0;
cout << "Enter the decryption key: ";
cin >> key;
simpleDecryption unObfuscate(inFileName, outFileName);
unObfuscate.decrypt(unObfuscate.setEncKey(key));
cout << "\t\t\t-=[ Decryption Completed ]=-\n" << endl;
return 0;
}
| |