Python Programming

profileGoals
April28th.docx

Modification Exercise M 01

In Modification Exercises, you will be given working code. You will need to understand the code, and modify it.

This exercise involves encryption & decryption. Below is a diagram that shows that the starting point is  plaintext . This is the readable message. The process of  encryption  creates  ciphertext , a form of the message that is, ideally, NOT readable by anyone except the intended receipient. The ciphertext is transmitted. Once received, it must undergo a process of  decrpytion  which restores the message to its original form: plaintext.

--> encrypt -->

/ \

/ \

plaintext ciphertext

\ /

\ /

<-- decrypt <--

Part I: Monoalphabetic Subsitution Cipher: "Shift" or "Caesar" Cipher

Look at the program in this repository: monosubenc.py.

It implements a simple substitution cipher:

alphaPlain = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '

alphaCiphr = 'GHIJKLMNOPQRSTUVWXYZABCDEF '

Run it and verify that the output is as follows:

plaintext: HELLO WORLD

ciphertext: NKRRU CUXRJ

NOTE: You can verify this just by looking at the alphaPlain & alphaCiphr above.

MODIFY monosubenc.py to create monosubdec.py that starts with the ciphertext NKRRU CUXRJ and decrypts it (to HELLO WORLD).

For example, here is the desired output:

ciphertext: NKRRU CUXRJ

plaintext: HELLO WORLD

IMPORTANT: DO NOT SIMPLY SWITCH THE print() LINES !!! YOU MUST MODIFY THE PROGRAM TO ACTUALLY DO THE DECRYPTION!

You should test your monosubdec.py so that it can produce the above output.

Now, here is a new ciphertext: MUUJ PUH

Use your program to decrypt the new ciphertext and enter its plantext below:

ciphertext: MUUJ PUH

plaintext: <PUT YOUR ANSWER HERE !!!> # Use the pencil, above.

Upload your monosubdec.py here to this repository.

Part II: Monoalphabetic Subsitution Cipher: "Key Sentence"

Consider the sentence: `THE QUICK BROWN FOX JUMPED OVER LAZY DOGS"

Use the given sentence above as the basis for a new substitution cipher, as follows:

alphaPlain = 'ABCDEFGHIJKLMNOPQRSTUVWYYZ '

alphaCiphr = 'THEQUICKBRO...

Complete the alphaCiphr. Each time a letter appears for the first time in the sentence (The quick brown fox jumped over lazy dogs) it is added to the alphaCiphr. Any time a letter appears in the sentence for a second or more time, it is ignored. In this manner, the alphaCiphr is a complete alphabet, but the sequence is NOT known!

Comment-out your first alphaCiphr from Part I and replace it with the one for Part II. As a test, run your monosubenc.py with the following plaintext; it should produce the ciphertext:

plaintext: HELLO WORLD

ciphertext: KUWWX ZXPWQ

Now, using the substitution cipher (based on the sentence), decrypt: GXL IXLFQ BV

ciphertext: GXL IXLFQ BV

plaintext: <PUT YOUR ANSWER HERE !!!> # Use the pencil, above.

Be sure to upload this new version of your monosubdec.py. Yes, use the same name. The git/gitHub revision history will preserve the different versions. Furthermore, your original alphaCiphr and your current one should both be in your program now.

Part III: Describe the role of the dictionary, dict, in these programs

In your own words, please describe the role of the Python dictionary, dict, in these samples of monoalphabetic substitution ciphers.

BEGIN

Push "the pencil" in the upper right, find & erase this text and type your response here over this sentence.

END

'''\

monosubenc.py

Substitution cipher: Encrypt

'''

# the alphabets

alphaPlain = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '

alphaCiphr = 'GHIJKLMNOPQRSTUVWXYZABCDEF '

# initialize the main dictionary

dEncrypt = {}

for i in range(len(alphaPlain)):

dEncrypt[alphaPlain[i]] = alphaCiphr[i]

# print(dEncrypt)

textPlain = 'HELLO WORLD'

print(' plaintext:', textPlain)

# IMPORTANT: In Python, strings, str, are FIXED, and cannot be modified!

# On the other hand: programmers can .append() elements to lists

# So, we build-up the ciphertext using a list, and we will

# convert the list to a string later.

listCipher = []

# run through each character of the plaintext

for a in textPlain:

listCipher.append(dEncrypt[a]) # for each letter in plain, substitute

# Now, convert the listCipher to a string: textCipher

textCipher = ''.join(listCipher)

print('ciphertext:', textCipher)