Jibble

Author Topic: MODULE: EncryptedFile v0.9b  (Read 10034 times)

MODULE: EncryptedFile v0.9b
« on: 17 Oct 2005, 05:28 »
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
« Last Edit: 19 Mar 2007, 21:33 by strazer »

Ubel

Re: MODULE EncryptedFile
« Reply #1 on: 17 Oct 2005, 05:37 »
Interesting. Does this include video files too?
« Last Edit: 19 Apr 2006, 23:50 by strazer »

Kweepa

  • Mutated Guano Deviser
    • Best Innovation Award Winner 2009, for his modules and plugins
    • Kweepa worked on one or more games that won an AGS Award!
    •  
    • Kweepa worked on one or more games that was nominated for an AGS Award!
Re: MODULE EncryptedFile
« Reply #2 on: 17 Oct 2005, 07:10 »
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

  • Flying round the world at the speed of haggis
    • I can help with scripting
    • SSH worked on one or more games that won an AGS Award!
    •  
    • SSH worked on one or more games that was nominated for an AGS Award!
Re: MODULE EncryptedFile
« Reply #3 on: 17 Oct 2005, 10:29 »
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.
« Last Edit: 19 Apr 2006, 23:51 by strazer »

Re: MODULE EncryptedFile
« Reply #4 on: 17 Oct 2005, 14:09 »
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.

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.
« Last Edit: 19 Apr 2006, 23:50 by strazer »

Ubel

Re: MODULE: EncryptedFile v0.9b
« Reply #5 on: 12 Mar 2007, 18:43 »
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

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #6 on: 13 Mar 2007, 15:43 »
The flaw* that you seem to have noticed is that EncryptedFile isn't a static struct! Try something like this:

Code: [Select]
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

Re: MODULE: EncryptedFile v0.9b
« Reply #7 on: 13 Mar 2007, 16:26 »
Oh my, I hadn't realised that! Thank you very much monkey, and sorry for my noobness. :D

Dualnames

  • Local Moderator
  • Rottwheelers
  • Pretty Badass
    • Dualnames worked on one or more games that won an AGS Award!
    •  
    • Dualnames worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #8 on: 05 Jul 2007, 20:50 »
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.
No more military army stuff. I'm alive and back.

Rui 'Trovatore' Pires

  • Lunge da lei per me non v'ha diletto!
    • I can help with AGS tutoring
    • I can help with play testing
    • I can help with proof reading
    • I can help with scripting
    • I can help with story design
    • I can help with translating
    • I can help with voice acting
Re: MODULE: EncryptedFile v0.9b
« Reply #9 on: 05 Jul 2007, 22:37 »
Quote
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.

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

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #10 on: 05 Jul 2007, 23:41 »
Would it be possible to do something like this (which would create a trash file, but might provide a solution)?

Code: [Select]
// 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???
« Last Edit: 06 Jul 2007, 00:02 by monkey_05_06 »

Dualnames

  • Local Moderator
  • Rottwheelers
  • Pretty Badass
    • Dualnames worked on one or more games that won an AGS Award!
    •  
    • Dualnames worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #11 on: 05 Jul 2007, 23:50 »
Awesome . Thanks Monkey.
No more military army stuff. I'm alive and back.

monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #12 on: 06 Jul 2007, 00:26 »
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.
« Last Edit: 06 Jul 2007, 00:33 by monkey_05_06 »

Dualnames

  • Local Moderator
  • Rottwheelers
  • Pretty Badass
    • Dualnames worked on one or more games that won an AGS Award!
    •  
    • Dualnames worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #13 on: 07 Jul 2007, 03:05 »
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.
No more military army stuff. I'm alive and back.

monkey0506

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #14 on: 07 Jul 2007, 04:39 »
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

  • Mittens Vassal
  • Cavefish
  • Mittens Half Initiate
    • I can help with proof reading
    • I can help with translating
    • I can help with voice acting
    • Monsieur OUXX worked on one or more games that won an AGS Award!
    •  
    • Monsieur OUXX worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #15 on: 07 Jul 2007, 17:55 »
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
« Last Edit: 07 Jul 2007, 17:59 by Monsieur OUXX »
 

Dualnames

  • Local Moderator
  • Rottwheelers
  • Pretty Badass
    • Dualnames worked on one or more games that won an AGS Award!
    •  
    • Dualnames worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #16 on: 10 Jul 2007, 09:37 »
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.
No more military army stuff. I'm alive and back.

Gilbert

  • Local Moderator
  • * KILL* * KILL * * KILL *
    • Lifetime Achievement Award Winner
Re: MODULE: EncryptedFile v0.9b
« Reply #17 on: 10 Jul 2007, 10:13 »
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

  • SEND PIZZA.
    • Best Innovation Award Winner 2017, for his work to help AGS games reach the widest possible audience - through popular distribution platforms (Steam, Galaxy) as well as other operating systems (Android, Linux)
    • monkey0506 worked on one or more games that won an AGS Award!
    •  
    • monkey0506 worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #18 on: 10 Jul 2007, 16:33 »
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

  • Mittens Vassal
  • Cavefish
  • Mittens Half Initiate
    • I can help with proof reading
    • I can help with translating
    • I can help with voice acting
    • Monsieur OUXX worked on one or more games that won an AGS Award!
    •  
    • Monsieur OUXX worked on one or more games that was nominated for an AGS Award!
Re: MODULE: EncryptedFile v0.9b
« Reply #19 on: 10 Jul 2007, 21:22 »
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.