Okay so i was bored and whipped up (what seems to be yet another) XOR encryptor in about a half hour tops, and just as the million other ones i have on here, it doens't work 100% =(
It decrypts the first couple characters of the message but from there it's just garbage. Can't figure i out, here it is (important parts highlighted):
Code:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
void crypt(string file);
void decrypt(string file);
void writeFile(string file, string contents);
void readFile(string file, string& contents);
int stringToInt(string n);
int power(char c, int i);
int main()
{
//Infinite Loop
while(1)
{
//Clearscrean
system("cls");
//title
cout<<"****************************\n";
cout<<"* Exclusive Or Encryptor *\n";
cout<<"* V. 1.0.1 *\n";
cout<<"****************************"<<endl<<endl;
//Menu
cout<<"(1) Encrypt File\n";
cout<<"(2) Decrypt File\n";
cout<<"(3) New File\n";
cout<<"(4) View File\n";
cout<<"(5) About\n";
cout<<"(6) Help\n";
cout<<"(7) Exit"<<endl<<endl;
int mchoice=0;
cout<<"Choice: ";
cin>>mchoice;
cin.ignore();
//evaluate
if(mchoice == 1)
{
//encrypt file
cout<<"File: ";
string eFile;
getline(cin, eFile);
crypt(eFile);
cout<<"Done!";
cin.ignore();
continue;
}
else if(mchoice == 2)
{
//Decrypt File
cout<<"File: ";
string dFile;
getline(cin, dFile);
decrypt(dFile);
cout<<"Done!";
cin.ignore();
continue;
}
else if(mchoice == 3)
{
//New File
cout<<"Name: ";
string nFile;
getline(cin, nFile);
cout<<"Enter Contents:\n";
string contents;
getline(cin, contents);
//Write File
writeFile(nFile, contents);
cout<<"Done - File NOT Encrypted!";
cin.ignore();
continue;
}
else if(mchoice == 4)
{
//view file
cout<<"File: ";
string filename;
getline(cin, filename);
ifstream fin(filename.c_str());
char ch;
while(fin.get(ch))
{
cout<<ch;
}
fin.close();
cout<<endl<<"DONE!"<<endl;
cin.ignore();
continue;
}
else if(mchoice == 5)
{
//About
cout<<"This is a basic XOR encryption program"<<endl
cin.ignore();
continue;
}
else if(mchoice == 6)
{
//Help
cout<<"UNDER CONSTRUCTION!";
cin.ignore();
continue;
}
else if(mchoice == 7)
{
//Exit
cout<<"Press Enter To Exit...";
cin.ignore();
break;
}
else{
//incorrect choice
cout<<"INCORRECT CHOICE!";
cin.ignore();
continue;
}
}
return 0;
}
void crypt(string file)
{
string contents;
string key;
cout<<"Key: ";
getline(cin, key);
readFile(file, contents);
//Encrypt
vector<int> encrypted;
for(int i=0; i<contents.size(); i++)
{
encrypted.push_back(((int)contents.at(i))^(key.at((i%(key.size()-1)))));
}
//Write it to file
ofstream fout(file.c_str());
fout<<encrypted.at(0);
for(int i=1; i<encrypted.size(); i++)
{
fout<<" "<<encrypted.at(i);
}
fout.close();
//Finished
}
void writeFile(string file, string contents)
{
ofstream fout(file.c_str());
fout<<contents;
fout.close();
}
void readFile(string file, string& contents)
{
ifstream fin(file.c_str());
char ch;
while(fin.get(ch))
{
contents.push_back(ch);
}
fin.close();
}
void decrypt(string file)
{
//Get file information and Key
cout<<"Key: ";
string key;
getline(cin, key);
string contents;
readFile(file, contents);
//Decode to Decipher
vector<int> decrypted;
//get individual numbers
string temp;
for(int i=0; i<contents.size(); i++)
{
if(contents.at(i) != ' ')
{
temp.push_back(contents.at(i));
}else{
decrypted.push_back(stringToInt(temp));
temp.clear();
}
}
decrypted.push_back(stringToInt(temp));
temp.clear();
//Decrypt
for(int i=0; i<decrypted.size(); i++)
{
decrypted.at(i) = (decrypted.at(i)^key.at(i%key.size()));
}
for(int i=0; i<decrypted.size(); i++)
{
temp.push_back((char)decrypted.at(i));
}
writeFile(file, temp);
//Done
}
int stringToInt(string n)
{
int size = n.size();
int num=0;
for(int i=size; i>0; i--)
{
num += power(n.at(size-i), i);
}
return num;
}
int power(char c, int i)
{
int n;
if(c == '0')
{
n = 0;
}else if(c == '1')
{
n = 1;
}else if(c == '2')
{
n = 2;
}else if(c == '3')
{
n = 3;
}else if(c == '4')
{
n = 4;
}else if(c == '5')
{
n = 5;
}else if(c == '6')
{
n = 6;
}else if(c == '7')
{
n = 7;
}else if(c == '8')
{
n = 8;
}else if(c == '9')
{
n = 9;
}
int m=1;
for(int j=1; j<i; j++)
{
m *= 10;
}
return(n*m);
}
Thanks for any help you can give!