Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble gettting the vigenere square to work for this python project any help would be appreciated CSC 130 Lesson 2 Lab Vigenere

I am having trouble gettting the vigenere square to work for this python project any help would be appreciated

CSC 130 Lesson 2 Lab

Vigenere Encryption/Decryption Algorithm

Goal

Take a detailed requirements and system design specification and code up a small program

Understand the use of functions to breakdown a problem into smaller pieces to code and test

Use the Vigenere encryption algorithm to encrypt a string

Use the Vigenere decryption algorithm to decrypt the coded string

Use a nested list: a matrix

Learn how to convert a character to its ASCII value and back again

Reading

Read this Requirements and System Design document carefully. Being able to translate this into code is an important piece of this exercise. After reading the document, if you still don't understand this lab, then please:

Read it again, more slowly (perhaps out loud).

While reading it, take notes. Writing something down often helps you to understand it better.

Email me with specific questions. Just saying "I don't understand" won't cut it.

Come to class to do the lab and perhaps Open Lab on Wednesday night.

Come to office hours for help

Please follow the design specifications for this Lab. You are required to use functions and a nested list (matrix) to hold the Vigenere square. You must code up the encryption function called encrypt and the decryption function called decrypt. These two functions must call other functions to help perform their task. You will be using most of this code again in Project 3.

Read the following documents before attempting this Lab:

Review Slides

Submission

The project folder name and Python file must be exactly as specified below.

Name your PyCharm project: Lab2. This is a folder.

Place the code for this program in the file vigenere.py.

Zip up the Lab2 folder producing the Lab2.zip file and submit it to Blackboard. Do not submit any rar files. It should contain the following four files:

vigenere.py

I do not grade programs that have syntax errors or ones that raise exceptions when running the program. Email me broken code all zipped up as you would to submit it. Do not submit broken code.

Requirements Specification

For this program:

1) The user will input a string containing a message of several words and punctuation.

2) Only the alphabetic characters in the string must be encrypted. The rest of the characters are left as is in the message.

3) You must use the Vigenere encryption algorithm as described below.

4) The user will also enter the key needed which must contain only one word with only alphabetic characters.

5) The Vigenere square will only contain the lowercase letters. You should convert the message and key to lower case before the encryption.

6) The program will display the encrypted message.

7) Next, the encrypted message is decrypted, and the decoded message should be displayed in lowercase.

Background

In todays cyber world, files are encrypted and sent over the worlds networks. Cryptography is the science of making messages secure, of transforming readable messages (plaintext) into unreadable messages (ciphertext) and back again. The process of turning plaintext into ciphertext is called encryption. The reverse process is called decryption.

Most encryption schemes use an encryption key. In principle, it is possible to decrypt the message without possessing the key. But, for a well-designed encryption scheme, large computational resources and skill are required. An authorized recipient can easily decrypt the message with the key provided by the originator to recipients, but not to unauthorized interceptors.

Basic Substitution Cipher

The art of creating and sending secret messages goes back to at least the time of Caesar. The most basic encryption scheme is the substitution cipher, where one letter in the text is substituted for another. Here, the key would be the list of substitution characters. These basic substitution cipher encoded messages are fairly easy to decipher without the key using the frequency of letters in the language.

Example of Substitution Cipher:

Alphabet: abcdefghijklmnopqrstuvwxyz

Key: bpzhgocvjdqswkimlutneryaxf

Encode: waketech:ybqgngzv

Vigenere Cipher

In 1592, the Vigenere cipher was created. This Vigenere cipher encryption scheme uses a key and a matrix called the Vigenere square (the pink matrix below bounded by the green column to the left and the tan row above with the column indices in the row above the square and the row indices in the column to the left of the square). It uses multiple letter mappings, rather than one key for the whole message. By using a different key for each letter, it makes the frequency analysis much more difficult and basically useless.

The Vigenere square is a simple rotation of the alphabet for each letter. The Vigenere square below is for the lowercase alphabet: (This is the one used in this lab. Project 3 will use the first 127 characters of the ASCII code).

The first row goes from a to z. The second row goes from b to z with a at the end. The next row goes from c to z with a and b at the end. The next row goes from d to z with a to c at the end. This pattern is repeated with the last row starting with z and having a to y at the end. See the picture below.

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

0

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

1

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

a

2

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

3

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

4

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

5

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

6

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

7

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

8

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

9

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

10

k

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

11

l

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

12

m

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

13

n

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

14

o

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

15

p

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

16

q

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

17

r

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

18

s

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

19

t

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

20

u

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

21

v

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

22

w

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

23

x

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

24

y

z

a

b

c

d

e

f

g

h

i

j

k

l

m

n

o

p

q

r

s

t

u

v

w

x

25

z

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

This matrix of lowercase letters can be created by using the Python ord and chr functions that work with the ASCII numeric codes for characters. The ord function takes the letter and converts it to its ASCII numeric code. The chr function takes the ASCII numeric code and converts it back to its character.

For example:

chr_code = ord(letter) - returns 97, when letter is a

letter = chr(chr_code) - return a, when chr_code is 97

The lowercase letters have sequential ASCII numeric codes starting with 97 and ending with 122. Converting the characters to their ASCII codes allows you to increment the characters numerically.

The first row of the matrix has a to z, which is 97 to 122. So, starting with a at column 0 of row 0, you can loop 26 times placing each of the letters in that row by adding one to the ASCII code of the previous letter, and then converting the number to its character using the chr function, before placing it in the matrix.

