After reading a tutorial on XOR encryption at cplusplus.com I decided to code
this super program called X-Encrypter, just so I can practice what I've learned
from tutorials.
This is what it does: it can create xor-encrypted text files from scratch,
it can read xor-encrypted text files, and it also allows the user to
set the encryption key.
This is what ive come up with so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string key="k";
void commandScreen(int& command);
void readXorFile(fstream& file);
void newXorFile(fstream& file);
void setKey(string& key);
void encrypt(string str, string& newStr);
int main()
{
int c=0; // stores command chosen by user
fstream file;
do
{
commandScreen(c);
switch(c)
{
case 1:
readXorFile(file);
break;
case 2:
newXorFile(file);
break;
case 3:
setKey(key);
break;
}
}
while(c != 0);
cout<<"\n\nAstalavista.\n";
}
void commandScreen(int& command)
{
cout<<"\n*************************************************\n**************** X-ENCRYPTER 0.1 ****************\n*************************************************\n";
cout<<"\n1-> READ ENCRYPTED TEXT"<<endl;
cout<<"2-> NEW ECRYPTED TEXT"<<endl;
cout<<"3-> SET ENCRYPTION KEY"<<endl;
cout<<"\nCOMMAND: ";
cin>>command;
}
void setKey(string& key)
{
cout<<"\KEY: ";
cin>>key;
cout<<"Key set to '"<<key<<"'"<<endl;
system("PAUSE");
}
void readXorFile(fstream& file)
{
char filePath[100];
string encTxt;
string result;
cout<<"\nENCRYPTED FILE: ";
cin>>filePath;
file.open(filePath,ios::in);
if(file.is_open())
{
file>>encTxt;
encrypt(encTxt,result);
cout<<"\nResult: "<<result<<"\n";
system("PAUSE");
}
else
{
cout<<"\nThere's no such file '"<<filePath<<"'\n";
system("PAUSE");
}
file.close();
}
void newXorFile(fstream& file)
{
char filePath[100]; // it wont allow me to use string on fstream::open()
cout<<"\nNEW FILE: ";
cin>>filePath;
file.open(filePath,ios::out | ios::binary);
//binary because it doesnt need to be txt file
if(file.is_open())
{
string txt;
string enc; // stores encrypted text
cout<<"TEXT: ";
cin>>txt;
encrypt(txt,enc);
file<<enc;
cout<<"\nEncrypted text stored into '"<<filePath<<"'\n";
system("PAUSE");
}
else
{
cout<<"Couldn't create file...\n";
system("PAUSE");
}
file.close();
}
void encrypt(string str, string& newStr)
{
newStr=str;
int strLen = str.length() - 1;
int keyLen = key.length();
for(int x=0; x <= strLen; x++)
{
newStr[x] = str[x]^key[x % keyLen];
}
}
Now these are the issues i've bumped into so far:
1-)No matter how simple it might seem to be, the key setter function never works
right, example: the default key is 'k', but when manually changing it to 'k' it works differently.
2-)When creating a new file, typing the text to be saved, if you enter a spacebar the program will loop infinitely, i dont know why
3-)Somehow I cant figure out how to make the fstream.open() function work with string, I even tried type casting, but it doenst work...
4-)When choosing to read a encrypted file, when it tries to read the character 'a' it stops reading...