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

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




PostPosted: Mon Mar 08, 2004 6:32 pm   Post subject: encryption program

here is a simple encryption program i made... what can i do to improve it?

code:
const letters := 52
var key : array 1 .. letters of string
var c : string

procedure createKey
    for i : 1 .. letters
        key (i) := ""
    end for
    var next : int
    for i : 1 .. letters
        loop
            key (i) := chr (Rand.Int (33, 126))
            next := 1
            for j : 1 .. letters
                if key (i) = key (j) and j not= i then
                    next := 0
                end if
            end for
            exit when next = 1
        end loop
    end for
end createKey

createKey

fcn encrypt (message : string) : string
    var encrypted : string := ""
    for i : 1 .. length (message)
        if ord (message (i)) <= 90 then
            encrypted += key ((ord (message (i)) - 38))
        else
            encrypted += key ((ord (message (i)) - 96))
        end if
    end for
    result encrypted
end encrypt

fcn getKey (key : array 1 .. letters of string) : string
    var keyString : string := ""
    for i : 1 .. letters
        keyString += key (i)
    end for
    result keyString
end getKey

fcn decrypt (message : string, key : array 1 .. letters of string) : string
    var decrypted : string := ""
    for i : 1 .. length (message)
        for j : 1 .. letters
            if message (i) = key (j) then
                if j <= 26 then
                    decrypted += chr (j + 96)
                else
                    decrypted += chr (j + 38)
                end if
            end if
        end for
    end for
    result decrypted
end decrypt

createKey

var option : int
var message : string
loop
    put "1) encrypt"
    put "2) decrypt"
    put "3) show key"
    put "4) exit"
    put "what do you want to do?"
    get option
    cls
    case option of
        label 1 :
            put "enter message"
            get message
            put encrypt (message), "\n"
        label 2 :
            put "enter message"
            get message
            put decrypt (message, key), "\n"
        label 3 :
            put getKey (key), "\n"
    end case
    exit when option = 4
end loop


-zylum
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Mon Mar 08, 2004 6:58 pm   Post subject: (No subject)

I don't know a whole lot about encryption techniques but I do no that the exit statement should be inside the case construct if you don't want it to crash. (ie, "label 4: exit" and take out the exit when line, or add "label :" and leave the rest)
It looks pretty good, by the way.
Delta




PostPosted: Wed Apr 21, 2004 10:05 am   Post subject: (No subject)

ok first of all....
code:
key (i) := chr (Rand.Int (33, 126))

this is not good.... when creating your new key... this randomizes all the characters in the key...meaning some can be the same...meaning your ecryption does not work properly... at least theres a chance that it won't
Andy




PostPosted: Fri Apr 23, 2004 10:29 am   Post subject: (No subject)

i made an simple encryption program in c++ too lazy to convert it to turing, so here's my encrypt and decrpt functions btw it only works for 10 letters max or something like that...

//encryption program
string encrypt(char word[])
{
//used for holding data
char tempword[15],final[15];
int val[15],j,tempval;
string encrypted;

//encrypt the data
for (int i=0;i<strlen(word);i++)
val[i]=i;

for (i=0;i<strlen(word);i++)
{
j=rand()%strlen(word);
tempval=val[i];
val[i]=val[j];
val[j]=tempval;
}

for (i=0;i<strlen(word);i++)
{
tempword[i]=word[val[i]];
final[i]=val[i]+48;
}

tempword[strlen(word)]='\0';

final[strlen(word)]='\0';
strcat(final,tempword);

for (i=0;i<strlen(final);i++)
final[i]+=((i%3)-1)*i;

encrypted=final;
return encrypted;
}

//decryption program
string decrypt(string word)
{
//used for holding data
char tempword[15],final[15];
int val[15];
string decrypted;
for (int i=0;i<word.length();i++)
final[i]=word[i];
final[word.length()]='\0';
for (i=0;i<strlen(final);i++)
final[i]-=((i%3)-1)*i;

for (i=0;i<strlen(final)/2;i++)
{
val[i]=final[i]-48;
tempword[i]=final[i+(strlen(final)/2)];
}

tempword[strlen(final)/2]='\0';
for (i=0;i<strlen(tempword);i++)
final[val[i]]=tempword[i];

final[strlen(tempword)]='\0';

decrypted=final;
return decrypted;
}

its simple but its pretty good
white_dragon




PostPosted: Fri Apr 23, 2004 5:34 pm   Post subject: (No subject)

WOW!!!! out of all da possibilities i got da same character! anywho. yeah just like Delta said, use someting else rather than tat. good job
WhatAmIDoing




PostPosted: Fri Apr 23, 2004 8:43 pm   Post subject: (No subject)

it's a good idea but it won't work. you can't change the encrypyion back if u want. a code is a set thing not random. but good try
Catalyst




PostPosted: Fri Apr 23, 2004 10:17 pm   Post subject: (No subject)

hes right u need way of saving or sending the key along (too many non-alpha numeric chars for the person to write down, the chr codes would be better) and inputting the key into the prog other wise u can only decrypt the message from the instance of the program that created it which makes it useless Wink
jdworrow




PostPosted: Sun Apr 25, 2004 9:00 am   Post subject: (No subject)


Well, I'm still a little rough around the edges with things in Turing. But I liked the basics of the encryption program. Very good idea to move forward on. Twisted Evil
[/img]
Sponsor
Sponsor
Sponsor
sponsor
Delta




PostPosted: Tue Apr 27, 2004 12:29 pm   Post subject: (No subject)

Here is my encryption program... its got a set key that allows proper encryption and decryption.(proper meaning it works correctly) give it a try.

code:
var start : int := ord ('A')
var newword : string := ""

function CryptIt (words : string) : string
    var word : string := ""
    for cnt : 1 .. length (words)
        word := word + chr (start xor ord (words (cnt)))
    end for
    newword := word
    result newword
end CryptIt

put "Type in a sentence"
get newword : *
put "The sentence encrypted looks like this"
put CryptIt (newword)
put "The sentence decrypted looks like this"
put CryptIt (newword)
WhatAmIDoing




PostPosted: Wed Apr 28, 2004 11:24 am   Post subject: (No subject)

here is my encryption program
var sentence : string
var number : int
loop
put ""
put "Please enter your message:('exit' to quit) " ..
get sentence : *
exit when sentence = "exit"
for i : 1 .. length (sentence)
number := ord (sentence (i))
if number = 90 then
number := 64
elsif number = 122 then
number := 96
elsif number = 32 then
number := 31
end if
put chr ((number) + 1) ..
end for
end loop

if u want to decrypt just keep typing in the letters and eventualyy yuo'll get back to original message
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  [ 10 Posts ]
Jump to:   


Style:  
Search: