Encryption/Decryption

I needed some help in understanding how to encrypt and decrypt a file. I couldn't find any article on it and I am pretty much a beginner in programming, so I was hoping for a bit of help.
By the way, I want to know how to encrypt it because I have made a quiz program and I store the data into a .txt file. But the highscores can easily be tampered with since they are the basic most text. I just want it to be hard enough to prevent tampering. That is, the program will decrypt the data furing runtime, and will encrypt as soon it saves the data to the file.
Thanks in advance!
How basic?

EXTREME BASIC/AWESOME INTENSITY: http://cplusplus.com/forum/articles/38516/

Less basic, and still awesome: ultifinitus@gmail.com
Last edited on
I understood that part. Thanks for the article link.
I would like to know how to encrypt something like this while it is being sent to a file:
user_name << "The Previous user, " << Name << ", scored " << score << " points.";
Here, Name and score is a string and an float value respectively. Now I know that I can encrypt an string, but what about the regular text?
Now I know that I can encrypt an string, but what about the regular text?


What's the difference between a string and regular text?
Last edited on
by regular text I meant the one between the "" (like "the previous user"). As far as I understood, we can't perform operations on them. Hence my question!
Make it a string

string original = "The Previous user, ";

and then carry out your encryption operations on it.

The more technical term for "regular text" in your meaning is "string literal".
Thanks for both.
The suggestion and the correct term!!
I decided to take a more complex method, but one more effective in my opinion.
I transferred the data to one file in unencrypted form.
Then I retrieved it into a single string using getline. Then I created another file that will contain the encrypted version of the file. Here is the 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
ofstream user_name ("score.txt",ios::out|ios::trunc);
user_name << "The Previous user, " <<  Name << ", scored " << score << " points.";
user_name.close();
//
//Encryption - Previous
//

ofstream User_name ("PrevScore.txt",ios::out|ios::trunc);
for (int x = 0; x < scores.size();x++)
{
	scores[x] ^= Key;
	User_name << scores[x];
}
User_name.close();

string TheScore;
//
//Decryption - Previous
//

ifstream User_score ("PrevScore.txt");
while (User_score.good())	
{
	for (int x = 0; x < scores.size();x++)
	{
		getline (User_score,TheScore);
		TheScore[x] ^= Key;
		cout << TheScore[x];
	}
}
User_score.close();

But it just continues indefinately. Any help?
Last edited on
Help Please?

If it runs forever, it's usually because a loop is never finishing. That can be easily debugged. Stick a

cout << "SOMETHING TO IDENTIFY THE LOOP" << endl;

in every loop that might be running forever and then run the code again.
Last edited on
I found another error that would prevent the code from working any way. So I changed it. Now my newer one is:
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
string scores;
char Key = 'j';


ifstream user_score ("score.txt");
while (user_score.good())
{
	getline (user_score,scores);
	for (long long int y = 0;y < scores.size();y++)
	{
		scores[y] ^= Key;
		cout << scores[y];
	}
}
user_score.close();

//
//Derivation
//
		
//Strings
string Previous = "The Previous user, ";
string scored = ", scored ";
string points = " points.";

ofstream user_name ("score.txt",ios::out|ios::trunc);
for (long long int x = 0;x < Previous.size();x++)
{	
	Previous[x] ^= Key;
	user_name <<  Previous[x];
}
for (long long int x = 0;x < Name.size();x++)
{
	Name[x] ^= Key;
	user_name<<  Name[x];
}
for (long long int x = 0;x < scored.size();x++)
{
	scored[x] ^=Key;
	user_name << scored[x];
}
user_name << score;
for (long long int x = 0;x < points.size();x++)
{
	points[x] ^=Key;
	user_name << points[x];
}
user_name.close();
	
//
//All Scores
//
string highscores;
string UserName = "User Name -   ";
string Score = "		 Score -  " ;
//
//Ading data to High scores list
//
ofstream high_score ("high_score.txt",ios::out|ios::app);
for (long long int x = 0;x < UserName.size();x++)
{
	UserName[x] ^= Key;
	high_score <<  UserName;
}
for (long long int x = 0;x < Name.size();x++)
{
	Name[x] ^= Key;
	high_score << Name;
}
for (long long int x = 0;x < Score.size();x++)
{
	Score[x] ^= Key;
	high_score << Score;
}
	high_score << score;

high_score.close();

//
//Listing the Highscores
//

cout << endl << divider << "The Scores of previous users are: " << endl;

ifstream high_scores ("high_score.txt");
while (high_scores.good())
{
	getline (high_scores,highscores);
	for (long long int x = 0; x < highscores.size();x++)
	{
		highscores[x] ^= Key;
		cout <<  highscores << endl;
	}
}
user_score.close();

My newer problem is that the loop continues infinitely, but there is a continuous beep with it.
Now I know that the new problem is after line 59, because the code runs properly upto a breakpoint in the code set just above it.
This is somewhat what is present in the File:

?ser Name - ___ ?#ser Name - ____ ...........

here # is another encrypted symbol, the _____ are at some points blanks and at other characters etc. The ...... is to show that they extend to a very long line.

Any help?
Don't Bother. Found the error. It was in Line 63, 68 and 73.
I had forgotten to add [x] to them!!
Topic archived. No new replies allowed.