Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / String Encryption and Decryption

Author
Message
Sph!nx
15
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 4th Feb 2015 18:24 Edited at: 4th Feb 2015 18:30
Hey everybody,

I'm trying to write two functions. One to encrypt a string and one to decrypt a string with a randomiser with seed/key.

Now, I'm totally in the dark with this (Still learning). I have a function that works in DBP and tried to "translate" it to AppGameKit but I made a total mess of it.

This is the DBP code I tried to translate to AGK:


Any help is much appreciated!

Edit
After running some tests in DBP, I also noticed it does not work with extended characters, like "é" and "ä", which I do need..

Regards Sph!nx
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 4th Feb 2015 21:36
Do you want to hide the string, or just protect it from being edited?

The command Sha1() will let you protect your data.




Quidquid latine dictum sit, altum sonatur
Sph!nx
15
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 4th Feb 2015 22:36 Edited at: 4th Feb 2015 22:36
Thanks BatVink!

This is the encrypt string function I made of it:


Now how would I do the reverse?

Regards Sph!nx
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Feb 2015 10:13
Here is how I use it. Let's say I have a high score table, saved to a file like this:

Jane,61
Bob,57
Fred,43
Jimmy,39


I put all the details in a string:



Then I create the hash, and add it to the file as an extra line at the end:



When you read the scores back in, create MyString$ again in the same way. Read the Hash string in to a variable, and do the check:



Quidquid latine dictum sit, altum sonatur
Sph!nx
15
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 5th Feb 2015 15:08
Thanks a lot BatVink and I'm sorry that I was not very specific.

In your example, the application needs to know what it will receive, correct? I wish to create some sort of script files. Comprehensive data files that will determine game action with custom data, like conversations and object data. I do not believe that is possible with your idea.

Basically, I need a system that can encrypt any string and then decrypt it, without the encryption itself knowing the actual data.

Whenever a string (from an encrypted file) is read and decrypted in the game, the app will perform the right actions, based on what it had found in the string. I hope this makes any sense at all...

I appreciate your help very much though, but I'm afraid I need something more like the stuff I posted in the first post.

Regards Sph!nx
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Feb 2015 15:25 Edited at: 5th Feb 2015 15:34
Quote: "In your example, the application needs to know what it will receive, correct?"


No, it doesn't need to know. When you create the hash, it is an encrypted record of the data at the time you wrote it to the file. You can change the file contents all of the time (like a hiscore file) so long as you recreate the hash key each time you save the file.

When you read the file (script), you read the hash key that you saved with it. You then create the hash key again based on the data you read in, exactly as you did when you saved the file. Now you have the read-in hash key and the newly generated hash key. As long as they are the same, you know that nobody altered the script.

e.g create a file that contains:
The Quick Brown Fox
ABCD123



Read the file, it contains:
The Quick Brown Fox
ABCD123


Hash "The Quick Brown Fox" again and you get:
Hash Key = "ABCD123"
ABCD123 = ABCD123, Everything is OK

Read the file, it contains:
The Slow Blue Fox
ABCD123

Hash "The Slow Blue Fox" and you get:
Hash Key = "EFGH456"

ABCD123 != DEFG456
Therefore the file was edited.


I prefer this method so that I can still read my parameter files visually.
I also have a dummy hash key (e.g 99999). If the file contains the dummy key, I know that I have edited the file, skip the hash key check and rewrite the file with a genuine hash key in it.

I start the hash key line with "HSH" so I know this is the hash key line.

[EDIT][ By the way, I used to do almost the same as you, you'll find a tutorial about it in an old edition of the newsletter I wrote. I use Sha1() now because it is neater and more secure.

Quidquid latine dictum sit, altum sonatur
Sph!nx
15
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 5th Feb 2015 18:14 Edited at: 5th Feb 2015 19:03
Aha! I think I understand now. Every string in a file is readable, but contains a key that gets checked, correct?

If so, I think I can really use this. In fact, it would be better for testing and stuff.

Thanks a lot BatVink!

Edit
Adapted it to my needs and tested it with success!

You're the best!

Regards Sph!nx
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 5th Feb 2015 23:34
Glad it all worked for you

Quidquid latine dictum sit, altum sonatur

Login to post a reply

Server time is: 2024-04-20 08:42:54
Your offset time is: 2024-04-20 08:42:54