Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Want to encrypt output files data?
Index -> Programming, Turing -> Turing Tutorials
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
MysticVegeta




PostPosted: Thu Mar 02, 2006 10:06 pm   Post subject: Want to encrypt output files data?

So, you are creating a game or an application eh?

Now, in order for you to recognize a user's score the second time he runs it, you need to know what his score before was! So here your program is, saving his scores into a text file.

Do you really know what the user can do =0?

If you use text file to save the settings/scores/names/etc (only way in turing to do so) and you write the scores in the text in the following way:

"Name: MMCC
Pass: 13375569
Score: 25
Level: 2 "

Say you do this, now the user closes the game, open the text file and does this:

"Name: MMCC
Pass: 13375569
Score: 100000000
Level: maxLevel"

The next time he rns the program , he automatically at the alst level with a nice high score. You want to make sure he doesn't know what it is you have in your text file.

Encryption to the rescue!

BASIC:

Ok, this is the really basic method to do and is easily crackable if used brain lol.

The idea is to change a letter into something else. For example: Using the QWERTY Encryption method, what happens is each letter shifts two to right:

q -> e
w -> r
e -> t

etc...

This program can we easily made in turing.. For numbers, recall that you are free to give each digit a character for, example, let 0 be a, 1 be b, 2 be c, etc. and then use that method. To decode you just have to go 2 characters left Razz

INTERMEDIATE: Intermediate encoding often requires using a "key" to encode/decode files. A good example would be S2 from CCC 2006, but if you don't know that problem, let me fill you in with another method.

Say you have this key:

"I HAVE ALWAYS HATED YOU BECAUSE YOU ARE UGLY." (key)

and there is always your main alphabets:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" (alphas)

And you want to decode this string: "AWT IS BAD" (encode)

Now a basic encoding follows like this:

code:
var ind : int

var key := "I HAVE ALWAYS HATED YOU BECAUSE YOU ARE UGLY."
var alphas := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var encode := "AWT IS BAD"
var encodedMessage := ""

for x : 1 .. length (encode)
    ind := index (key, encode (x))
    if ind > 0 then
        encodedMessage += alphas (ind)
    else
        encodedMessage += encode (x)
    end if
end for

put encodedMessage


So, now the AWT is bad encodes to: "DJQBAMBYDS"

Even the spaces get encoded, would someone ever be able to guess it? probably yes, around a million years. lol

Now how do I decode this string?????????????????? AHHH!
No need to AHHHHHH!!

We just go backwards now!
Find the encodedString's character index in the alpha stirng, and add the character at that location of key. For a more confusing,.. I mean... a better explanation, look at the code.

code:
var ind : int

var key := "I HAVE ALWAYS HATED YOU BECAUSE YOU ARE UGLY."
var alphas := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var encode := "AWT IS BAD"
var encodedMessage := ""
var decodedMessage := ""

for x : 1 .. length (encode)
    ind := index (key, encode (x))
    if ind > 0 then
        encodedMessage += alphas (ind)
    else
        encodedMessage += encode (x)
    end if
end for

put encodedMessage

for x : 1 .. length (encodedMessage)
    ind := index (alphas, encodedMessage (x))
    if ind > 0 then
        decodedMessage += key (ind)
    else
        decodedMessage += encodedMessage (x)
    end if
end for

put decodedMessage


Again, this is really basic intermediate level encoding because you are limited to using charaacters inside the key, also due to the doubling of charaacters in the key you wil have some errors, how do we fix it?

Lets just say, its not really that challenging Rolling Eyes Modify the damn key! lol.

So your key is now "I HAVE ALWAYS HATED YOU BECAUSE YOU ARE UGLY." nice key. eh? Lets complicate it Mwhahahhah Twisted Evil Lets make it ugly:

-> remove doubled characters (hint: use boolean arrays)
-> add characters that do not appear.
-> Sort it
-> reverse it

Sounds like a job for the computer eh?

Well the advantages of having that now are a lot, you now have access to however type of characters you want inside the message to encode since all characters are present inside the key.

Be more creative. Do something no one could guess.
As for advanced level, well lets just say I am not advanced in it, so people like zylum, cornflake, cervantes, bugz, wtd etc would give a far betterunderstanding of advanced level of cryptography.

Look up previous contest at www.dwite.org and you will find some problems and look at the pattern they use to encode/decode and be creative! Smile
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Thu Mar 02, 2006 10:14 pm   Post subject: (No subject)

