XOR encrypter

Feb 21, 2011 at 4:48pm
Hello.
I'm making a XOR based decryptor, that works like this.
you have a character, fx 3, and a user key, fx 5
in bits:
3 = 00000011
5 = 00000101
-- Now if we do XOR operation, we get 6
6 = 00000110
-- This can be reversed by saying 6 XOR 5, which is 3
...
So I have made this program, but its really buggy, and adds a lot of characters in the end of the file (depeding which key you are using).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using namespace std;
int main(int argc, char *argv[])
{
	char buffer[5001]; 
	ifstream fin("a.txt", ifstream::in);
	ofstream fout("b.txt");
	int key;
	char znak;

	// console

	cout << "Key: ";
	cin >> key;
	fin.get(znak);
	while(!fin.eof() && znak != ' ')
	{
		
		

		fin.get(buffer, sizeof(buffer)); 

	}

	for(int i = 0; i < sizeof(buffer); i++)
	{
		fout << function(key, buffer[i]);
	}

	cout << "done" << endl;

	cin.get();
	return 0;
}

char function(int key,char input)
{
		char encryptedChar = NULL;
		encryptedChar = input ^ key;  
		 return encryptedChar;
}


What am I doing wrong? Do I read the file wrong, or is the function bad?



Feb 21, 2011 at 5:01pm
Line 15 should just be while(!fin.eof()) at the end of the file there's nothing left to do anyway so why does it matter if znak has a value?

In your function you're comparing an integer key with what should be a char key. Integer numbers and Char numbers have different values believe it or not.
Feb 21, 2011 at 5:15pm
#computergeek01
Yeah, the line 15 was a mistake. It was only while(!fin.eof()) , but since it didn't work, I tried other ways.
In my function I'm comparing integer to character, but does it matter? It should work anyway, right?
Feb 21, 2011 at 5:21pm
They're not the same, it seems silly but there are reasons for it. Try this in a seperate project or something:
1
2
    if('2' == 2)
    {std::cout << "YES\n";}


That's the best way I know how to illustrate it.
Last edited on Feb 21, 2011 at 5:22pm
Feb 21, 2011 at 5:24pm
#computergeek01
I know its now the same, but what I meant was that the ecryption operation should still be possible?
Feb 21, 2011 at 5:26pm
It would be possible, but a mathmatical process is only reversable if the values are the same. You have to change three lines of code to test out my suggestion. If it doesn't work then put them right back. What do you have to lose?
Last edited on Feb 21, 2011 at 5:27pm
Feb 21, 2011 at 6:42pm
#computergeek01
okay, here is the code, but its still the same:
1
2
3
4
5
6
7
8
9
10
char function(int key,char input)
{
		int intChar = input;
		char encryptedChar = NULL;
		


		encryptedChar = intChar ^ key;  
		 return encryptedChar;
}
Feb 21, 2011 at 6:50pm
Obligatory style interjection

My post will not help with the problem you're asking about, however it does have some good advice that is somewhat relevant.

1
2
3
4
5
6
7
8
9
10
11
12
13
char function(int key,char input)
{
    int intChar = input;   // you already have 'input', why copy to another variable?
        // why not just use 'input' directly?
        // furthermore, why promote to an int?  it makes no sense here

    char encryptedChar = NULL;  // why initialize it with null...
    encryptedChar = intChar ^ key;    // ...only to reassign it right away?
        // why not just initialize it with what you want?


    return encryptedChar;  // if you're just returning it anyway, why bother with a variable at all?
}


Above function simplified:

1
2
3
4
char function(char key,char input)
{
    return (input ^ key);
}


Save yourself typing. KISS.
Last edited on Feb 21, 2011 at 6:51pm
Feb 21, 2011 at 6:54pm
#Disch
I know my code is very messy, because I edit in it all the time.

In the function, I assign input to intChar, because Computergeek01 said that I have to use two integers, not one integer and one character. (Look above in the comments)

Feb 22, 2011 at 5:23pm
If I left you with the impression that you have to use two integers then I apoligize I only meant to say that the data types have to be the same. You could use two chars, and I think you'd be better off that way anyway.
Topic archived. No new replies allowed.