Problem 1.2: Encryption Extensions (100pts)
There are two subclasses that extend the encryption methods:
- Multiple Encryption: The constructor takes an integer
n
, and theencrypt
method applies the chosen encryption methodn
times on the input string. - Split Encryption: The constructor takes a number
x
and theencrypt
method, then extracts characters from positions that are multiples ofx
(excluding 0), concatenates them, and appends them to the original string before applying the encryption.
Both subclasses of the encryption extensions have a decorator
method that takes a encrypt
function and a string, applies the function to the string in the above manner, and returns the result.
class extensions:
"""The base class for encryption extensions."""
def decorator(self, function, message):
"""Apply the function to the message."""
pass
class multipleencryption(extensions):
"""A class for multiple encryption extension.
>>> cipher = shiftcipher(3)
>>> extension = multipleencryption(2)
>>> extension.decorator(cipher.encrypt, 'hello')
'nkrru'
>>> extension.decorator(cipher.encrypt, 'world')
'cuxrj'
"""
def __init__(self, counts=1):
"""Initialize the multiple encryption 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 splitencryption(extensions):
"""A class for split encryption 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 encryption method.
>>> cipher = shiftcipher(3)
>>> extension = splitencryption(2)
>>> extension.decorator(cipher.encrypt, 'hello')
'khoor'
>>> extension.decorator(cipher.encrypt, 'world')
'zroug'
"""
def __init__(self, x):
"""Initialize the split encryption 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 ***"
Now we can finally finish the encryption system by implementing the classes above. Congratulations!
class encryption:
"""A class for encryption.
>>> cipher = shiftcipher(3)
>>> extension = multipleencryption(2)
>>> encrypt = encryption(cipher, extension)
>>> encrypt.encrypt('hello')
'nkrru'
>>> encrypt.encrypt('world')
'cuxrj'
"""
def __init__(self, method, extension):
"""Initialize the encryption with a method and an extension."""
"*** YOUR CODE HERE ***"
def encrypt(self, message):
"""Encrypt the message using the method and extension."""
"*** YOUR CODE HERE ***"