Encryption
Author |
Message |
Prince Pwn
|
Posted: Tue Jun 15, 2010 6:24 am Post subject: Encryption |
|
|
Program I made for my algorithms class a few months ago:
Turing: |
/*
Author:
Program: Encryption v1.0
Date: 1/17/2010
*/
View.Set ("graphics:600,250;position:400,400;nocursor,nobuttonbar")
var debug : boolean := false % Show debugging output
const ubound : int := 26 % Size of alphabet
var encryptionKey := Rand.Int (1, maxint) % Encryption key
var randNum : int % Used to hold a random number
var encryptionLetter : array 1 .. ubound of char % Stores each letter in order
var encryptionStorage : array 1 .. ubound of char % Stores each letter perminantely
var alphabet : string := "" % Stores the alphabet perminantely
var encryptedAlphabet : string := "" % Stores the encrypted alphabet
var ch : char % Used for character input in menu
var convertMessage : string % Message to convert
var message : string := "" % Message after conversion
var encryptedMessage : string % Encrypted message
var decryptedMessage : string % Decrypted message
for i : 1 .. ubound % Counts the alphabet
encryptionLetter (i ) := chr (64 + i ) % Fills each letter uniquely and in order as capitals
alphabet + = encryptionLetter (i ) % Fills the alphabet
end for
loop
/* Coloring the application */
colorback (black)
Draw.FillBox (0, 0, maxx, maxy, black)
color (brightgreen)
/* Main Menu */
put "My Encryption Program"
put "---------------------"
put "1 - Encrypt a Message"
put "2 - Decrypt a Message"
put "3 - View the Key"
put "4 - Change the Key"
/* Makes user select an option */
loop
Input.Flush
delay (0)
Input.Flush
ch := getchar
exit when ch = '1' or ch = '2' or ch = '3' or ch = '4'
end loop
/* Encrypt a message */
if ch = '1' then
Rand.Set (encryptionKey ) % Starts a sequence of pseudo-random numbers
message := "" % Blanks message
encryptedMessage := "" % Blanks encrypted message
for decreasing i : ubound .. 1 % Counting alphabet backwards
randNum := Rand.Int (1, i ) % Sets random letter from 'a' to 'z,y,x...'
encryptionStorage (i ) := encryptionLetter (randNum ) % Store random letter into storage
encryptedAlphabet + = encryptionStorage (i ) % Stores the alphabet randomly
encryptionLetter (randNum ) := encryptionLetter (i ) % Sets that random letter back where it cannot be reached on next loop
end for
if (debug ) then
put "\nAlphabet:\t\t\t\t", alphabet
put "Encrypted Alphabet:\t\t\t", encryptedAlphabet, '\n' ..
put "Generated an encryption key:\t\t", encryptionKey, '\n'
end if
put "\n>> Please enter a message to encrypt:\t" .. % Prompts user input
get convertMessage : * % User input
for i : 1 .. length (convertMessage ) % Scans through message to convert
if ord (convertMessage (i )) >= 97 and ord (convertMessage (i )) <= 122 then % If the character is lower case
message + = chr (ord (convertMessage (i )) - 32) % Converts to upper and appends
else % Anything but lower case remains the same
message + = convertMessage (i ) % Appends character to final message
end if
end for
for i : 1 .. length (message ) % Scans through the message
if ord (message (i )) >= 65 and ord (message (i )) <= 90 then % If the character is in the alphabet
for j : 1 .. length (encryptedAlphabet ) % Scans through encrypted alphabet
if message (i ) = alphabet (j ) then % If current letter in message matches the alphabet
encryptedMessage + = encryptedAlphabet (j ) % Appends encrypted letter to message
exit % Exits the loop to save time
end if
end for
else % If the character is not a-z
encryptedMessage + = message (i ) % Add it, unencrypted
end if
end for
/* Output for user */
color (brightgreen)
put ">> Encrypted message:\t\t\t", encryptedMessage ..
put "\n>> Encryption key:\t\t\t", encryptionKey
put "\nPress any key to continue..."
Input.Pause
cls
/* Encrypt a message */
elsif ch = '2' then
decryptedMessage := ""
message := "" % Blanks message
put "\n>> Please enter a message to decrypt:\t" ..
get convertMessage : * % Input message
for i : 1 .. length (convertMessage ) % Scans through message to convert
if ord (convertMessage (i )) >= 97 and ord (convertMessage (i )) <= 122 then % If the character is lower case
message + = chr (ord (convertMessage (i )) - 32) % Converts lower case character to upper case character and appends it to final message
else % Anything but lower case remains the same
message + = convertMessage (i ) % Appends character to final message
end if
end for
put ">> Enter the decription key in decimal:\t" ..
var decriptionKey : int
get decriptionKey
var decryptedAlphabet : string := ""
Rand.Set (decriptionKey ) % Sets the decryption key
var decryptionLetter : array 1 .. ubound of char % Stores each letter in order
var decryptionStorage : array 1 .. ubound of char % Stores each letter perminantely
for i : 1 .. ubound % Counts the alphabet
decryptionLetter (i ) := chr (64 + i ) % Fills each letter uniquely and in order as capitals
end for
for decreasing i : ubound .. 1 % Counting alphabet backwards
randNum := Rand.Int (1, i ) % Sets random letter from 'a' to 'z,y,x...'
decryptionStorage (i ) := decryptionLetter (randNum ) % Store random letter into storage
decryptedAlphabet + = decryptionStorage (i ) % Stores the alphabet randomly
decryptionLetter (randNum ) := decryptionLetter (i ) % Sets that random letter back where it cannot be reached on next loop
end for
if debug then
put "Decrypted Alphabet:\t\t\t", decryptedAlphabet ..
put "\nAlphabet:\t\t\t\t", alphabet, '\n' ..
end if
for i : 1 .. length (message ) % Scans through the message
for j : 1 .. length (decryptedAlphabet ) % Scans through the decrypted alphabet
if message (i ) = decryptedAlphabet (j ) then % If letters match
decryptedMessage + = alphabet (j ) % Add to decrypted message
end if
end for
if ord (message (i )) < 65 or ord (message (i )) > 90 then % If character is anything but 'A' - 'Z'
decryptedMessage + = message (i ) % Add to message
end if
end for
/* Output for user */
put ">> Decrypted Message:\t\t\t", decryptedMessage
put "\nPress any key to continue..."
Input.Pause
cls
elsif ch = '3' then
/* View the key */
put "\n>> Current encryption key: ", encryptionKey
put "\nPress any key to continue..."
Input.Pause
cls
elsif ch = '4' then
/* Change the key */
put "\n>> Current encryption key: ", encryptionKey
put ">> Enter a new key: " ..
get encryptionKey
cls
end if
end loop
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cezna
|
Posted: Tue Jun 15, 2010 2:07 pm Post subject: RE:Encryption |
|
|
Nice looking interface, I love it this kind of style.
I was looking through it and don't have the patience, attention span or likely the skill necessary to understand how it works.
Mind briefly explaining it (just enough to give me an idea so that I can read through the code with this information in mind and be able to understand it). |
|
|
|
|
|
USEC_OFFICER
|
Posted: Tue Jun 15, 2010 2:43 pm Post subject: RE:Encryption |
|
|
It looks like a Caesar shift to me. Though it could just be random letters instead. |
|
|
|
|
|
Cezna
|
Posted: Tue Jun 15, 2010 3:26 pm Post subject: RE:Encryption |
|
|
Isn't that when the numerical value of a letter is shifter a given number of values in one direction?
I doubt that, as I looked at the key, and it is so large that the letters would be way out of range.
Unless the first number in the key is the ammount to shift the first letter, the second number in the key the ammount to shift the second letter, and so on (and then it repeats after reaching the last number in the key). |
|
|
|
|
|
chrisbrown
|
Posted: Tue Jun 15, 2010 3:54 pm Post subject: Re: RE:Encryption |
|
|
Cezna @ Tue Jun 15, 2010 3:26 pm wrote: Isn't that when the numerical value of a letter is shifter a given number of values in one direction? Yep.
Cezna @ Tue Jun 15, 2010 3:26 pm wrote: I doubt that, as I looked at the key, and it is so large that the letters would be way out of range. Easily solved with the mod function.
From the looks of it, he's generating an alphabet in random order, then replacing each letter with its randomized positional equivalent.
In this case, the key is actually used as a random number seed. Normally, Rand.Int returns an unpredictable number each time, but if you set a seed with Rand.Set, it will generate the same sequence every time. |
|
|
|
|
|
Cezna
|
Posted: Tue Jun 15, 2010 4:06 pm Post subject: Re: RE:Encryption |
|
|
Cezna @ Tue Jun 15, 2010 3:26 pm wrote: I doubt that, as I looked at the key, and it is so large that the letters would be way out of range. Easily solved with the mod function.[/quote]
But if you were going to use mod, why have a key that is like 10 digits long?
Cezna @ Tue Jun 15, 2010 3:26 pm wrote: In this case, the key is actually used as a random number seed. Normally, Rand.Int returns an unpredictable number each time, but if you set a seed with Rand.Set, it will generate the same sequence every time.
And if you are having it generate the same sequence every time, what is the prupose of it being random (or do you eman each time until the program is closed and rerun?)
I guess I should probably understand Rand.Set a little better before I go commenting on it
|
|
|
|
|
|
USEC_OFFICER
|
Posted: Tue Jun 15, 2010 5:17 pm Post subject: Re: RE:Encryption |
|
|
Cezna @ Tue Jun 15, 2010 5:06 pm wrote:
But if you were going to use mod, why have a key that is like 10 digits long?
To make it more secure? |
|
|
|
|
|
Cezna
|
Posted: Tue Jun 15, 2010 5:39 pm Post subject: RE:Encryption |
|
|
Maybe, but it is all open source, and you can ask it what the key is and decrypt encrypted messages.
Wouldn't make sense to go to such lengths to hide the key and then do all that. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
chrisbrown
|
Posted: Tue Jun 15, 2010 5:48 pm Post subject: Re: RE:Encryption |
|
|
USEC_OFFICER @ Tue Jun 15, 2010 5:17 pm wrote: Cezna @ Tue Jun 15, 2010 5:06 pm wrote:
But if you were going to use mod, why have a key that is like 10 digits long?
To make it more secure?
Reducing modulo the number of printable characters means you have around 100 possible shift values. Whether you have 1 digit or a million, there is nothing secure about a ceasar cipher.
And in this case, a large key simply means it isn't likely to be duplicated. A key of "1" will give the same type of results as any other integer when used as a random seed, it's just harder to guess a big one.
Cezna @ Tue Jun 15, 2010 5:06 pm wrote: If you are having it generate the same sequence every time, what is the prupose of it being random (or do you mean each time until the program is closed and rerun?)
If you know the seed when encrypting, by seeding the same value later on, you can generate the same "random" alphabet to decrypt with.
Turing generates a random seed automatically when execution starts. Run this a few times and compare what changes.
Turing: | for i : 1.. 5
put Rand.Int (0, 100)
end for
put ""
Rand.Set(12345)
for i : 1.. 5
put Rand.Int (0, 100)
end for |
|
|
|
|
|
|
DtY
|
Posted: Tue Jun 15, 2010 7:54 pm Post subject: Re: RE:Encryption |
|
|
chrisbrown @ Tue Jun 15, 2010 5:48 pm wrote: USEC_OFFICER @ Tue Jun 15, 2010 5:17 pm wrote: Cezna @ Tue Jun 15, 2010 5:06 pm wrote:
But if you were going to use mod, why have a key that is like 10 digits long?
To make it more secure?
Reducing modulo the number of printable characters means you have around 100 possible shift values. Whether you have 1 digit or a million, there is nothing secure about a ceasar cipher.
And in this case, a large key simply means it isn't likely to be duplicated. A key of "1" will give the same type of results as any other integer when used as a random seed, it's just harder to guess a big one.
No, because of how few keys there are for a caeser shift, if you are working with n numbers, whatever key you choose, say k, will be the same key as k mod n, that is, if you have 26 characters, and you choose the key 27, it is the same key as 1. Picking a large key for a caeser shift wont make it more secure. |
|
|
|
|
|
chrisbrown
|
Posted: Tue Jun 15, 2010 8:09 pm Post subject: Re: RE:Encryption |
|
|
chrisbrown wrote: Reducing modulo the number of printable characters means you have around 100 possible shift values. Whether you have 1 digit or a million, there is nothing secure about a ceasar cipher.
And in this case, a large key simply means it isn't likely to be duplicated. A key of "1" will give the same type of results as any other integer when used as a random seed, it's just harder to guess a big one. DtY @ Tue Jun 15, 2010 7:54 pm wrote: No, because of how few keys there are for a caeser shift, if you are working with n numbers, whatever key you choose, say k, will be the same key as k mod n, that is, if you have 26 characters, and you choose the key 27, it is the same key as 1. Picking a large key for a caeser shift wont make it more secure.
Sorry, I wasn't very clear. That's what I was trying to say in the first part; in the second, that - with respect to OP's program - Rand.Seed (1) will encrypt just as well as Rand.Seed(1087590873), but the second is much harder to guess. |
|
|
|
|
|
Prince Pwn
|
Posted: Wed Apr 13, 2011 9:14 am Post subject: Re: Encryption |
|
|
Updated code.
Changelog:
- Password protected key
- Program stores encryption key in a separate, unencrypted file
- Minor GUI changes
Turing: | /*
Author:
Program: Encryptopolis
Date Created: 1/17/2010
Last Modified: 4/13/2011
*/
var version : string := "1.0.1"
View.Set ("graphics:600,250;position:400,400;nocursor,nobuttonbar,title:Encryptopolis " + version )
var debug : boolean := false % Show debugging output
const ubound : int := 26 % Size of alphabet
var encryptionKey := 3713 % Default encryption key
var randNum : int % Used to hold a random number
var encryptionLetter : array 1 .. ubound of char % Stores each letter in order
var encryptionStorage : array 1 .. ubound of char % Stores each letter perminantely
var alphabet : string := "" % Stores the alphabet perminantely
var encryptedAlphabet : string := "" % Stores the encrypted alphabet
var ch : char % Used for character input in menu
var convertMessage : string % Message to convert
var message : string := "" % Message after conversion
var encryptedMessage : string % Encrypted message
var decryptedMessage : string % Decrypted message
var passwordInput : string % User input for password
var password : string := "bunnies" % Password to view/change the key
var verification : string % Yes/No dialogue prompt
var fileID : int % Encryption file ID
var fileName : string := "enc" % File holding the encryption key
var fileText : string % Read from the encryption file
/* Sets a new key */
procedure SetKey (key : int)
open : fileID, fileName, put, mod
put : fileID, key
close : fileID
end SetKey
/* Obtains current key */
function GetKey : int
open : fileID, fileName, get
get : fileID, fileText
close : fileID
result strint (fileText )
end GetKey
for i : 1 .. ubound % Counts the alphabet
encryptionLetter (i ) := chr (64 + i ) % Fills each letter uniquely and in order as capitals
alphabet + = encryptionLetter (i ) % Fills the alphabet
end for
loop
/* Coloring the application */
colorback (black) % Background for output
Draw.FillBox (0, 0, maxx, maxy, black) % Background for application
color (brightgreen) % Color of text
if not File.Exists (fileName ) then % First run
put " Enter an encryption key from 0 - ", maxint, ": " .. % Set an encryption key
get encryptionKey
SetKey (encryptionKey )
cls
else
encryptionKey := GetKey
end if
/* Main Menu */
put "\n Encryptopolis v" + version
put " ---------------------"
put " 1 - Encrypt a Message"
put " 2 - Decrypt a Message"
put " 3 - View the Key"
put " 4 - Change the Key"
put " \n Message is NOT case sesnsitive\n"
/* Makes user select an option */
loop
Input.Flush % Clears keyboard echoing
ch := getchar
exit when ch = '1' or ch = '2' or ch = '3' or ch = '4'
end loop
/* Encrypt a message */
if ch = '1' then
Rand.Set (encryptionKey ) % Starts a sequence of pseudo-random numbers
message := "" % Blanks message
encryptedMessage := "" % Blanks encrypted message
for decreasing i : ubound .. 1 % Counting alphabet backwards
randNum := Rand.Int (1, i ) % Sets random letter from 'a' to 'z,y,x...'
encryptionStorage (i ) := encryptionLetter (randNum ) % Store random letter into storage
encryptedAlphabet + = encryptionStorage (i ) % Stores the alphabet randomly
encryptionLetter (randNum ) := encryptionLetter (i ) % Sets that random letter back where it cannot be reached on next loop
end for
if (debug ) then
put "\n Alphabet:\t\t\t\t", alphabet
put " Encrypted Alphabet:\t\t\t", encryptedAlphabet, '\n' ..
put " Generated an encryption key:\t\t", encryptionKey, '\n'
end if
put " >> Please enter a message to encrypt:\t" .. % Prompts user input
get convertMessage : * % User input
for i : 1 .. length (convertMessage ) % Scans through message to convert
if ord (convertMessage (i )) >= 97 and ord (convertMessage (i )) <= 122 then % If the character is lower case
message + = chr (ord (convertMessage (i )) - 32) % Converts to upper and appends
else % Anything but lower case remains the same
message + = convertMessage (i ) % Appends character to final message
end if
end for
for i : 1 .. length (message ) % Scans through the message
if ord (message (i )) >= 65 and ord (message (i )) <= 90 then % If the character is in the alphabet
for j : 1 .. length (encryptedAlphabet ) % Scans through encrypted alphabet
if message (i ) = alphabet (j ) then % If current letter in message matches the alphabet
encryptedMessage + = encryptedAlphabet (j ) % Appends encrypted letter to message
exit % Exits the loop to save time
end if
end for
else % If the character is not a-z
encryptedMessage + = message (i ) % Add it, unencrypted
end if
end for
/* Output for user */
color (brightgreen)
put " >> Encrypted message:\t\t\t", encryptedMessage ..
put "\n >> Encryption key:\t\t\t", encryptionKey
put "\n Press any key to continue..."
Input.Pause
cls
/* Encrypt a message */
elsif ch = '2' then
decryptedMessage := ""
message := "" % Blanks message
put " >> Please enter a message to decrypt:\t" ..
get convertMessage : * % Input message
for i : 1 .. length (convertMessage ) % Scans through message to convert
if ord (convertMessage (i )) >= 97 and ord (convertMessage (i )) <= 122 then % If the character is lower case
message + = chr (ord (convertMessage (i )) - 32) % Converts lower case character to upper case character and appends it to final message
else % Anything but lower case remains the same
message + = convertMessage (i ) % Appends character to final message
end if
end for
put " >> Enter the decription key:\t\t" ..
var decriptionKey : int
get decriptionKey
var decryptedAlphabet : string := ""
Rand.Set (decriptionKey ) % Sets the decryption key
var decryptionLetter : array 1 .. ubound of char % Stores each letter in order
var decryptionStorage : array 1 .. ubound of char % Stores each letter perminantely
for i : 1 .. ubound % Counts the alphabet
decryptionLetter (i ) := chr (64 + i ) % Fills each letter uniquely and in order as capitals
end for
for decreasing i : ubound .. 1 % Counting alphabet backwards
randNum := Rand.Int (1, i ) % Sets random letter from 'a' to 'z,y,x...'
decryptionStorage (i ) := decryptionLetter (randNum ) % Store random letter into storage
decryptedAlphabet + = decryptionStorage (i ) % Stores the alphabet randomly
decryptionLetter (randNum ) := decryptionLetter (i ) % Sets that random letter back where it cannot be reached on next loop
end for
if debug then
put " Decrypted Alphabet:\t\t\t", decryptedAlphabet ..
put "\n Alphabet:\t\t\t\t", alphabet, '\n' ..
end if
for i : 1 .. length (message ) % Scans through the message
for j : 1 .. length (decryptedAlphabet ) % Scans through the decrypted alphabet
if message (i ) = decryptedAlphabet (j ) then % If letters match
decryptedMessage + = alphabet (j ) % Add to decrypted message
end if
end for
if ord (message (i )) < 65 or ord (message (i )) > 90 then % If character is anything but 'A' - 'Z'
decryptedMessage + = message (i ) % Add to message
end if
end for
/* Output for user */
put " >> Decrypted Message:\t\t\t", decryptedMessage
put "\n Press any key to continue..."
Input.Pause
cls
end if
if ch = '3' then
/* View the key */
put " >> Enter the password to view the key: " ..
get passwordInput
if passwordInput = password then
put " >> Current encryption key: ", GetKey
else
put " >> Invalid Password!"
end if
put "\n Press any key to continue..."
Input.Pause
cls
elsif ch = '4' then
/* Change the key */
put " Enter the password to change the key: " ..
get passwordInput
if passwordInput = password then
put " >> Current encryption key: ", encryptionKey
put " >> Enter a new key: " ..
get encryptionKey
put " >> Are you sure? (Y/N)? " ..
get verification
if verification = 'y' or verification = 'Y' or verification = "yes" then
SetKey (encryptionKey )
put " >> New key was set successfully!"
else
put " >> You did not set a new key"
end if
else
put " >> Invalid Password!"
end if
put "\n Press any key to continue..."
Input.Pause
cls
end if
end loop
|
|
|
|
|
|
|
|
|