For each inner list, (matrix row) the code is

# Adding the row number to the ASCII code for a

# starts the row with the correct letter.

# For row=0: chr_code is 97 + 0 = 97, which is a

# For row=1: chr_code is 97 + 1 = 98, which is b

chr_code = ord(a) + row

for col in range(26):

letter = chr(chr_code)

vig_row.append(letter)

chr_code = chr_code + 1

You will have to handle the wrap back to a after placing z in the matrix and appending the inner row (list) to the outer matrix.

With this algorithm, the rows are associated with the characters in the key and the columns are associated with the characters in the message to encrypt. In this lab both the message and key should be converted to lower case, but not with Project 3.

For this example of the Vigenere cipher, the key is LINKED and the message is The eagle has landed.

Example:

plaintext message: the eagle has landed.

Key: linked

l

i

n

k

e

d

l

i

n

k

e

d

l

i

n

k

e

.

t

h

e

e

a

g

l

e

h

a

s

l

a

n

d

e

d

.

The algorithm works as follows:

Choose a key: Say: LINKED

Convert the message and key to lowercase.

The key will be used repeatedly, matching the letters in the message with the letters in the key.

Find the ciphertext letter (the code letter) in the Vigenere square matrix by following the steps below:

a) Using the character from the key, find the row where it exists by looking down column 0 until you find the correct row.

b) Using the character from the plaintext message, find the column where it exists by looking across row 0 until you find the correct column.

c) The ciphertext character will be the one in the matrix having the row and column found above

d) Add the ciphertext character to the coded message.

key

l

i

n

k

e

d

l

i

n

k

e

d

l

i

n

k

e

.

row

11

8

13

10

4

3

11

8

13

10

4

3

11

8

13

10

4

col

19

7

4

4

0

6

11

4

7

0

18

11

0

13

3

4

3

msg

t

h

e

e

a

g

l

e

h

a

s

l

a

n

d

e

d

.

e

p

r

o

e

j

w

m

u

k

w

o

l

v

q

o

h

.

With this example, we start at the beginning of the key at l matching it to the t in the message to encode.

a) We find the row for l (row 11) and the column for t (column 19).

b) The character at matrix [row, col] = matrix [11, 19] = e.

c) So, place e in the coded message.

d) Next, we move to the i in linked and match that to h in the message to encode.

e) We find the row for i (row 8) and the column for h (column 7).

f) The character at matrix [8, 7] = p. So, place p in the coded message.

g) And so forth.

h) When we reach a character that is not an alphabetic letter, we dont encode it.

i) So, the blank remains a blank and the period remains a period.

j) When we reach the end of the key, we start back over again with the key. This happens above at the l in eagle and the a in landed.

For decryption we use the same Vigenere square and key word, but this time we are finding the plaintext.

The algorithm works as follows:

Find the ciphertext letter in the Vigenere square, using the key letter row.

In that same column, get the character at row 0, this is the plaintext letter.

Add the plaintext letter to the decoded message.

From our example above, the encrypted message was

"epr oejwm ukw olvqoh.".

So, in the matrix, we find the letter e, in row l, look up the letter at row 0 of that column. It is a t. So, add it the plaintext message. Next, we find the p, at row i. Row 0 of that column has an h. So, now our message is th". Do this for the rest of the cipher text to get our original message.

System Design

In the vigenere.py file, the main function reads in the plaintext string to be encrypted and the key to use. Both are converted to lowercase. The main function then calls the create_vig_square function which returns the matrix setup as described above, using the ord and chr functions. Next, it calls the encrypt function passing in the matrix, the string to be coded and the and the key. The encrypt method calls other methods that help it do the encryption (). The encrypt function then passes the encoded ciphertext message back to the main method where it is displayed.

You must breakdown the encrypt method into simpler tasks and create at least 2 other functions that are called from the encrypt method to do these tasks.

After the encrypted ciphertext message is displayed, the main method must call the decrypt method to convert the message back to its lowercase form.

An example of functions you should use are given below:

Function

Returns

Description

Called From

encrypt(msg, key, vig_square)

coded_msg

Using the key, encode the message

and return the encoded message

main

decrypt(coded_msg, key, vig_square)

decoded_msg

Using the key, decode the encoded message and return the original message in lowercase

main

create_vig_square ()

vig_square

Setup the Vigenere square matrix initialized with lower case letters: See the example above.

main

get_col_index(msg_char, vig_square)

col_index

Return the column index of the character, ch, in the message to be encoded

encrypt

get_row_index(key_char, vig_square)

row_index

Return the row index of the next character, keyCh, in the key

encrypt

decrypt

get_plain_text_char(

coded_char, key_char, vig_square)

plain_text_char

Return the plaintext character corresponding to the ciphertext character from the square

decrypt

main()

Be sure to do Steps 1 and 2 in this order:

1. Prompt the user to enter the plaintext message; convert to lowercase

2. Prompt the user to enter the key; convert the message and key to lowercase.

3. Setup up the Vigenere square

4. Encrypt the plaintext message using the Vigenere square

5. Display encoded ciphertext message

6. Decrypt the ciphertext message, returning a lowercase version of the original message

7. Display the lowercase decoded plaintext message

system

Use Sample Output Below as Guide

Enter a multi-word message with punctuation:

The eagle has landed.

Enter a single word key with no punctuation:

LINKED

The encoded message is:

epr oejwm ukw olvqoh.

The decoded message is:

the eagle has landed.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions