Computer Science Canada

Simple encryption

Author:  Raknarg [ Thu Sep 12, 2013 1:03 pm ]
Post subject:  Simple encryption

For anyone interested in how encryption works, this is one simple method I discovered recently. This is a program that I made to encrypt strings with a key code, and can basically only be unlocked with that key. Without it, the message is a jumbled mess.

Turing:

const dictionary := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
const numbers := "1234567890"

fcn encrypt (str, key : string) : string
    var news := ""
    var s := Str.Upper (str)
    for i : 1 .. length (s)
        if index (dictionary, s (i)) > 0 then
            news += chr (65 + (((ord (s (i)) - 65) + (ord (key (i mod length (key) + 1)) - 65)) mod 26))
        elsif index (numbers, s (i)) > 0 then
            news += chr (49 + (((ord (s (i)) - 49) + (ord (key (i mod length (key) + 1)) - 49)) mod 10))
        else
            news += s (i)
        end if
    end for
    result news
end encrypt

fcn decrypt (str, key : string) : string
    var news := ""
    var s := Str.Upper (str)
    for i : 1 .. length (s)
        if index (dictionary, s (i)) > 0 then
            news += chr (65 + (((ord (s (i)) - 65) - (ord (key (i mod length (key) + 1)) - 65)) mod 26))
        elsif index (numbers, s (i)) > 0 then
            news += chr (49 + (((ord (s (i)) - 49) - (ord (key (i mod length (key) + 1)) - 49)) mod 10))
        else
            news += s (i)
        end if
    end for
    result news
end decrypt

var input, message : string

put "Input message to encrypt: " ..
get message : *
put "Input your password: " ..
get input : *

message := encrypt (message, input)

cls

loop
    put "Your encrypted message: ", message
    put "Input the password to display the message: " ..
    get input
    put "The message with this key reads: ", decrypt (message, input)
    put "Press any key to continue..."
    loop
        exit when hasch
    end loop
    cls
end loop


Basically, it takes the string you input, then adds the value of your key in the same position to that letter. if you don't know what ord and chr do, ord takes in a letter and returns its ASCII value. chr takes an ASCII value and returns the corresponding letter.

Author:  mirhagk [ Sat Sep 14, 2013 2:05 pm ]
Post subject:  RE:Simple encryption

ahh the good ol vigenere cipher, just make sure your password is as long as the message and random

Author:  Raknarg [ Sat Sep 14, 2013 8:40 pm ]
Post subject:  RE:Simple encryption

yup. Cool to note that the password "dogdog" is the same as the password "dog". I'm not sure how you'd get around that.

Author:  mirhagk [ Sun Sep 15, 2013 7:42 am ]
Post subject:  RE:Simple encryption

You'd have to get into more complicated password generators.


: