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

Username:   Password: 
 RegisterRegister   
 Simple encryption
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
mirhagk




PostPosted: 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
Raknarg




PostPosted: 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.
mirhagk




PostPosted: Sun Sep 15, 2013 7:42 am   Post subject: RE:Simple encryption

You'd have to get into more complicated password generators.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: