Encryption Program Woes...

This is a discussion on Encryption Program Woes... within the C++ Programming forums, part of the General Programming Boards category; Okay so i was bored and whipped up (what seems to be yet another) XOR encryptor in about a half ...

  1. #1
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434

    Encryption Program Woes...

    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!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  2. #2
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Just a thought, but ever think of using Case Switch statements? A lot less Else-Ifing involved, and it's easier to read.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks that you might be reading the file incorrectly. Or at least in a very complicated way.

    It looks that the encrypted file would contain a sequence of space-separated numbers, like: 4 23 77...

    Any reason why you read that as a string and then parse into an int vector manually and not read it into a vector of ints directly as they are saved? In short, you can read (white-space delimited) numbers from a file and don't need to convert them into a number yourself.

    Code:
    vector<int> numbers;
    int temp;
    while (fin >> temp)
        numbers.push_back(temp);
    Just a thought, but ever think of using Case Switch statements? A lot less Else-Ifing involved, and it's easier to read.
    Could be an improvement but in the power function one might notice a pattern and do without either.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Thanks! I impolemented what you said and it simplified things but the problem was still there. I figured it out however, and my method worked, ha, it just overcomplicated things.

    Works great now, thanks for the input!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encryption Program (help me please)
    By Arkanos in forum Windows Programming
    Replies: 7
    Last Post: 10-30-2005, 07:01 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. Basic encryption program???
    By Finchie_88 in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2004, 09:01 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21