Not a bad Tut Mystic. Have a look at some of the, uh, prettier tuts (eg Cervantes' Classes Tut) and try emulate the style. You've got a start with the Red text, but try bring the format up to that level.

+ bits.
batman




PostPosted: Fri Mar 03, 2006 4:33 pm   Post subject: Tutorial

Very nice tutorial I learned a lot. Thanks! Very Happy
md




PostPosted: Fri Mar 03, 2006 4:38 pm   Post subject: (No subject)

Looks good! But it's in no way secure Wink A Simple brute force would be able to crack all your methonds absurdly quickly. But then for something really simple it would work.

'Cept your last one. Following your directions hte key would end up being hte alphabet backwards (thanks to hte sorting), making it pretty simple too.
MysticVegeta




PostPosted: Fri Mar 03, 2006 8:55 pm   Post subject: (No subject)

Cornflake wrote:
Looks good! But it's in no way secure Wink A Simple brute force would be able to crack all your methonds absurdly quickly. But then for something really simple it would work.


Thats cause I left the advanced part to pros like you lol Laughing
[Gandalf]




PostPosted: Sat Mar 04, 2006 8:54 am   Post subject: (No subject)

Yep, nice tutorial.

Brute force? I'm not really knowledgable in that area, but does that not depend on how many letters it has to guess, checking most/all of the possibilities? So the solution would be to make whatever it has to guess longer, right? It wouldn't matter what encryption was used.
md




PostPosted: Sat Mar 04, 2006 10:24 am   Post subject: (No subject)

[Gandalf] wrote:
Yep, nice tutorial.

Brute force? I'm not really knowledgable in that area, but does that not depend on how many letters it has to guess, checking most/all of the possibilities? So the solution would be to make whatever it has to guess longer, right? It wouldn't matter what encryption was used.


Not really; you only need to decrypt a small amount of a message to test and see if you are using hte right method. By giving a larger message all you do is make it easier to attack the encryption with a statistical aproach.

The problem is that guessing which method was used is hard; whereas in cases where you know the method all you need to do is get the key. Getting a computer to decrypt the first few lines of a message with every possible key, and then return the ones that are statistically close to the original language the message was written in is pretty easy for a computer though.
[Gandalf]




PostPosted: Sat Mar 04, 2006 10:36 am   Post subject: (No subject)

Ah... True. What I said is more applicable to passwords, where the next letter can be anything and no 'key' was used.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Sat Mar 04, 2006 7:49 pm   Post subject: (No subject)

come on guys, someone add an advanced tut Smile
Booya




PostPosted: Sun Mar 05, 2006 7:06 pm   Post subject: Tutorial

Awesome Tutorial!
MysticVegeta




PostPosted: Tue Mar 07, 2006 10:50 am   Post subject: (No subject)

ok that by far is the 20th useless post for you. You seriously need to change your behaviour.
TheOneTrueGod




PostPosted: Sun Mar 19, 2006 7:46 pm   Post subject: Advanced encrypting.

I believe that one of the mods (I can't remember which one) posted a tutorial similar to this one somewhere, and I think thats where I got the idea for this from (Once again, I can't remember, it may have been somewhere else). Basically, you just make your own random number generation, and add that to the users high score. Then, to revert the changes, you just emulate the process again, using the starting seed again, but this time you subtract the value from the encrypted value. Heres some coding that will hopefully help make sense of things.

code:
function Scramble (var RandomSeed : int, NumberToBeScrambled : int) : int
    %Basically, this function emulates the random number generation used in
    %many programs.  It then returns the number you provided.
    RandomSeed *= 3284
    RandomSeed mod= 345762
    %We then add that randomly generated number to the number the user wants to be scrambled.
    result NumberToBeScrambled + RandomSeed
end Scramble

function DeScramble (var RandomSeed : int, NumberToBeDeScrambled : int) : int
    %Functions the same as above, but reverses it.
    RandomSeed *= 3284
    RandomSeed mod= 345762
    result NumberToBeDeScrambled - RandomSeed
end DeScramble

var RandomSeed : int := 245387 %This is the number not known by anyone but the programmer.
%This makes it so the file cannot be translated.
%If this number changes, the entire translation process does as well.
var ScrambledNum : array 1 .. 10 of int
View.Set ('text')

put "Scrambling"
for i : 1 .. 10
    ScrambledNum (i) := Scramble (RandomSeed, i)
    put i, " becomes ", ScrambledNum (i)
end for

RandomSeed := 245387 %We need to reset that random seed to its original value
%or else the retranslation process won't work.

put ""
put ""
put "Descrambling"
for i : 1 .. 10
    put ScrambledNum (i), " becomes ", DeScramble (RandomSeed, ScrambledNum (i))
end for



In theory, if the user of your program were to just open the datafile and start changing random numbers to REALLY high ones, it would still work, but thats where we get clever. You can use a similar random number generation system (Using a constant starting seed) to get a "random" number between, say, 1 and 100. You then multiply this number by a larger number, say, 34572. Now, when you save their high score to a datafile, you instead save (LargerNumber - (Randomly Skewed Number)). That way, if they enter extremely large numbers, it will translate into negative numbers in your game.

One more thing, is you can put in a "red herring" value. Basically, pick a line, say, the third line down in the datafile. Output a constant number to that line. Then, when you recieve the numbers from the datafile, if that number is not what it should be, you know they have edited their datafile. Now that you have this information, you can either put an informative "Don't mess with datafiles" to the screen, or you can mess with their head by giving them a score and level of zero Twisted Evil .

Anyways, I would love to give credit to whoever made the original tutorial on this kind of encryption, but I don't know who did it.

*Note: Any source code I posted here is my own: You can probably tell that by the crappy programming style Cool *
MysticVegeta




PostPosted: Sun Mar 19, 2006 9:59 pm   Post subject: (No subject)

Thats pretty good for encrypting numbers. But all we need to do now is make the RandomSeed actually a random # so that 5 doesn't always encrypt to x, but then you will ask how to decrypt it when it the RandomSeed is Random everytime when you run the program. The answer is simple. When generating the RandomSeed, after generating it save it to a file, so that next time when you open it you can access that RandomSeed by opening the file you saved before and then decrypting. Now thats what I call unlimited complexity Twisted Evil Twisted Evil
TheOneTrueGod




PostPosted: Sun Mar 19, 2006 11:00 pm   Post subject: (No subject)

Aye, excellent addition.
For those who are curious, (and did not know), you can generate "completely" random numbers by basing the seed on the time. Turing has a handy function that tells you the time elapsed since January 1st, 1970

code:

        var seconds : int
        wallclock ( seconds )
        put "The number of seconds since 1970 is ", seconds


(I copied that directly from the help file, with the exception of changing the seconds variable from a string to an int [The program directly from the help file doesn't work])
TokenHerbz




PostPosted: Sat Apr 01, 2006 7:45 pm   Post subject: (No subject)

I personally use just a basic encoder...

i made a .t file, and saved it as "CustomLibrary.t" where i put in all my fuctions which i made...

Here is an example of my basic encoder::

code:

%encrypt/decrypt
var Data_File : int %%to get your files


%%%%ENCRYPTION%%%%
%%Info:  Encrypts readable files

%%How to use this:
%-make a variable like (file_name_variable), string type, := "TheTextFile.txt"
%-make a variable like (encoding), string type.  this is to use the function
%%Syntax:  encoding := encrypt (file_name_variable)
fcn encrypt (var file_name : string) : string
    var data : string   %%gets all your data
    var amount : int

    open : Data_File, file_name, get
    get : Data_File, data : *   %%all your data is saved
    close : Data_File

    amount := length (data)
    %%encryption process
    var encode : string := ""   %%temperary data variable
    for i : 1 .. length (data)
        encode += chr (ord (data (i)) + 4)
    end for

    %%puts in the newly encrypted data
    open : Data_File, file_name, put
    put : Data_File, encode
    close : Data_File

    result encode   %%ends the function
end encrypt

%%%%DECRYPTION::::::
%%Info: Decrypts files encypted by this library.

%%%How to use this:
%Assiming you only use this with encryption you...
%dont need to add variables, just add syntax
%Syntax:  encoding := decrypt (file_name)

fcn decrypt (file_name : string) : string
    var data : string

    %%gets encrypted infomation
    open : Data_File, file_name, get
    get : Data_File, data : *
    close : Data_File

    %%decryption process
    var encode : string := ""   %%temperary data variable
    for i : 1 .. length (data)
        encode += chr (ord (data (i)) - 4)
    end for

    %%puts in the newly encrypted data
    open : Data_File, file_name, put
    put : Data_File, encode
    close : Data_File

    result encode     %%ends the function
end decrypt



O yeah, ofcourse you can get way more specific, but as its in my out .t, i use include, and WALA!, encodeing for every program i want, sweet eh?


+ bits to me???
Display posts from previous:   
   Index -> Programming, Turing -> Turing Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: