Question: The Substitution Cipher A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (i.e. scrambled). Ciphers were an early form of security

The Substitution Cipher

A cipher is mirrored algorithm that allow phrases or messages to be obfuscated (i.e. "scrambled"). Ciphers were an early form of security used to send hidden messages from one party to another. The most famous and classic example of a cipher is the Caesar Cipher. This cypher worked by shifting each letter in a message over by a certain number of times. For example, in a cipher shift of 4, any "a" would be changed to "e", "b" to "f", "c" to "g", and so on. Once the messaged reached the recipient, they would reverse the shift to recover the original message.

The Caesar Cipher is a simplistic and easily breakable algorithm, however. Depending on the language used, the total number of possible encodings is equal to the number of letters/symbols in that language. (in the case of the English Alphabet, 26). To improve this, we are going to look at and implement a Randomized Substitution Cipher. In a randomized substitution, the mappings for each character (letter, digit, or punctuation) are randomly mapped to another character (i.e. a letter, digit or punctuation). For example, let us say our alphabet has A, T, C, K, D, W, N, 0, 1, *, and & for a set. One possible mapping of a plaintext character to a ciphertext character could be as followed (we are going to call this association a cipher-key):

A to N

C to *

T to 1

K to C

W to A

N to &

0 to T

1 to W

* to K

& to 0

Using this cipher, encrypt the message "ATTACK&":

A T T A C K &

N 1 1 N * C 0

To create this, we will need to generate a map for each character. One way we can solve this is by using a dictionary. Remember, a dictionary is a type of sequence where each item in the dictionary is associated with a key, where keys are typically strings. The items, called values, are accessed using their identifying key. For our cipher, we could use the plain text character as the key and the ciphertext character as the value. If we were to use a dictionary to make cipher-key for the example above:

cipher_key = {'A': 'N', 'C':'*', 'T':'1', 'K':'C', 'W':'A', 'N':'&', '0':'T', '1':'W', '*':'K', '&':'0'}

We could then use the subscript operator to determine what the plain text character would change to in the ciphertext. (i.e. cipher_key[plaintext_character]). If we wanted to revert to the original message (i.e. decrypt), we would have to find a way to match every value with a key in the dictionary. Think about how this could be achieved.

Your program should be able to achieve the following (The rubric points will map to each of these criteria):

***Criteria 1: Retrieve a plaintext input from a file. Use any text file, for example, lyrics of a song. (How would you import a text file as a string?)

***Criteria 2: Generate a randomized cipher-key for this file. This should its own function that returns the cipher key as a dictionary.

***Criteria 3: Use the generated cipher-key to make an encoding of the original message. You should make a new string for holding the encoded message and concatenation for building the string. This should be its own function that returns the encoded message.

***Criteria 4: Output the generated message ciphertext.

***Criteria 5: Use the generated cipher-key to "revert back" to the original plain text message. This should be its own function that returns the decoded plain message.

***Criteria 6: Output the conversion back to the plaintext message (i.e. do NOT output the original string, but the string you get when you convert back from ciphertext).

Additional Notes:

1. For Criteria 2, you can make a list of scrambled letters by using the following algorithm:

a. Make a list of all the characters by using Python's "list()" function on the printable string. (See Appendix A and B).

b. Shuffle the list by using Pythons "shuffle()" function on the list.

c. The original list of printable characters and the new shuffled list are in parallel. Make a dictionary for the cipher key where the dictionary keys would be characters in the original list and the values for each of the key will be the characters in the shuffled list, in the order of their index.

2. For the functions in 3 and 5: Think of the relationship between encoding and decoding the message. The process for decoding is the reverse of the process for encoding, which can help in creating the two functions.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!