Question
Goal : Write a P ython program plaifair .py as follows: def playfair(key, plaintext): # your code here if __name__ == __main__: # your code
Goal
:
Write
a P
ython program
plaifair
.py
as follows:
def
playfair(key, plaintext):
#
your code here
if __name__ == "__main__":
# your code here
The
conditional statement above
allows
the program
to be
ran
directly.
STEP
A
-
C
ompute
and print the encryption tab
le
1
-
Create
a 5x5 table as a list of lists
.
2
D
efine a function called
init
_table()
which
initializes the table
with stars
. This function does not return anything
.
3
Print out the table. The output should look like this:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
4
Define a function called
table_has
(
letter
)
which checks if a
letter already exists in the table
. This function should return true
or false depending on
whether
the letter exists.
5
Define a function
clean_key(key)
which changes
the secret key
to
uppercase and replace I by J, then returns the clean key
.
6
Define a function
set_cell(letter)
whic
h sets a table cell to a
specific letter.
7
Define a function
create
_table(key)
to
populate
the encryp
tion
table given the secret key.
This function should:
-
I
nitialize the table
.
-
C
lean the secret key
.
-
Build
the table using the set_cell
()
and table_has
()
function
s
.
8
out the encryption table.
STEP
B
Encrypt
your
secret message
1
D
efine
a functio
n
find_letter(letter)
which
returns
the row and
column of the letter in the table.
2
-
D
efine a
function
encode_pair(a,
b)
which takes
a pair of letters
a and b,
runs t
he encoding rules
and returns the encoded pair as
follows:
-
AB
-
> LF (sample)
-
F
unction should print an error message if a equals b
and stop
program execution.
Suggested approach
:
-
C
ompute
a_row, a_col, b_row, b_col
using the find_letter
function
.
-
U
se an if
-
elif
-
else statement to compute a_new_row, a_new_col,
b_new_row
, b_new_col
based on the encryption rules
.
-
Optional: To make your code cleaner, y
ou may want to create
separate
encoding
functions for the 3 different
cases:
same
row
(
with
wrapping
)
, same column
(
with
wrapping
)
, and rectangle
.
3
D
efine a function
cleanup(plaintext)
which
return
s
the cleaned up
message as follows
:
-
Change
s
it to uppercase
.
-
Remove
s
spaces
.
-
Replaces J by I
.
-
Places a letter X between 2
consecutive repeating letters
as
follows:
o
EE
-
> EXE
-
I
f the repeated
letter
happen to be an
X, insert the letter Q
in between as follows:
o
XX
-
> XQX
-
If the length of the resulting text is an
add
number, add the
letter Z
at the end as follows:
o
..G
-
> ..GZ
-
If the last letter happens to be a Z
, add
the letter Q at the
end as follows:
o
..Z
-
> ..ZQ
Su
ggested appro
ach
:
Note that fixing a problem may lead to an new
one
. E.g. insert
ing
an
X may lead to
a
plaintext with an odd
length. Therefore, you should
c
r
eate a while loop which will
repeatedly
call a function to
fix
issue
s
and return a
new
plaintext until no more issues are found.
STEP C
Encrypt your secret message
1
Define
a function
encrypt
(
plaintext
)
which encode
s
the message
plaintext
and
returns the cipher
text.
Make
sure
to insert a space
every 5 letters
in the resulting ciphertext
(e.g. KLDGD NXSLO CWGQH
KUINF ACWER)
2
Execute and print the ciphertext. The execution phase should:
a.
Prompt for the secret key
.
b.
Prints the secret key as
entered.
c.
Prompt for the secret message (plaintext)
.
d.
Prints the secret message as
entered.
e.
Computes and p
rints the encrypted message (ciphertext)
.
f.
Asks if
user
want
s
to encrypt another message
o
If yes, program goes back to step a
.
o
If no, program stops
ex
ecution.
STEP D
Extra
-
Credit
Create a
function
decrypt
(key, ciphertext)
which takes two parameters,
the secret and the ciphertext and returns the plaintext.
THE SUBMISSION
Submit a playfair.txt
or py
file as follows:
-
Program should be structure as
described in this project.
-
A
ll functions
should have the same names as indicated above. T
hey
may be tested separately for partial credit in case your program
fails.
-
All functions have a comment above them explaining their purpose.
-
Submission should be free
from debugging code or test code.
TESTIN
G YOUR CODE
Not to include with you
r
submission
Create a Python program called test_playfair.py in and place it in the
same directory as your main program. The code should import your
encryption code and then
run some tests as follows:
import playfair
def
test(key, plain, expected):
cipher = playfair.playfair(key, plain)
if
cipher != expected:
print("%s : %s != %s" % (plain, cipher, expected))
# execute test:
test(mysecretkey, this is my secret message,
WQIUW QOKSG ADAJH
AOIQU WIQUW
)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started