XOR encryption problem

Ok, For some reason when I go to encrypt a specific string it just disappears completely.

any help?

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//part of the Database class
void save()
    {
        ofstream write("friends.dat", ios::out);
        write<<count<<'\n';
        saveNode(parent, write);
        write.close();
    }

void saveNode(Node *root, ofstream & write)
    {
	char pwd[11] = "CSC220LEON";
        if (root != NULL)
        {
			string s = root->getLastName();
			write << encrypt(s, pwd) << '\n';
			s = root->getFirstName();
			write << encrypt(s, pwd) << '\n';
			s = root->getMonth();
			write << encrypt(s, pwd) << '\n';
			s = root->getDay();
			write << encrypt(s, pwd) << '\n';
			s = root->getYear();
			write << encrypt(s, pwd) << '\n';
			s = root->getTelephone();
			write << encrypt(s, pwd) << '\n';
			s = root->getStreet();
			write << encrypt(s, pwd) << '\n';
			s = root->getCity();
			write << encrypt(s, pwd) << '\n';
			s = root->getState();
			write << encrypt(s, pwd) << '\n';
			s = root->getZip();
			write << encrypt(s, pwd) << '\n';
			saveNode(root->getLeft(), write);
            saveNode(root->getRight(), write); 
        }
    }

string encrypt(string s, char key[11])
	{
		char *temp = toCharArray(s);
		int len = s.size();
		for (int i = 0, j = 0; i < len; i++)
		{
			temp[i] ^= key[j++];
			if (key[j] == '\0')
			{
				j = 0;
			}
		}

		return string(temp);
	}



int main(int argc, char *argv[])
{
	string one = "/?";
	if (argc == 2 && argv[1] == one)
	{
		cout << "Usage: \ncontact project /? (Help)\ncontact project (Display birthdays)\ncontact project -m (Display menu)\n";
	}
	else
	{
		Database d;
		d.load(); //loads the file on start
		cout<<"\nHello!\nWelcome to your personal Contact book."<<endl;
		d.loop(); //loops through the program in database;
		d.save(); //leaves the file on exit (here is where I'm having the problem)
		return 0;
	}
}




All of the info that it is saving/encrypting is coming from the keyboard by the user. When the information is initially saved it is showed correctly because it has yet to encrypted, but when it starts to save the info and encrypt it the City string and the Zip string completely disappears while it is being encrypted (according to my compiler during break points in encrypt.)

Any and all help will be appreciated!
Last edited on
char *temp = toCharArray(s);

I don't know how this works -- but it either has undefined behavior, or it is a memory leak.

Why do you need to do this anyway? You can use the [] operator on a string, you don't need to convert it to a char array.
new encrypt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string encrypt(string s, char key[11])
	{
		int len = s.size();
		for (int i = 0, j = 0; i < len; i++)
		{
			temp[i] ^= key[j++];
			if (key[j] == '\0')
			{
				j = 0;
			}
		}

		return s;
	}

even like this still does not work. Is there any thing else that I should show that might help me fix this problem?
Last edited on
encrypt is still modifying 'temp' and not 's'


Also you'll have other problems which may or may not be related to this. For example, if you are left with a \r, \n, or \0 character after encryption, this will throw a wrench in your file saving/reading.
Oh, I'm sorry... I fixed it right in my program but not here

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string encrypt(string s, char key[11])
	{
		int len = s.size();
		for (int i = 0, j = 0; i < len; i++)
		{
			s[i] ^= key[j++];
			if (key[j] == '\0')
			{
				j = 0;
			}
		}

		return s;
	}


Again, It still does not work like this it does the same as it did before.
Pertaining to the '\0'; Don't worry about that. The program accepts it.
The problem is that only City and Zip do not get encrypted for some reason. The rest of the information gets encrypted correctly, EXCEPT City and Zip... I do everything the same as the rest but for some reason those two do not get encrypted because (while i set some breakpoints and looked at what it did) it would, for some reason, erase the string and just not encrypt it.
Last edited on
I ended up deleting this encryption and used something completely different. thanks for the help anyway.

This is what I ended up doing
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
	bool encrypt(char *pass, char *i, char *o)
	{
		FILE *in;
		FILE *out;

		int count = 0;
		int bytes = 0;

		if ((in = fopen(i, "rb")) == NULL)
		{
			return false;
		}
		if ((out = fopen(o, "wb")) == NULL)
		{
			return false;
		}
		int byte = 0;
		int passC = 0;
		while ((count = getc(in)) != EOF)
		{
			count = count ^ pass[passC++];
			bytes++;
			putc(count, out);
			if (pass[passC] == '\0')
			{
				passC = 0;
			}
		}

		fclose(out);
		fclose(in);
		
		ofstream DB(i, ios::out);
		DB.close();
		return true;
	}
Last edited on
Topic archived. No new replies allowed.