Problem 3: Decryption (0pts)
In fact, we can construct a decryption system using exactly the same method as the encryption system. Isn't that cool? Give it a try!
To test your code, you just need to randomly generate some test data using your already written encryption system, then test it in the decryption system in reverse. If you get the original message, then your decryption system works perfectly!
class dmethods:
"""The base class for decryption methods."""
def decrypt(self, message):
"""Decrypt the message."""
pass
class shiftdecipher(dmethods):
"""A class for shift cipher decryption method.
>>> cipher = shiftdecipher(3)
>>> cipher.decrypt('khoor')
'hello'
>>> cipher.decrypt('zruog')
'world'
"""
def __init__(self, shift):
"""
Initialize the shift cipher with a shift.
If shift is greater than 26, shift = shift % 26.
"""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message by shifting each character by the shift."""
"*** YOUR CODE HERE ***"
class dictionarydecipher(dmethods):
"""A class for dictionary cipher decryption method.
>>> cipher = dictionarydecipher({'h': 'a', 'e': 'b', 'l': 'c', 'o': 'd', 'w': 'e', 'r': 'f', 'd': 'g'})
>>> cipher.decrypt('abccd')
'hello'
>>> cipher.decrypt('edfcg')
'world'
"""
def __init__(self, dictionary):
"""
Initialize the dictionary cipher with a dictionary.
If a key is not in the dictionary, the value is the key itself.
We promise that any letter will only appear once as a value in the dictionary.
"""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message using the dictionary cipher."""
"*** YOUR CODE HERE ***"
class fencedecipher(dmethods):
"""A class for fence cipher decryption method.
>>> cipher = fencedecipher(3)
>>> cipher.decrypt('hleol')
'hello'
>>> cipher.decrypt('wlodr')
'world'
"""
def __init__(self, rails):
"""Initialize the fence cipher with a number of rails."""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message using the fence cipher."""
"*** YOUR CODE HERE ***"
class extensions:
"""The base class for encryption extensions."""
def decorator(self, function, message):
"""Apply the function to the message."""
pass
class multipledecryption(dextensions):
"""A class for multiple decryption extension.
>>> cipher = shiftdecipher(3)
>>> extension = multipledecryption(2)
>>> extension.decorator(cipher.decrypt, 'nkrru')
'hello'
>>> extension.decorator(cipher.decrypt, 'cuxrj')
'world'
"""
def __init__(self, counts=1):
"""Initialize the multiple decryption extension with a number of times."""
"*** YOUR CODE HERE ***"
def decorator(self, function, message):
"""Apply the function to the message multiple times."""
"*** YOUR CODE HERE ***"
class splitdecryption(dextensions):
"""A class for split decryption extension.
It extracts characters from positions that are multiples of `x` (excluding 0),
concatenates them, and appends them to the original string before applying the decryption method.
>>> cipher = shiftdecipher(3)
>>> extension = splitdecryption(2)
>>> extension.decorator(cipher.decrypt, 'khoor')
'hello'
>>> extension.decorator(cipher.decrypt, 'zroug')
'world'
"""
def __init__(self, x):
"""Initialize the split decryption extension with a number x."""
"*** YOUR CODE HERE ***"
def decorator(self, function, message):
"""Apply the function to the message after splitting the message."""
"*** YOUR CODE HERE ***"
class decryption:
"""A class for decryption.
>>> cipher = shiftcipher(3)
>>> extension = multipleencryption(2)
>>> encrypt = encryption(cipher, extension)
>>> decrypt = decryption(encrypt)
>>> decrypt.decrypt('nkrru')
'hello'
>>> decrypt.decrypt('cuxrj')
'world'
"""
def __init__(self, encryption_instance):
"""Initialize the decryption with an encryption method."""
"*** YOUR CODE HERE ***"
def decrypt(self, message):
"""Decrypt the message using the method and extension."""
"*** YOUR CODE HERE ***"