Question

WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...

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:

  • a function called encode that takes two parameters:
    • key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order;
    • plaintext, a string of unspecified length that represents the message to be encoded.

    encode will return a string representing the ciphertext.

  • a function called decode that takes two parameters:
    • key, a 26-character long string that identifies the ciphertext mapping for each letter of the alphabet, in order;
    • ciphertext, a string of unspecified length that represents the message to be decoded.

    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:

  • print the plaintext message.
  • encode the message and print the ciphertext.
  • decode the ciphertext message you just calculated and print the plaintext message again. The purpose of this is to demonstrate that your encode and decode functions work properly as inverses of each other.

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:

  • print the ciphertext message.
  • decode the message and print the ciphertext.

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:

  • Encrypted and decrypted lower-case letters should map to lower-case letters, as defined by the key.
  • Encrypted and decrypted upper-case letters should map to upper-case letters. Note that upper-case letters do not appear in the key! (Look at the first character in the expected output shown above: the upper-case "T" maps to an upper-case "U" in the encoded message.) Your program must detect when an upper-case letter appears in the data, and figure out how to convert it to the correct upper-case letter based on the corresponding lower-case letters in the key.
  • Non-alphabetic characters, such as numbers, spaces, tabs, punctuation, etc. should not be changed by either encode() or decode().
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def encode( key, message):
# Create the running alphabet string
alphabetlist = "abcdefghijklmnopqrstuvwxyz"

# Create an empty string where the encoded message is to be stored.
encmessage = ""
# We walk through each character of the message and find its index in the alphabetlist string.
# If it is not present in that, it will return -1 and we copy the character as it is to the
# encoded message. While finding the index we convert it to lower case because the key and alphabetlist
# are lower case. We use python string find method to find the index.
for ch in message:
idx = alphabetlist.find(ch.lower())
# Once index is found, we will fetch the character at the same index from the key string. This is
# the encoded character and check if it was uppercase or lowercase before and convert the case correctly
# and add it to the encoded message.
if idx != -1:
if ch.isupper():
encmessage = encmessage + key[idx].upper()
else:
encmessage = encmessage + key[idx]
# This is the case where the character is a non alphabet and we add it as it is
else:
encmessage = encmessage + ch

return encmessage

def decode( key, message):
# Create the running alphabet string
alphabetlist = "abcdefghijklmnopqrstuvwxyz"

# Create an empty string where the decoded message is to be stored.
decmessage = ""
# We walk through each character of the message and find its index in the key string since it is already encoded.
# If it is not present in that, it will return -1 and we copy the character as it is to the
# encoded message. While finding the index we convert it to lower case because the key and alphabetlist
# are lower case. We use python string find method to find the index.
for ch in message:
idx = key.find(ch.lower())
# Once index is found, we will fetch the character at the same index from the alphabet string to find the decoded character. This is
# the decoded character and check if it was uppercase or lowercase before and convert the case correctly
# and add it to the decoded message.
if idx != -1:
if ch.isupper():
decmessage = decmessage + alphabetlist[idx].upper()
else:
decmessage = decmessage + alphabetlist[idx]
# This is the case where the character is a non alphabet and we add it as it is
else:
decmessage = decmessage + ch

return decmessage

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"] ]

# We loop through the plaintext message call the encode and decode functions and print message accordingly
# We use python string formatting to achieve the desired output.
for msg in plaintextMessages:
print("plaintext: {}".format(msg[0]))
encmessage = encode(msg[1],msg[0])
print("encoded: {}".format(encmessage))
decmessage = decode(msg[1],encmessage)
print("re-decoded: {}".format(decmessage))
print("")

for msg in codedMessages:
print("encoded: {}".format(msg[0]))
decmessage = decode(msg[1],msg[0])
print("decoded: {}".format(decmessage))
print("")

Since the program was long attached the split screenshots and output.

Add a comment
Know the answer?
Add Answer to:
WE ARE USING PYTHON TO COMPLETE THIS ASSIGNMENT :) THANK YOU! In this programming assignment, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need some help doing and writing this project in python. The purpose of this assignment is...

    Need some help doing and writing this project in python. The purpose of this assignment is to assess your ability to Evaluate properties of modular arithmetic relations Utilize modular arithmetic to encrypt and decrypt plain text Research the Vigenere cipher. Write a program that contains an encode and a decode function. The encode function should take a plaintext message and a key. It should return the encoded message. The decode function should take the encoded message and the key and...

  • Q16-Decoder (0.5 points) Write a code block to decode strings from secret encodings back to reada...

    Q16-Decoder (0.5 points) Write a code block to decode strings from secret encodings back to readable messages. To do so: Initialize a variable called decoded as an empty string . Use a for loop to loop across all characters of a presumed string variable called encoded Inside the loop, convert the character to another character o Get the unicode code point for the character (using ord o Subtract the value of key to that code point (which should be an...

  • In C++ Having heard you have gotten really good at programming, your friend has come to...

    In C++ Having heard you have gotten really good at programming, your friend has come to ask for your help with a simple task. She would like you to implement a program to encrypt the messages she exchanges with her friends. The idea is very simple. Every letter of the alphabet will be substituted with some other letter according to a given key pattern like the one below. "abcdefghijklmnopqrstuvwxyz" "doxrhvauspntbcmqlfgwijezky" // key pattern For example, every 'a' will become a...

  • Can someone please help me for this assignment? Cryptography — the science of secret writing —...

    Can someone please help me for this assignment? Cryptography — the science of secret writing — is an old science; the first recorded use was well before 1900 B.C. An Egyptian writer used previously unknown hieroglyphs in an inscription. We will use a simple substitution cypher called rot13 to encode and decode our secret messages. ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the 13th letter after it, in...

  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two...

    Using Python; Caesar part of the homework: Write c_encrypt() and c_decrypt(), both of which take two arguments, the first one a string and the second one an integer key. Both should return a string. Vigenère part of the homework: Write vig_encrypt() and vig_decrypt() functions. Each takes two strings as inputs, with the first being the plaintext/ciphertext, and the second being the key. Both should be calling functions you wrote earlier to help make the work easier. The key will be...

  • create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The...

    create a Java class ShiftCipher The program ShiftCipher should take two inputs from the terminal. The first should be a string of any length which contains any type of symbol (the plaintext). The second will be a shift value which should be between 0 and 25 inclusive (though you may design your program to be resilient to shifts beyond this value). The program should print the cipher text, in which each of the alphabetic characters in the string is shifted...

  • Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple...

    Caesar Cipher v3 Decription Description A Caesar cipher is one of the first and most simple encryption methods. It works by shifting all letters in the original message (plaintext) by a certain fixed amount (the amounts represents the encryption key). The resulting encoded text is called ciphertext. Example Key (Shift): 3 Plaintext: Abc Ciphertext: Def Task Your goal is to implement a Caesar cipher program that receives the key and an encrypted paragraph (with uppercase and lowercase letters, punctuations, and...

  • Cryptography, the study of secret writing, has been around for a very long time, from simplistic...

    Cryptography, the study of secret writing, has been around for a very long time, from simplistic techniques to sophisticated mathematical techniques. No matter what the form however, there are some underlying things that must be done – encrypt the message and decrypt the encoded message. One of the earliest and simplest methods ever used to encrypt and decrypt messages is called the Caesar cipher method, used by Julius Caesar during the Gallic war. According to this method, letters of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT