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

Username:   Password: 
 RegisterRegister   
 Creating a New Seed for Random
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Prince Pwn




PostPosted: Sun Jan 17, 2010 1:11 pm   Post subject: Creating a New Seed for Random

What is it you are trying to achieve?
Changing my random seed inside the program.

What is the problem you are having?
I can manually change it in the code, encryptionKey = 15, but cannot change it when I execute the program, 4 - Change the Key.

Describe what you have tried to solve this problem
I've organized all my variables at the top and in my code, examined the code, and I'm stumped.

Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)

Turing:


/*
 Author:
 Program: Encryption v1.0
 Date:    1/17/2010
 */


View.Set ("graphics;nocursor")

var debug : boolean := false                            % Show debugging output
const ubound : int := 26                                % Size of alphabet
var encryptionKey : int := 15                           % 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

    /* 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 */
        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



Please specify what version of Turing you are using
4.1.1


Sorry if it's hard to read. I'm running on two 1680x1050 so I can read it all fine.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: