The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.

The two Qbasic programs attached include an Encode program and a Decode program, use the model diagrammed above to encode and decode messages.

Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by , 2016-06-20 02:24:03

Encoding & Decoding cryptography

The two Qbasic programs attached include an Encode program and a Decode program, use the model diagrammed above to encode and decode messages.

Encoding & Decoding

The study of secret codes is called cryptography. Early examples of codes go back 4000 years to

Egypt. Writing messages in code plays an important role in history and technology. Today you can

find applications of codes in ATMs, in communications, and on the internet. During World War II the

Germans used their Enigma machine to encode messages to their military forces. Alan Turing (1912-
1954) was an English mathematician who led the Allied Forces team which “cracked” the German

codes. The Allied Forces intercepted the German encoded messages and decoded the messages to gain

critical military information. The Allies were careful not to reveal to the Germans that they had
“cracked the codes”, leaving the Germans ignorant of the information leak.

The Enigma was a machine that used rotors to perform a different shift for each letter of the code,
according to a prearranged keyword. For example, if the keyword was ENCODE, the first letter of the
message would shift by 5 (because E is the fifth letter in the alphabet), the second letter would shift by
14 (because N is the fourteenth letter), and so on.

INPUT CONVERSION OUTPUT

Each letter of Each letter of
Message Encoded Message

ENIGMA

Another Encoding Method: You can also represent the code with a grid as shown below. Note that
the input letters run across horizontally and the output (coded) letters run up or down vertically.

CODE GRID

A X
B X
CX X
D X
EX X
F X
GX X
H X
IX
J
KX
L
MX
N
OX

PX
QX
RX
SX
TX
UX
VX
WX
XX
YX
ZX

A B C D E F G H I J K L M N O P Q R S T U V WX Y Z

The coded rules above describe relationships between input letters and output letters. Codes
that have exactly one output letter for each input letter are examples of functions. The domain of a
function is the set of input values and the range of a function is the set of output values.

Another Encoding Method using ASCII: Our computer languages use the American Standard Code
for Information Interchange (ASCII). Special characters, symbols, and numbers are represented by
numbers 0 through 64 and letters are represented by numbers 65 to 90 as shown in the table below.

ASCII TABLE

Input A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Output 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

The diagram below demonstrates how a message can be encoded, sent to the receiver, then decoded by
the receiver using ASCII Codes and an inverse function.

ASCII Function Inverse ASCII
Conversion Function Conversion

Message ASCII Encoded ASCII Message

Letters Numbers Message Numbers Letters

(numbers)

SENDER RECEIVER

The two Qbasic programs attached include an Encode program and a Decode program, use the
model diagrammed above to encode and decode messages. The program coding is provided as
information only. You will use these programs and the steps below to complete the encoding and
decoding exercise.

During the Lab you and your partner each send an encoded message via email. Follow the steps
below to complete the Lab.

a. First, you will need the Qbasic program, the encoding program, and the decoding program
available on the teacher website. Your secret code will be the sender's birthday. Your function will
be in the form: Y = month (X + year)2 - day. Note the month, year and day represent your date of
birth. Now write your encoding function below.

b. Now determine the “inverse function” that your partner must use to decode your messages.

c. Write your message below.

d. Encode your message. Download and execute the Qbasic program located on the teacher’s
website. Then open the Encode7.bas file. Then run the Encode7.bas file. Follow the program
prompts. The program should have created a data file called encode.dat on the same directory
in which you initiated the Encode program. Check your directory to see if the file is there.

e. Send your message via email. Send an email message to your partner attaching the data file,
encode.dat.

f. Receiving the message. Your partner should check her email and bring up the message on the
screen. Then he/she should save the encode.dat file to his/her directory. Then examine the
contents (the encoded message) of the data file.

g. Decode your message. Your partner should execute the Qbasic program. Then open the
Decode7.bas file. Then run the Decode7.bas file. Be sure to enter the sender's birthdate.
Otherwise the message will not be decoded. You must show your instructor the decoded
message to receive credit.

h. Switch roles. The receiver now becomes the sender and vice versa.

File: Encode1a.bas

CLS
REM This QBasic file has programming code that is useful to encode a message
REM The method used to encode a message is to convert each letter in the message
REM to its corresponding ASCII code, then convert each ASCII code to another
REM number through the use of a function (for example, Y = 6*(X + 1955)^2 - 23),
REM where X represents the ASCII code of each letter in the message. These new
REM numbers represent the encoded message sent to the addressee. The addressee
REM then uses the appropriate inverse function (such as, SQR((X + 23)/6)-1955)
REM to decode the message by converting the encoded numbers back to the ASCII REM
code and also converting those ASCII codes to corresponding letters.
REM Joining these characters together will finish decode the message.

REM Run the programs and examine the output to understand how to encode and
REM decode messages. Send your encoded message to a friend and see if he/she
REM can decode the message using the decode program and the "key" (the inverse
REM function). Set the secret code function by inputting your birth date.

INPUT "Input your birth date (Day, Month (a number), and Year, e.g. 23, 6, 1955)
", day, month, year
PRINT
PRINT "Your date of birth is: "; day; month; year
PRINT

REM Input the message you want to send. Print the message to test
REM the character variable (Message$). Find the length of the message
REM and use the length to dimension the array, Message.

INPUT "Input message (DO NOT INCLUDE COMMAS!) ", message$
PRINT
PRINT "Your message is: "; message$
PRINT
length = LEN(message$)
PRINT "The length of your message is: "; length
PRINT
DIM message(1 TO length)
DIM encode(1 TO length)
DIM decode(1 TO length)

REM Create a loop which takes each letter of your message (variable message$)
REM and converts it to ASCII code using the ASC and MID$ functions and stores
REM each converted letter into an array, message(I). Then print out the array.

PRINT "The ASCII codes for your message are: ";
FOR I = 1 TO length
message(I) = ASC(MID$(message$, I, 1))
PRINT message(I);
NEXT I
PRINT " "
PRINT
REM Create a loop which takes the ASCII codes and converts each to a new
REM number using the secret code function, in this case I used a function
REM based on my birth date: f(X) = 6*(X + 1955)^2 - 23. In this sequence
REM I have done the conversion to ASCII codes and then to encoded numbers in
REM one step: Encode(I) = .... . Then print out the array, Encode(I) to
REM show the encoded numbers.

PRINT "The encoded numbers are: ";
FOR I = 1 TO length
encode(I) = 6 * (ASC(MID$(message$, I, 1)) + 1955) ^ 2 - 23
PRINT encode(I);
NEXT I

PRINT " "
PRINT " "
PRINT "These codes may be in scientific notation, such as: 7.00456E+07"
PRINT " "
PRINT "You may convert such numbers to standard notation, such as: 70045670"
PRINT
END

File: Decode1a.bas

CLS
REM This QBasic file has programming code that is useful to decode a message.
REM The method used to decode an encoded message is to use the appropriate
REM "inverse function" (for example, SQR((X + 23)/6)-1955) to decode the
REM message by converting the encoded numbers back to the ASCII code and then
REM converting those ASCII codes to the corresponding letters. Joining these
REM characters together will finish decode the message.

REM Input encoded message you have received by writing the numbers
REM separated by spaces. Print the encoded message to test your entries.
REM Input the length of the encoded message and use the length to dimension
REM the array, Encode(I).

INPUT "How many numbers are in your message?: ", length
DIM encode(1 TO length)
INPUT "Enter the first encoded number in the message: ", encode(1)

FOR I = 2 TO length
INPUT " Enter the next number in the encoded message: ", encode(I)

NEXT I
PRINT
PRINT "Your encoded message has: "; length; "numbers"
PRINT
PRINT "Your encoded message is: ";
FOR I = 1 TO length
PRINT encode(I);
NEXT I
PRINT
PRINT
DIM decode(1 TO length)
DIM decode$(1 TO length)

REM Verify your message by decoding these numbers using the appropriate
REM "inverse function". In this case, the appropriate inverse function is
REM SQR((X + 23)/6)-1955 where X is now the encoded numbers from above,
REM the same numbers that would be sent from the sender in a data file.
REM Then print the decoded numbers, array decode(I).

PRINT "The decoded numbers are: ";
FOR I = 1 TO length
decode(I) = INT((SQR((encode(I) + 23) / 6) - 1955) + .5)
PRINT decode(I);
NEXT I
PRINT

REM Now convert the decoded numbers back to ASCII characters using the CHR$
REM function and print the decoded message, array decode$(I).

PRINT "The decoded message is: ";
FOR I = 1 TO length
decode$(I) = CHR$(decode(I))
PRINT decode$(I);
NEXT I
END


Click to View FlipBook Version