MODULE: EncryptedFile v0.9b

Started by HeirOfNorton, Mon 17/10/2005 05:28:56

Previous topic - Next topic

HeirOfNorton

Hey folks, I've got yet another script module for your consumption.  Are any of you making a game using the File I/O functions, perhaps for a player-generated character, or some other use? Were you perhaps worried that some unscrupulous player could hack the file and change the character's stats on a whim? Or perhaps you are using text files for some of the game's content, but don't want players to be able to change what's been written? Well, worry no more!  ;D

Ahem, anyway, this module adds an EncryptedFile class that is intended to be a drop-in replacement for the standard File objects already used. It uses a simple (but nearly impossible to break) Exclusive-Or encryption to secure your external files, so no one can read them but you. It also includes a small EXE for encrypting plain text/binary files so they can be read by the program (source code included).

Download here (Requires AGS v2.71!) (Thanks Neole!)
Mirror 1 (Thanks Colxfile!)
Mirror 2 (Thanks Candle!)

Please note that this is still a beta version (I'd love to hear about any bugs/issues/suggestions for the sucker), and that it requires AGS 2.71 or higher.

I hope you find it useful.
HoN

Ubel

#1
Interesting. Does this include video files too?

Kweepa

Quote from: HeirOfNorton on Mon 17/10/2005 05:28:56
It uses a simple (but nearly impossible to break) Exclusive-Or encryption to secure your external files, so no one can read them but you.

Heh. I remember Comanche Maximum Overkill's data files used XOR encryption with a 32 bit key (64 bit for the sequel). That was easy to crack, because the data was so regular.

Nice idea for a module!
Still waiting for Purity of the Surf II

SSH

#3
Indeed, people who know about cryptography never call anything "nearly impossible" to break. Every encryption can be broken by brute force attacks, it's just that good ones would require 1000 years or so of supercomputer time to do so, by which time you probably no longer care if it is broken. DES used to be resonabley secure, then triple DES, but now even AES is easily cracked.
12

HeirOfNorton

#4
Quote from: SSH on Mon 17/10/2005 10:29:15
Indeed, people who know about cryptography never call anything "nearly impossible" to break.

Yeah, I know, but I'm working on my salesman skills.   ;)
Obviously nothing this simple could be that hard to crack with a concerted effort, but it should be enough to deter any Joe Gameplayer with a hex editor who wants to give himself 10,000 experience points.

Quote from: Pablo on Mon 17/10/2005 05:37:37
Does this include video files too?

Well, the included encrypter WILL encrypt video files, but said encrypter would have to be used to decrypt them as well. They would not be usable in a game.

Ubel

I've been trying to use this module but there's this little problem that prevents me from doing so. It doesn't work. Whenever I type EncryptedFile. in the script editor, the autocomplete doesn't show me the functions from the struct that is defined in the module's script header, even though it should. And if I manually try typing the function it doesn't work either.

I believe there's something wrong in how the struct is defined but I just can't find the flaw even though I've searched through the whole script module. I guess my scripting skills are not quite enough for understanding the whole thing. Is anyone else encountering these same problems or is it just me for some weird reason? :-\

monkey0506

The flaw* that you seem to have noticed is that EncryptedFile isn't a static struct! Try something like this:

Code: ags
EncryptedFile ef;
ef.Open("file.enc", eFileWrite, "EncryptKey");
if (ef.GetFileHandle() == null) return;
ef.WriteString("this is some text!");
ef.Close();


*It's not actually a flaw. HON made it this way by design.

Ubel

Oh my, I hadn't realised that! Thank you very much monkey, and sorry for my noobness. :D

Dualnames

I'm probably be called a newbie but what the heck, well I checked your module. I read all the txt file. And managed to encrypt a video of me using the keyword "deadaswell" and the file from shoryuken.wmv was made to shoryuken.scbr(just an extension I came up with).
  Probably from what I've read I can't encrypt videos. Can I? Please write an example code. Thank you.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Rui 'Trovatore' Pires

QuoteDoes this include video files too?


Well, the included encrypter WILL encrypt video files, but said encrypter would have to be used to decrypt them as well. They would not be usable in a game.

So, you can't decrypt them in-game, so you can't encrypt videos for use in a game.
Reach for the moon. Even if you miss, you'll land among the stars.

Kneel. Now.

Never throw chicken at a Leprechaun.

monkey0506

#10
Would it be possible to do something like this (which would create a trash file, but might provide a solution)?

Code: ags
// encrypt the video prior to distribution...
EncryptedFile encVideo;
encVideo.Open("shoryuken.scbr", eFileWrite, "deadaswell");
File* video = File.Open("shoryuken.wmv", eFileRead);
if ((video == null) || (encVideo.GetFileHandle() == null)) {
  Display("Error encrypting video!");
  return;
  }
while (!video.EOF) {
  encVideo.WriteRawLine(video.ReadRawLineBack());
  }
encVideo.Close();
video.Close();
// delete the non-encrypted video file prior to distribution ;)

// decrypt the file for in-game use
EncryptedFile encVideo;
encVideo.Open("shoryuken.scbr", eFileRead, "deadaswell");
File* video = File.Open("shoryuken.wmv", eFileWrite);
if ((video == null) || (encVideo.GetFileHandle() == null)) {
  Display("Error decrypting video!");
  return;
  }
while (!encVideo.EOF()) {
  video.WriteRawLine(encVideo.ReadRawLine());
  }
encVideo.Close();
video.Close();
// play the video back
PlayVideo("shoryuken.wmv", 0, 0);
// but then destroy the video file afterwards (not the encrypted copy!)
File* video = File.Open("shoryuken.wmv", eFileWrite);
if (video == null) return;
video.WriteString("killme"); // clearly not valid WMV data ;)


Wouldn't something like that work???

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

monkey0506

#12
Actually now I'm not sure that it would work....perhaps it's just me, but in my tests it seems that the encryption isn't working very well with videos. My encrypted video-file is loaded with instances of the key in plain text, and when I try to decrypt the video, these instances remain, invalidating the video data.

I'm looking into the cause...but...don't get your hopes up too high if your results are the same.

P.S. If anyone knows about the bitwise XOR operator, it appears to be the result of a character (A) that when XORed with a character taken from the key (B) results in B, provided that B is non-zero. I'm not entirely clear what circumstances this would occur in, but if it's of any significance, when opening the video in WordPad it seems the culprits are these strange box characters...which I can't copy and paste.

Dualnames

I know about the XOR operator.

A    B     Y
0    0     0
0    1     1
1    0     1
1    1     0

When A and B are different Y equals to 1.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

monkey0506

I know basically how XOR works. I'm just confused regarding the results that I'm seeing...which are apparently:

A ^ B = B where B != 0

So...every bit in A is 0...meaning that the resulting bits will all be the same as the bits in B? So then the reverse operation (i.e., (A ^ B) ^ B) wouldn't work?

Hmm...I just tested it and it seems that the reverse operation would work. I'll look into the module code to try and see if this is possibly a bug.

P.S. Decrypting the video in-game may possibly be a slow operation depending on the length of the video. Just something to keep in mind.

Monsieur OUXX

#15
Quote from: monkey_05_06 on Sat 07/07/2007 04:39:46
A ^ B = B where B != 0 would mean that every bit in A is 0

...or that A = B! Maybe you forgot this case?

I thought of OR instead of XOR... Sorry
 

Dualnames

Monkey I'd like to inform you that I tried to encrypt a video file and then decrypt it.
You said that there might be a slowdown while encrypting and decrypting, and it was. And a very big one. The file I made experiments with was a wmv size 45 kb and bit rate 105kb. So if this causes slowdowns imagine what a 1mb file would do. Anyway, if you want any help for betatesting this module trying to fix the slowdown in some way, feel free to PM me.
Thanks again.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Gilbert

I think the problem is with slow file access of the AGS file funtions.
To en/decrypt a file the module actually (I think) writes to a new file byte by byte, which would be VERY slow in general (as file functions are intended for small files like stats in QFG type games, etc. only), I think there isn't any way to "fix" those slowdowns, so it's never recommended to encrypt video files in an AGS game. Unless of cause, someone makes a plugin instead to handle the process, which would just kick Linux and Mac users away (I think it doesn't matter anyway, AFAIK the Linux port doesn't suppport video playback, not sure about the Mac port).

monkey0506

RTFM! ;D

Yes, video playback works on Mac. Regarding the decryption, it has to read in the file one byte at a time, decrypt the byte, write the byte to the new file, rinse and repeat. If it would be possible to run the decryption over a more extensive period of time, instead of just in a single loop it might be possible to decrypt the file while other game events are run. Not sure how you might want to handle that...but maybe it will give you an idea you could use.

As for me, I've been looking into this module, and I think I may have found an un-related issue with the method used for appending to files. I'm not 100% sure, but I think there might be a bug there. But for now I have to go to..."work". Which mostly involves me sitting on a bench out in the hot 90 - 100 F weather. When I get home I may have a chance to look at it, but I have to go to work early tomorrow. I'll be off Thursday, and don't have to work Friday until 2 PM (GMT -6:00)...so I'll try to keep you all posted on my findings.

Monsieur OUXX

Quote from: monkey_05_06 on Tue 10/07/2007 16:33:48
If it would be possible to run the decryption over a more extensive period of time, instead of just in a single loop it might be possible to decrypt the file while other game events are run. Not sure how you might want to handle that...

Good idea; It's usually how slow processings are synchronized with fast processings within a program.

It could be done this way : the programer could create a list of videos that are likely "about" to be viewed (for example, he knows that most likely the intro sequence will be played if the user clicks on "New game"). This list would be created as soon as possible (at game start) and updated as often as necessary.

Then in each game loop (repeatedly_execute_always) a given number of bytes from the next video in this list would be decrypted. When it gets entirely decrypted, the engine begins to decrypt the next video in our list.

In our example, that means that the few seconds after the game starts and before the player clicks on "New Game" (even if it's only 4 seconds) would be used to begin decryption.

 

SMF spam blocked by CleanTalk