PYTHON
CRYPTOGRAPHY – SUBSTITUTION CIPHER

 

 

DESCRIPTION


You will write code related to a substitution cipher that can be used in encrypting/decrypting messages.


A FEW DEFINITIONS


Cryptography is the study of creating, analyzing and deciphering coded messages.

 

A cipher is a method used to encrypt and decrypt a message.  The simplest cipher that you may have used in elementary school to share notes with friends is a substitution cipher. 

 

A substitution cipher is when each letter is replaced by another letter (or symbol) when encrypted.  More advanced versions of this could also include swapping multiple letters at a time, but we will focus on single-letter substitutions here.

 

A simple substitution cipher can be created by simply mapping each letter to another.

Example
Below is an example of a letter to letter mapping for four letters:

 

 

The message abc would be encrypted to dab. 


In cryptography, the original message is often refered to as the plaintext and the encrypted message, as the ciphertext.

 

WORK

 

QUESTION 1

A) Copy the code below into a Python file called Substitution.py.  Test it.  Take time to understand how it works. 

 

#substitution cipher mapping

map1 = "abcdefghijklmnopqrstuvwxyz"

map2 = "zaifcdeghbkjlmnxopqsrwtuvy"

 

 

def enc(word):

    word2 = ""

    for letter in word:

        index = map1.index(letter)

        letter2 = map2[index]

        word2 = word2 + letter2

    return word2

 

 

print(enc("campeau"))

 

B) Add the function named dec (which is short for decrypt) that reverses the process of the enc (which is short for encrypt).  This should be very similar to the enc function. 

 

Test it out by checkin if

 

print(dec(enc("campeau")))

 

outputs campeau as expected.

 

C) The following mapping is the Campoz cipher.  Alter the code so that your program works with the Campoz cipher.

 

A -> C

B -> A
C -> M
D -> P
E -> O
F -> Z
G -> B
H -> D
I -> E
J -> F
K -> G
L -> H

M -> I

N -> J

O -> K

P -> L

Q -> N

R -> Q

S -> R

T -> S

U -> T

V -> U

W -> V

X -> W

Y -> X

Z -> Y

 

 

QUESTION 2

The following sentence has been encrypted using the Campoz cipher.  Use your program to decrypt it.

 

ICXSDOZKQMOAOVESDXKT