encryption

I want to be able to encrypt my save files so that no one except for those whom have the extractor are able to read it.

the way i am saving the files is...


ofstream myfile ("note.bin");
if (myfile.is_open())
{
myfile << notes;
myfile.close();
}

so im wondering is, is there a way to save the data so that when opened with notepad it shows up with symbols and numbers?

and if there is a tutorial on it thats even better..

thanks in advance.
So you need an encryption algorithm.

Google for things like DES, Triple DES, Blowfish, etc.

Or just "encryption algorithm"
im not looking for programs that do the encryption i want to include the encryption in my code... if there is a way to reassign values to letters so that a=8 or something like that would be nice as well.
Ok, I thought about this more after I went to bed last night.

If you are serious about encryption and need it to obfuscate sensitive data, then you must use a published algorithm for security reasons, because anything you invent will be trivially hackable.

On the other hand, if you are using encryption because you are making a game that saves data to disk and you don't want the player to cheat by modifying the data with an editor, then I suggest using a hash instead of encryption. (Gamers are clever and will defeat any encryption algorithm or hash algorithm you use anyway, so why spend the effort?)

Or, if you are wanting to use encryption because of the novelty factor (it 'looks cool' -- which is a valid reason, IMHO), then I suggest a very simple reversible function that works at the character level. here's some examples:

1
2
3
4
5
6
7
8
9
// Rotate characters 13 positions - A becomes M, B becomes N, etc.
char rot13( char x ) {
    return ( x - 'A' + 13 ) % 26 + 'A';
}

// Reverses the most significant 4-bits and the least significant 4-bits:
char nibble_swap( char x ) {
    return x >> 4 | ( x << 4 );
}



Other ways are to make a string of random bytes, then use an XOR.
For example, given the random bytes "12345" and the input string "Hello World", you would form the encrypted string by XORing 1 and H, 2 and e, 3 and l, 4 and l, 5 and o, 1 and (space), 2 and 'W', etc.

Keep in mind that all of the above algorithms are hardly secure; they are trivially hackable.
i thought that i might convert evrything to a binary but i am unable to do this i have tried and it still is not saving the data as binary heres my code...

#include <iomanip>
#include <conio.h>
#include <iostream>
#include <windows.h>
#include <fstream>
#include <cctype>
#include <math.h>
#include <string.h>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;

#include <helper.h>




int main()
{
randomize();


string notes, cmd;



getline(cin, notes);
do
{



getline(cin, cmd);


if (cmd=="save")
{
ofstream myfile ("note.s", ios::in | ios::binary | ios::ate);
if (myfile.is_open())
{
myfile << notes;
myfile.close();


}

}




if (cmd=="quit")
{
return (0);
}
}
while (1);



this code is working but not saving as binary.
Last edited on
It's working; you aren't understanding the difference between binary and text mode.

The character 'A' (ASCII value 65d or 0x41) is both text and binary at the same time.

Text mode just means the stream "interprets" some characters specially and translates them. Binary mode turns off the interpretation.
okay, i also noticed that i put the wrong file extension on for the save ot make it binary but thats okay.

on another question is there a way (im sorry if i am repeating this question)
to assign the value of an ascii character to something else like
a=1
b=2
c=3
but abc are the characters the user put into the program.
Topic archived. No new replies allowed.