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!
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?
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!
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:
string scores;
char Key = 'j';
ifstream user_score ("score.txt");
while (user_score.good())
{
getline (user_score,scores);
for (longlongint 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 (longlongint x = 0;x < Previous.size();x++)
{
Previous[x] ^= Key;
user_name << Previous[x];
}
for (longlongint x = 0;x < Name.size();x++)
{
Name[x] ^= Key;
user_name<< Name[x];
}
for (longlongint x = 0;x < scored.size();x++)
{
scored[x] ^=Key;
user_name << scored[x];
}
user_name << score;
for (longlongint 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 (longlongint x = 0;x < UserName.size();x++)
{
UserName[x] ^= Key;
high_score << UserName;
}
for (longlongint x = 0;x < Name.size();x++)
{
Name[x] ^= Key;
high_score << Name;
}
for (longlongint 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 (longlongint 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.