I'm going to xor encrypt some files for my C++ game. Right now, I want to read some text from a file, encrypt it and then save the encrypted information to the same file. And when I run the program again, it should decrypt the file and give the original content of the file.
string crypt(string input, char key)
{
string ret;
std::size_t size = input.size();
for(int i=0;i<size;i++)
{
ret += input ^ key+i;
}
return ret;
}
int main()
{
ifstream ifile;
ofstream ofile;
string content = "";
string filename = "fox.txt";
ifile.open(filename.c_str());
if (ifile.is_open())
{
while (!ifile.eof())
{
getline (ifile,content);
}
ifile.close();
ifile.clear();
}
cout << "content: " << content << endl;
cout << "content size: " << content.size() << endl;
content = crypt(content,1);
cout << "crypt: " << content << endl;
cout << "crypt size: " << content.size() << endl;
ofile.open(filename.c_str());
if (ofile.is_open())
{
ofile << content;
ofile.close();
}
return 0;
}
The problem is that, when I encrypt a simple sentence everything goes alright. But when I try to encrypt the file which I am using in my game, it plays a lot of beep sounds and the total amount of characters in the file are decreased. I think the beep sound might come from the bell character, but I'm not sure.
When I encrypt "The quick brown fox jumps over the lazy dog.", I get "Ujf$tsnkb*i~bya0w}k4czhj:tjxl?TIGHD\^MEL" and vice versa.
But when I encrypt:
Quote:02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 02 02 00 00 00 00 00 00 00 00 02 02 00 00 00 02 02 00 00 00 02 03 00 00 00 00 00 00 00 00 04 02 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 02 01 00 00 00 00 00 00 00 00 03 02 00 00 00 02 02 00 00 00 02 02 00 00 00 00 00 00 00 00 02 02 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02 02
It gives:
Quote:10#47&7:):9,=</ #2#&5&%8)(;,/>/?
pqbstevwhyzk|}n`qbctefwhizkl}no@QRCTUFWXIZ[L]^O@ARCDUFEXIH[LM^O°¡²³¤µ¶§¸¹ª»¼¾¿°¡¢³¤¥¶§¨¹ª«¼®¿‘‚“”…–—ˆ™š‹œŽŸ€‘‚”…„—ˆ‰š‹ŒŽàñðãô÷æ÷øéúûìýþïàáòãäõæçøéêûìíþïÒÁÒÑÄÕÖÇØÙÊÛÜÍÞÝÐÁÀÓÄÅÖÇÈÙÊËÜÍÌß02"34%67(9:+<=.? 1"#4%&7():+(=.- ?
parsduvgxyj{|m~pabsdevghyjk|mnPQBSTEVUHYXK\]N_@QBCTEFWHIZKL]NO ±²£´µ¦·¸©º»¬½¾¯ ¡²£¤µ¦§¸©ª»¬¾¯’‘„•”‡˜™Š›œžŸ‚“„…–‡ˆ™Š‹œŽŸðñâóôåö÷èùúëüýîÿàñâãôåæ÷èéúëìýîíÀÑÐÃÔÕÆ×ØÉÚÛÌÝÞÏÀÁÒÃÄÕÆÇØÉÊÛÌÍÞÏ0!23$56'89*;<->?0!"3$%6'(9*)<-,?? ? `qrctufwxiz{l}~o`arcdufexih{lm~oPARSDUTGXXJ[\M^_PABSDEVGHYJK\MN_°±¢³·¥¶µ¨¹º«¼½®¿ ±¢¡´¥¤·¨©º«¬½®¯€‘ƒ”—†—˜‰š›Œž€’ƒ„•†‡˜‰Š›ŒžòáòñäõöçøùêûüíþýðáàóäåöçèùêëüíîÿÐÑÂÓÔÅÖ×ÈÙÚËÜÝÎßÀÑÂÃÔÅÆ×ÈÉÚËÌÝÎÏ 12#45&78):9,=</ !2#$5&'8)*;,->/
pqbstevuhyxk|nbqbatedwhkzkn}nm@QPCTWFWZIZYL]\O@CRCFUFEXIH[LO^O²¡²±¤
And when I try to decrypt it again, it gives:
Quote:vvjz~nzzfvjznnzjvfzzn~zjvvZJN^JJfvzj~~jzvfjj~njzvvjz~nzxfv
öæúúîþúêööúêîþêêæöúêþþêúöæÊÊÞÎÊÚööêúþîúúæöêúîîúêöæúøîþøêöö
fvxj~|jzvfjj~njzvvjz~nzzfvJZNNZJtfzxn~zjvvzjn~jhfvxj~~jzvf
öõêúþîúúæöêúîîúêöæúúîþúêööÚÊÊÞÊÈæöúêþþêúöæ
Which certainly isn't the same as it started with.
I think the problem has something to do with the saving, loading or maybe the encoding as the first sentence works.
Sorry for the long post, but I felt all this information was necessary. Thanks for helping.