Code:
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <cstdio>
using namespace std;
int encryptchar(char ech);
char decryptchar(int dch);
void Encryption();
void Decryption();
int main()
{
cout << "File Encryption" << endl << endl;
cout << "1. Encrypt file" << endl;
cout << "2. Decrypt file" << endl << endl;
cout << "Command:";
int command;
cin >> command;
switch(command)
{
case 1:
Encryption();
case 2:
Decryption();
default:
exit(1);
}
return 0;
}
void Encryption()
{
cout << endl << "File to encrypt:";
string eFilepath;
cin >> eFilepath;
string eFileoutput;
cout << "Fileoutput:";
cin >> eFileoutput;
try
{
FILE *eFile;
FILE *eOutput;
eFile = fopen(eFilepath.c_str(), "r");
eOutput = fopen(eFileoutput.c_str(), "w");
while(!(feof(eFile)))
{
char ch;
ch = fgetc(eFile);
if(feof(eFile))
break;
cout << encryptchar(ch) << endl;
fprintf(eOutput, "%d", encryptchar(ch));
fprintf(eOutput, "\n");
}
fclose(eFile);
fclose(eOutput);
}
catch(exception e)
{
cout << endl << "ERROR!" << endl;
}
cout << endl << "File successfully encrypted." << endl;
cin.ignore().get();
exit(1);
}
void Decryption()
{
cout << endl << "File to decrypt:";
string dFilepath;
cin >> dFilepath;
cout << "Fileoutput:";
string dOutputpath;
cin >> dOutputpath;
try
{
ifstream dFile;
FILE *dOutput;
dFile.open(dFilepath.c_str());
dOutput = fopen(dOutputpath.c_str(), "w");
while(!(dFile.eof()))
{
string strLine;
getline(dFile, strLine);
if(dFile.eof())
break;
int intLine = atoi(strLine.c_str());
cout << decryptchar(intLine);
fprintf(dOutput, "%c",decryptchar(intLine));
}
dFile.close();
fclose(dOutput);
}
catch(exception e)
{
cout << endl << "ERROR!" << endl;
}
cout << endl << "File successfully decrypted." << endl;
cin.ignore().get();
exit(1);
}
/*
Functions and Encryptchar Decryptchar can be changed.
For example, passing an integer as a password, each individual number
different effect. pw = 1234, for example: +1 * 2/3 + 4
*/
int encryptchar(char ech)
{
int returnInteger = (int)ech;
returnInteger += 23;
returnInteger *= 2;
returnInteger -= 17;
returnInteger *= 5;
returnInteger -= 35;
return returnInteger;
}
char decryptchar(int dch)
{
dch += 35;
dch /= 5;
dch += 17;
dch /= 2;
dch -= 23;
char returnChar = (char)dch;
return returnChar;
}