Here's my code.
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
void encrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strlen(str);++i)
{
if(str[i] != key) str[i] = str[i] - key;
}
}
void decrypt(char str[],int key)
{
unsigned int i;
for(i=0;i<strlen(str);++i)
{
if(str[i] != key) str[i] = str[i] + key;
}
}
int main()
{
bool ProgramRunning = true;
int key;
int choice;
char FileLoc[1000];
char str[100000];
string All;
string DataRenew;
fstream PhpFile;
ofstream Write;
while(ProgramRunning)
{
key = 0;
system("cls");
cout <<"1. Encrypt a file."<<endl;
cout <<"2. Decrypt a file."<<endl;
cout <<"3. Add Encrypted files to server."<<endl;
cout <<"4. Remove Encrypted files from server."<<endl;
cin >> choice;
switch (choice)
{
case 1:
key = 0;
system("cls");
cout <<"Write the file location."<<endl;
cout <<">> ";
cin >> FileLoc;
cin.get();
PhpFile.open(FileLoc);
if (!PhpFile)
{
key = 0;
system("cls");
cout << "Could not find file.Press enter to continue.";
cin.get();
}
else if(PhpFile)
{
key = 0;
system("cls");
cout <<"File found."<<endl;
cout <<"\n";
cout <<"File code: \n\n\n\n"<<endl;
while(!PhpFile.eof())
{
PhpFile.getline(str, 100000);
All = All + str + "\n";
}
strcpy_s(str, All.c_str());
cout << str <<" \n\n\n\n"<<endl;
cout <<"Please write the encryption key. NUMBERS ONLY!"<<endl;
cout <<">> ";
cin >> key;
cin.get();
system("cls");
encrypt(str,key);
cout<<"Encrypted value = "<<str<<endl;
key = 0;
PhpFile.close();
cout <<"\n\n";
cout <<"Do you wish to save the Encrypted file?"<<endl;
cout <<"1.Yes"<<endl;
cout <<"2.No"<<endl;
cout <<">> "<<endl;
cin >> choice;
switch (choice)
{
case 1:
Write.open(FileLoc);
Write << str;
Write.close();
break;
case 2:
key = 0;
cin.get();
break;
}
break;
}
case 2:
key = 0;
system("cls");
cout <<"Write the file location."<<endl;
cout <<">> ";
cin >> FileLoc;
cin.get();
PhpFile.open(FileLoc);
if (!PhpFile)
{
key = 0;
system("cls");
cout << "Could not find file.Press enter to continue.";
choice = 0;
cin.get();
cin.get();
}
else if(PhpFile)
{
key = 0;
system("cls");
cout <<"\n";
cout <<"File code: \n\n\n\n"<<endl;
while(!PhpFile.eof())
{
PhpFile.getline(str, 100000);
All = All + str + "\n";
}
strcpy_s(str, All.c_str());
cout << str <<" \n\n\n\n"<<endl;
cout <<"Please write the key."<<endl;
cout <<">> ";
cin >> key;
cin.get();
while(key != 124)
{
PhpFile.getline(str, 100000);
All = All + str;
strcpy_s(str, All.c_str());
key++;
system("cls");
cout <<key<<endl;
decrypt(str,key);
cout<<"Decrypted value = "<<str<<endl;
DataRenew = DataRenew + str;
All = "";
strcpy_s(str, All.c_str());
PhpFile.close();
PhpFile.open(FileLoc);
}
Write.open(FileLoc);
Write << DataRenew;
Write.close();
PhpFile.close();
cout <<"\n\n";
cout <<"Do you wish to save the Decrypted file?"<<endl;
cout <<"1.Yes"<<endl;
cout <<"2.No"<<endl;
cout <<">> "<<endl;
cin >> choice;
switch (choice)
{
case 1:
Write.open(FileLoc);
Write << str;
Write.close();
break;
case 2:
break;
}
cin.get();
break;
}
}
}
return 0;
}
I didn't continue working on it, i was doing this to learn.
And a small bug, when you encrypt a file. close the program, and re open it again to decrypt. just modify it in your needs.
+ You can modify this in whatever you want. example:
Make an encrypter program. That will encrypt automaticly and send it to the server.
The decrypter will read the file and decrypt it. and save it into a log file.