Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

***IN PYTHON*** In cryptography, a simple substitution cipher is a method of encryption in which a symbol in the original message ( plaintext ) is

***IN PYTHON***

In cryptography, a simple substitution cipher is a method of encryption in which a symbol in the original message (plaintext) is replaced with a single coded symbol (ciphertext) according to a fixed system. The receiver of the message deciphers the text by performing the inverse substitution.

One easy example of a simple substitution cipher is to replace every letter in the message with the one that immediately follows it in the alphabet; in this way, the plaintext hello becomes the ciphertext ifmmp because h maps to i, e maps to f, l maps to m, and o maps to p. To decrypt this message, you perform the inverse operation, which is to replace each letter in the ciphertext ifmmp with the letter that immediately precedes it in the alphabet instead.

We describe the encryption method by defining a key, a string of 26 letters that identifies what the ciphertext character is for each character in the string "abcdefghijklmnopqrstuvwxyz", in order. Thus for the above cipher, the key would be

 "bcdefghijklmnopqrstuvwxyza" 

However, this example is extremely simple to detect, encode, and decode without even using a computer. If you know that this key was being used for encryption, you can probably decrypt the message "J'n tp tnbsu" in your head.

A better encryption scheme for a simple substitution cipher would be to provide a mapping for all 26 letters of the alphabet that doesn't necessarily follow any pattern. For example, if we used the key:

 "zvcrmysepndwibjgqhxfaokltu" 

Then encrypting the message hello using this key would give us the ciphertext emwwj, which is a lot harder to figure out in your head. However, it's still pretty easy if you use a computer program to do the mapping for 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. \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 \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 \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 \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().

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2022 Grenoble France September 19 23 2022 Proceedings Part 4 Lnai 13716

Authors: Massih-Reza Amini ,Stephane Canu ,Asja Fischer ,Tias Guns ,Petra Kralj Novak ,Grigorios Tsoumakas

1st Edition

3031264118, 978-3031264115

More Books

Students also viewed these Databases questions

Question

Define Decision making

Answered: 1 week ago

Question

What are the major social responsibilities of business managers ?

Answered: 1 week ago

Question

What are the skills of management ?

Answered: 1 week ago

Question

1 What demand is and what affects it.

Answered: 1 week ago