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.