Encryption / decryption

Hi,

Question 1.
I understand how a 'key' or single ASCII character like the letter 'k' is used to encrypt text. For example, the binary code for:
A: 01000001
k: 01101011

Thus, the encrypted character for A, with the key 'k' would be '*' given its binary code: 00101010.

How does the XOR encryption work when you have more than one character as a key, for example key = "hi"?

Question 2:
I'm using the code below to read (line by line) a text file "test.txt" which encrypts each line before storing it into a vector. This vector is then saved as the text file "newtest.txt". The program then decrypts the encrypted file using the same method / key and save it as "decrpytednewtest.txt".

test.txt reads:
1
2
3
4
aaa
my details
My Details
MY DETAILS


decryptednewtest.txt reads:
1
2
3
4
5
6
7
8



my det
ils
My Det
ils
MY DETAILS


And my code:
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

string ecryptor_decryptor(string toEncryptDecrypt) {
    char myKey = 'k'; //Any char will work
    string encrypted_decrypted = toEncryptDecrypt;
    
    for (int i = 0; i < toEncryptDecrypt.size(); i++)
        encrypted_decrypted[i] = toEncryptDecrypt[i] ^ myKey;
    
    return encrypted_decrypted;
}

int main(int argc, const char * argv[])
{
	// Encrypt File...
	vector<string> profile;
	ifstream filein("test.txt");
	string temp;

	while(getline(filein,temp))
	{
		string encrypted = ecryptor_decryptor(temp);
		profile.push_back(encrypted);
	}
	filein.close();

	ofstream newfile("newtest.txt");
	for(int i = 0; i<(profile.size()); i++)
		newfile << profile[i] + "\n";
	newfile.close();
	profile.clear();


		// Decrypt File...
	vector<string> profile2;
	ifstream filein2("newtest.txt");
	string temp2;

	while(getline(filein2,temp2))
	{
		string decrypted = ecryptor_decryptor(temp2);
		profile2.push_back(decrypted);
	}
	filein2.close();

	ofstream newfile2("decryptednewtest.txt");
	for(int i = 0; i<(profile2.size()); i++)
		newfile2 << profile2[i] + "\n";
	newfile2.close();
	profile2.clear();
	system("PAUSE");
	return 0;
}


It seems that the key of 'k' converts the letter 'a' in the original text as a new line in the encrypted file but the decrypted file doesn't correct this...

How do I avoid this occuring in my decrpytednewtest.txt no matter what key I use?

Thanks,
You just need to open the file in binary mode and not ignore any characters.
See Duoas post here:
http://www.cplusplus.com/forum/general/11286/
Thanks for your reply, kevinkjt2000.

I'll have a look and go at what Duoas posted. My programming understandings is limited but improving ever so slightly.... So I'm up for a new challenge.

I've only ever written data to text files.

The data I am writing to text files will store some of the user's personal data but not too personal... A simple XOR encryption should be enough anyhow.


Is there an advantage to writing / opening in binary mode? Or does it depend on what you're writing and / or the amount of data that is being written??

I would be very appreciative if anyone is willing to give me a brief explanation on my first question above too ;)

Cheers,

Edit: By the way, using ios::binary with my ifstream and ofstream seems to have done the trick.... Thanks!
Last edited on
Topic archived. No new replies allowed.