Computer Science Canada

Encryption module

Author:  Geniis [ Mon Nov 16, 2009 9:11 pm ]
Post subject:  Encryption module

This is just a simple little module i coded named Crypto that can encrypt and decrypt files. Its easy to use, its explained in the readme. Just use Crypto.EncriptFile to encrypt a file and Crypto.DecryptFile to decrypt a file (i recommend only decrypting files encrypted with Crypto). There is also a way to change the keys used. Leave feed back if you fell like it, or if you find bugs... i might release a better version of it later on.

Author:  SNIPERDUDE [ Tue Nov 17, 2009 1:25 pm ]
Post subject:  RE:Encryption module

Great job

+ bits

Author:  mirhagk [ Tue Dec 15, 2009 12:03 am ]
Post subject:  Re: Encryption module

This reminded me of an earlier encryption program I created, it uses a key, and you can create keys for it (that way even if someone has the program they can't do very much cuz need the exact key)

Author:  yazdmich [ Thu Dec 06, 2012 10:04 am ]
Post subject:  Re: Encryption module

mirhagk @ Tue Dec 15, 2009 12:03 am wrote:
This reminded me of an earlier encryption program I created, it uses a key, and you can create keys for it (that way even if someone has the program they can't do very much cuz need the exact key)


I edited your program as a module and added a few features.

Author:  mirhagk [ Thu Dec 06, 2012 5:46 pm ]
Post subject:  RE:Encryption module

Ahh code from my very early days of Turing, it makes me realize just how much I've learned over the past 3 years.

I'd suggest allowing the programmer pass in file names in the module's functions, so that it can truly be used as a module. I would then suggest using a better encryption algorithm, a one time pad would actually be pretty good for this scenario, is easy to implement, and is actually the most secure algorithm (so long as it's used right)

Author:  yazdmich [ Thu Dec 06, 2012 6:32 pm ]
Post subject:  Re: RE:Encryption module

mirhagk @ Thu Dec 06, 2012 5:46 pm wrote:
Ahh code from my very early days of Turing, it makes me realize just how much I've learned over the past 3 years.

I'd suggest allowing the programmer pass in file names in the module's functions, so that it can truly be used as a module. I would then suggest using a better encryption algorithm, a one time pad would actually be pretty good for this scenario, is easy to implement, and is actually the most secure algorithm (so long as it's used right)


2 things:

I thought I did allow for variable pass-through?

I'm actually relatively new to Turing (and programming in general) so I'm not quite sure how to implement a one-time pad cipher

Turing:

% KEY-BASED ENCRYPTION MODULE BASED ON http://compsci.ca/v3/download.php?id=7033
% MODIFIED AS A MODULE WITH KEY GENERATOR INCLUDED IN MAIN CODE


unit
module Crypto

    export GetKey, In, Out, key, GenKey, Decoded, Encoded

    var ind : int
    var key := ""
    var encode := ""
    var msg : string
    var msgout : string
    var Encoded := ""
    var Decoded := ""
    var stream : int
    var num : int
    var cha : string (1)
    var name : string
    var alphas := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.?\"'()[]{}1234567890<>;!@#$%^&*`~/\\-=_+ "
    var cha1 : array 1 .. 92 of string
    var used : array 1 .. 92 of boolean

    proc GetKey
        put "Please enter the name of the key you are using (key must be in key folder)"
        get name
        open : stream, "keys/" + name, read
        if stream > 0 then
            read : stream, key
            put "Key: ", key
        else
            put "Error, key not found, make sure its in the key folder and you spelled it right"
            quit
        end if
        close : stream
    end GetKey

    proc GetKeyInternal (name1 : string)
        open : stream, "keys/" + name1, read
        if stream > 0 then
            read : stream, key
        else
            put "Error, key not found, make sure its in the key folder and you spelled it right"
            quit
        end if
        close : stream
    end GetKeyInternal

    proc In (msg, name : string)
        GetKeyInternal (name)
        encode := msg
        Encoded := ""
        for x : 1 .. length (encode)
            ind := index (key, encode (x))
            if ind > 0 then
                Encoded += alphas (ind)
            else
                Encoded += encode (x)
            end if
        end for
        msgout := ""
    end In

    proc Out (msg, name : string)
        GetKeyInternal (name)
        Encoded := msg
        Decoded := ""
        for x : 1 .. length (Encoded)
            ind := index (alphas, Encoded (x))
            if ind > 0 then
                Decoded += key (ind)
            else
                Decoded += Encoded (x)
            end if
        end for
        msgout := ""
    end Out

    proc GenKey
        for i : 1 .. 92
            used (i) := false
        end for
        for i : 1 .. 92
            cha1 (i) := alphas (i)
        end for
        for i : 1 .. 92
            loop
                randint (num, 1, 92)
                exit when used (num) = false
            end loop
            used (num) := true
            key += (alphas (num))
        end for
        put "New key: " ..
        put key
        put "Please enter a name for this key: " ..
        get name
        open : stream, "keys/" + name, write
        write : stream, key
        close : stream
    end GenKey

end Crypto



Author:  mirhagk [ Thu Dec 06, 2012 11:54 pm ]
Post subject:  RE:Encryption module

Ah I only really looked at the GenKey method, the only one where variables aren't passed through lol.

A one time pad basically generates a really large list of random characters, and uses this as the key (the key must be as long as the message in order to be perfectly secure).

You can then go through and use some method to encrypt the message in such a way that you can reverse it. A very common method is to use the XOR operator, which basically works by comparing the bits of each number and puts a 1 every time they are different, and 0 every time they are the same, eg:
110001 message
101010 key
=====
011011 result
Then you can XOR the result with one of the values to get the other one:
011011 result
101010 key
=====
110001 message

Author:  yazdmich [ Fri Dec 07, 2012 9:03 am ]
Post subject:  Re: RE:Encryption module

mirhagk @ Thu Dec 06, 2012 11:54 pm wrote:
A one time pad basically generates a really large list of random characters, and uses this as the key (the key must be as long as the message in order to be perfectly secure).

I'm guessing 255 chars long would be best, so that any line passed through can be encrypted. Also, should it be completely random (with repeated characters)?

Author:  AntoxicatedDevil78 [ Fri Dec 07, 2012 10:29 am ]
Post subject:  RE:Encryption module

Amazing!! Smile

Author:  mirhagk [ Fri Dec 07, 2012 12:18 pm ]
Post subject:  RE:Encryption module

Yep completely random is best. So long as you use the exact same key it will decrypt and encrypt fine. Technically you should have perfect randomness (random in turing isn't fully random), but the concept is the same.

Author:  yazdmich [ Fri Dec 07, 2012 1:50 pm ]
Post subject:  Re: RE:Encryption module

Is this good for the key generator?

Turing:

    var ind : int
    var key : string := ""
    var encode := ""
    var msg : string
    var msgout : string
    var Encoded := ""
    var Decoded := ""
    var stream : int
    var num : int
    var name : string
    var alphas := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.?\"'()[]{}1234567890<>;!@#$%^&*`~/\\-=_+ "
    var cha, ind1 : array 1 .. 92 of string

    proc GenKey
        for i : 1 .. 92
            cha (i) := alphas (i)
        end for
        for i : 1 .. 255
            randint (num, 1, 92)
            key := key + cha (num)
        end for
        put "New key: " ..
        put key
        put "Please enter a name for this key: " ..
        get name
        open : stream, "keys/" + name, write
        write : stream, key
        close : stream
    end GenKey

Author:  mirhagk [ Fri Dec 07, 2012 2:56 pm ]
Post subject:  RE:Encryption module

Yeah that will generate a long enough key (again it's not perfectly secure, but you can't make it perfect without true random numbers).

Just curious, is there any reason for using cha instead of just using alphas?


: