File encryption
Blitz3D Forums/Blitz3D Programming/File encryption
| ||
Now first off... I do not want to pack my files into one big and single archive. I wanted to know if there is a simple way to encrypt-decrypt single, separate player configuration files (for example... aircraft loadout files for custom aircraft setups the player creates) Some years ago I saw one somewhere that did exactly this, but I lost all trace of it... The reason I want the files not to belong to an archive is to allow users to easily move their configs-variants around (send them to other players for example) yet still not allowing them to crack-open them and edit variables they are not supposed to mess with. Been to the code archives section, people mention a few libraries over there but they are all "file packaging" libs apparently, and that's not what I'm looking for. My game is already handling a pak archive for the protected game media. |
| ||
A simple XORing of data written to the config file should be enough to prevent it to the extent that you'd need.pat% = %00110011010100100100101101001011 ;32-bit 'encryption key' - can be anything. x% = 800 y% = 600 ;Encrypt x = x XOR pat y = y XOR pat ;print results (in your code, you'd write ints etc to a file here) DebugLog "X:" + x DebugLog "Y:" + y DebugLog:X:861030507 DebugLog:Y:861030675 ;Decrypt x = x XOR pat y = y XOR pat ;print results DebugLog "X:" + x DebugLog "Y:" + y DebugLog:X:800 DebugLog:Y:600 note: The encryption process is identical to the decryption. The XOR simply toggles, if you like, between encrypted and non-encrypted. For this reason you must use the same bit pattern (pat%) throughout, otherwise you'll get in a right mess. |
| ||
and, if you want to encrypt a little more, you could use SeedRnd on the encripter and the decripter with the same "seed" to generate the same sequence of pseudorandomics PATTERNS, in this way as you Pattern is changing, the same value will be encripted diferently. but you (knowing the seed) can always decript simply xoring with the correct Pattern. note: xor only toggles the bit that has a corresponding 1 in the pattern, the ones with a correspondig 0 are not modified. if by example in your pattern has a byte 0, the byte xored with that will be equal to the original, so you could see some bytes encripted and some not. Try to have at least one 1 in each data encripted to has at least one bit toggled. Juan |
| ||
Here is a quick function that both Decrypts and Encrypts files based on a key (if you encrypted a file, you can decrypt it by applying the function to it again, using the same key):Function Enc_Dec_File(src_file$,dst_file$,key%) size = FileSize(src_file) coded_bank = CreateBank(size) source = ReadFile(src_file) ReadBytes coded_bank,source,0,size CloseFile(source) For offset = 0 To size-1 val = PeekByte(coded_bank,offset) Xor key PokeByte(coded_bank,offset,val) Next dest = WriteFile(dst_file) WriteBytes coded_bank,dest,0,size CloseFile(dest) FreeBank(coded_bank) End Function Based off of Gfk's marvellous Hole in One source code. To use the function, you'd do this: Enc_Dec_File("config.txt","config.xyz",12345) ;I used the source "config.txt" to provide the data, and the file "config.xyz" will be generated as an encrypted version. ;The function doesn't do any harm to the original file, it just reads data from it. To use the function again, when you want to decrypt the file: Enc_Dec_File("config.xyz","config.txt",12345) ;Be careful not to overwrite your original file. file = ReadFile("config.txt") [...] DeleteFile("config.txt") ;delete the clean file as soon as you're finished so that nobody can take it. You see, to decrypt a file you pass the encrypted version as the Source_File, and the output would have the same name as the original one (well, it's not really necessary. You could also use "temp" for example, so it doesn't attract much attention). I won't mention how important it is to use the same key! Make sure to read what the function does so you know what you're doing. It's really simple. It's amazing to see how it does it's magic. You get a .BMP file, encrypt it using some simple key and then you open it with the Notepad...and it's all gibberish (more than it was before, anyway). Good luck! |
| ||
and, if you want to encrypt a little more, you could use SeedRnd on the encripter and the decripter with the same "seed" to generate the same sequence of pseudorandomics PATTERNS, in this way as you Pattern is changing, the same value will be encripted diferently. but you (knowing the seed) can always decript simply xoring with the correct Pattern. There used to be a bug in Blitz3D's RNG, so that the results may not be the same on different computers, given the same seed. I don't know if the bug was fixed. If the bug wasn't fixed, then one should not use the built-in RNG for encryption purposes. |
| ||
There used to be a bug in Blitz3D's RNG, so that the results may not be the same on different computers, given the same seed. I don't know if the bug was fixed. I did a test (with a load of forum people) once that indicated this bug may not even have existed. All I was able to determine, was that Blitz3D gave different results to BlitzPlus despite running identical code.But yes, I'd avoid relying on SeedRnd because of lingering doubt over this issue. |
| ||
Thank you very much, I will try these out ASAP! :) |