In: Computer Science
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU!
In this programming assignment, you will write functions to encrypt and decrypt messages using simple substitution ciphers. Your solution MUST include:
encode will return a string representing the ciphertext.
decode will return a string representing the plaintext.
Copy and paste the following statements into your file as the first two statements of your main program. These lines represent Python lists of messages for you to encode and decode to test the functions you write.
plaintextMessages = [ ["This is the plaintext message.", "bcdefghijklmnopqrstuvwxyza"], ["Let the Wookiee win!", "epqomxuagrdwkhnftjizlcbvys"], ["Baseball is 90% mental. The other half is physical.\n\t\t- Yogi Berra", "hnftjizlcbvysepqomxuagrdwk"], ["I used to think I was indecisive, but now I'm not too sure.", "mqncdaigyhkxflujzervptobws"], ["Einstein's equation 'e = mc squared' shows that mass and\n\t\tenergy are interchangeable.", "bludcmhojaifxrkzenpsgqtywv"] ] codedMessages = [ ["Uijt jt uif dpefe nfttbhf.", "bcdefghijklmnopqrstuvwxyza"], ["Qnhxgomhqm gi 10% bnjd eho 90% omwlignh. - Zghe Xmy", "epqomxuagrdwkhnftjizlcbvys"], ["Ulj njxu htgcfj C'gj jgjm mjfjcgjt cx, 'Ep pej jyxj veprx rlhu\n\t\t uljw'mj tpcez jculjm'. - Mcfvw Zjmghcx", "hnftjizlcbvysepqomxuagrdwk"], ["M 2-wdme uxc yr kylc ua xykd m qxdlcde, qpv wup cul'v gmtd mlw\n\t\t vuj aue yv. - Hdeew Rdyladxc", "mqncdaigyhkxflujzervptobws"] ]
You may alter the spacing or indentation of the two lines to conform to the rest of your code, but you are not allowed to change the strings or structure of the lists.
plaintextMessages is a list consisting of five items. Each item is a list of two strings corresponding to a plaintext message and a key. For each of these five items, you should:
If you have done this correctly, the output from your program for the first data item should look like the following:
plaintext: This is the plaintext message. encoded: Uijt jt uif qmbjoufyu nfttbhf. re-decoded: This is the plaintext message.
Then print a blank line to separate this block of three lines from the next block.
codedMessages is a list consisting of four items. Each item is a list of two strings corresponding to a ciphertext message and a key. For each of these four items, you should:
If you have done this correctly, the output from your program for the first data item should look like the following:
encoded: Uijt jt uif dpefe nfttbhf. decoded:
Then print a blank line to separate this block of two lines from the next block.
Special notes:
Given below is the code for the question. PLEASE MAKE SURE
INDENTATION IS EXACTLY AS SHOWN IN IMAGE.
Please do rate the answer if it helped. Thank you.
def encode(key, plaintext):
ciphertext = ''
for c in plaintext:
if c >= 'A' and c <=
'Z':
idx = ord(c) -
ord('A')
c =
key[idx]
#change to upper
case
c = chr(ord(c) -
ord('a') + ord('A'))
elif c >= 'a' and c <=
'z':
idx = ord(c) -
ord('a')
c =
key[idx]
ciphertext = ciphertext + c
return ciphertext
def decode(key, ciphertext):
plaintext = ''
for c in ciphertext:
if c.isalpha():
idx =
key.index(c.lower())
if c >= 'A'
and c <= 'Z':
c = chr(ord('A') + idx)
elif c >= 'a'
and c <= 'z':
c = chr(ord('a') + idx)
plaintext = plaintext + c
return plaintext
def main():
plaintextMessages = [
["This is the plaintext
message.",
"bcdefghijklmnopqrstuvwxyza"],
["Let the Wookiee win!",
"epqomxuagrdwkhnftjizlcbvys"],
["Baseball is 90% mental. The other
half is physical.\n\t\t- Yogi Berra",
"hnftjizlcbvysepqomxuagrdwk"],
["I used to think I was indecisive,
but now I'm not too sure.",
"mqncdaigyhkxflujzervptobws"],
["Einstein's equation 'e = mc
squared' shows that mass and\n\ t\tenergy are
interchangeable.",
"bludcmhojaifxrkzenpsgqtywv"] ]
codedMessages = [
["Uijt jt uif dpefe
nfttbhf.",
"bcdefghijklmnopqrstuvwxyza"],
["Qnhxgomhqm gi 10% bnjd eho 90%
omwlignh. - Zghe Xmy",
"epqomxuagrdwkhnftjizlcbvys"],
["Ulj njxu htgcfj C'gj jgjm
mjfjcgjt cx, 'Ep pej jyxj veprx rlhu\n\t\t uljw'mj tpcez jculjm'. -
Mcfvw Zjmghcx",
"hnftjizlcbvysepqomxuagrdwk"],
["M 2-wdme uxc yr kylc ua xykd m
qxdlcde, qpv wup cul'v gmtd mlw\n\t\t vuj aue yv. - Hdeew
Rdyladxc",
"mqncdaigyhkxflujzervptobws"]
]
for i in range(len(plaintextMessages)):
plaintext =
plaintextMessages[i][0]
key = plaintextMessages[i][1]
ciphertext = encode(key,
plaintext);
print('plaintext:',
plaintext)
print('encoded:', ciphertext)
print('re-decoded:', decode(key,
ciphertext))
print()
for i in range(len(codedMessages)):
ciphertext =
codedMessages[i][0]
key = codedMessages[i][1]
plaintext = decode(key,
ciphertext);
print('encoded:', ciphertext)
print('decoded:', plaintext)
print()
if __name__ == "__main__":
main()
output
----
plaintext: This is the plaintext message.
encoded: Uijt jt uif qmbjoufyu nfttbhf.
re-decoded: This is the plaintext message.
plaintext: Let the Wookiee win!
encoded: Wmz zam Bnndgmm bgh!
re-decoded: Let the Wookiee win!
plaintext: Baseball is 90% mental. The other half is
physical.
- Yogi Berra
encoded: Nhxjnhyy cx 90% sjeuhy. Ulj puljm lhyi cx qlwxcfhy.
- Wpzc Njmmh
re-decoded: Baseball is 90% mental. The other half is
physical.
- Yogi Berra
plaintext: I used to think I was indecisive, but now I'm not too
sure.
encoded: Y prdc vu vgylk Y omr ylcdnyrytd, qpv luo Y'f luv vuu
rped.
re-decoded: I used to think I was indecisive, but now I'm not too
sure.
plaintext: Einstein's equation 'e = mc squared' shows that mass
and
\ t energy are interchangeable.
encoded: Cjrpscjr'p cegbsjkr 'c = xu pegbncd' poktp sobs xbpp
brd
\ s crcnhw bnc jrscnuobrhcblfc.
re-decoded: Einstein's equation 'e = mc squared' shows that mass
and
\ t energy are interchangeable.
encoded: Uijt jt uif dpefe nfttbhf.
decoded: This is the coded message.
encoded: Qnhxgomhqm gi 10% bnjd eho 90% omwlignh. - Zghe
Xmy
decoded: Confidence is 10% work and 90% delusion. - Tina Fey
encoded: Ulj njxu htgcfj C'gj jgjm mjfjcgjt cx, 'Ep pej jyxj
veprx rlhu
uljw'mj tpcez jculjm'. - Mcfvw Zjmghcx
decoded: The best advice I've ever received is, 'No one else knows
what
they're doing either'. - Ricky Gervais
encoded: M 2-wdme uxc yr kylc ua xykd m qxdlcde, qpv wup cul'v
gmtd mlw
vuj aue yv. - Hdeew Rdyladxc
decoded: A 2-year old is kind of like a blender, but you don't have
any
top for it. - Jerry Seinfeld