Question

Write the code in python that implements the Caesar Encryption algorithm according to the following specs:...

Write the code in python that implements the Caesar Encryption algorithm according to the following specs:
1. The algorithm is implemented in a class called “CaesarEncriptor”
2. This class has two methods:
a. Constructor: takes the shift value
b. encode: a method that takes a text message and returns an encrypted message
c. decode: a method that takes an encrypted message and returns a readable message.
d. set_shift: a method that takes a shift value
e. create_shift_substitutions: this is a private method that returns a tuple of 2 dictionaries (one for encryption and other for decryption)

3. The encoding handles both UPPER and lower case letter sets.

A sample of test code that your code should be able to run is:
caesar = CaesarEncryptor(4)  
enc_msg = caesar.encode(“Let’s meet!”)
print( enc_msg)   # send this message to your friend to decrypt
# -- supposed you received a message from your friend, write the code that tries to crack it here?

*NOTE:* modify the code below to satisfy the requirements.

import string

def encode(message, subst):
cipher = ""
for letter in message:
      if letter in subst:
          cipher += subst[letter]
      else:
          cipher += letter
   
return cipher

# Begin auto test

def create_shift_substitutions(n):
    encoding = {}
    decoding = {}
    alphabet_size = len(string.ascii_uppercase)
    for i in range(alphabet_size):
        letter       = string.ascii_uppercase[i]
        subst_letter = string.ascii_uppercase[(i+n)%alphabet_size]

        encoding[letter]   = subst_letter
        decoding[subst_letter] = letter
    return encoding, decoding

test_message = "TEST"
subst, unsubst = create_shift_substitutions(10)

print( encode(test_message)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.

#code

import string


# CaesarEncryptor class
class CaesarEncryptor:
    # constructor taking shift value
    def __init__(self, shiftValue):
        # removing sign of shiftValue and performing modulo division by 26 to return a value between
        # 0 and 25, storing it as shift value
        self.__shift = abs(shiftValue) % 26

    # private method to create substitution dicts
    def __create_shift_substitutions__(self):
        # creating two dicts, for storing substitution characters for encryption and decryption
        enc_sub = dict()
        dec_sub = dict()
        # looping from 0 to 25
        for i in range(26):
            # adding self.__shift to i and wrapping around to get index of encrypted character
            enc_index = (i + self.__shift) % 26
            # subtracting self.__shift from i and wrapping around to get index of decrypted character
            dec_index = i - self.__shift
            if dec_index < 0:
                dec_index = 26 + dec_index
            # adding to enc_sub, the lower case character at index i as key, the lower case character at
            #  enc_index i as the value
            enc_sub[string.ascii_lowercase[i]] = string.ascii_lowercase[enc_index]
            # adding to enc_sub, the upper case character at index i as key, the upper case character at
            #  enc_index i as the value
            enc_sub[string.ascii_uppercase[i]] = string.ascii_uppercase[enc_index]
            # adding to dec_sub, the lower case character at index i as key, the lower case character at
            #  dec_index i as the value
            dec_sub[string.ascii_lowercase[i]] = string.ascii_lowercase[dec_index]
            # adding to dec_sub, the upper case character at index i as key, the upper case character at
            #  dec_index i as the value
            dec_sub[string.ascii_uppercase[i]] = string.ascii_uppercase[dec_index]
        # finally returning both dicts
        return enc_sub, dec_sub

    # method to encode a text and return encoded text
    def encode(self, message):
        # calling __create_shift_substitutions__ method, getting first dict returned as substitution dict
        enc_sub = self.__create_shift_substitutions__()[0]
        # initializing an empty string
        encoded_text = ""
        # looping through each char in message
        for c in message:
            # if c is in enc_sub, appending corresponding value from enc_sub to encoded_text
            if c in enc_sub:
                encoded_text += enc_sub[c]
            # otherwise appending c to encoded_text
            else:
                encoded_text += c
        # returning encoded text
        return encoded_text

    # method to decode a text and return decoded text
    def decode(self, message):
        # calling __create_shift_substitutions__ method, getting second dict returned as substitution dict
        dec_sub = self.__create_shift_substitutions__()[1]
        # initializing an empty string
        decoded_text = ""
        # looping through each char in message
        for c in message:
            # if c is in dec_sub, appending corresponding value from dec_sub to decoded_text
            if c in dec_sub:
                decoded_text += dec_sub[c]
            # otherwise appending c to decoded_text
            else:
                decoded_text += c
        # returning decoded_text
        return decoded_text

    # method to set shift value
    def set_shift(self, shift):
        # removing sign and wrapping around to set a shift value between 0 and 25
        self.__shift = abs(shift) % 26


# testing

# creating CaesarEncryptor object with shift value 4
caesar = CaesarEncryptor(4)
# encoding a text
enc_msg = caesar.encode("Let's meet!")
# decoding it
dec_msg = caesar.decode(enc_msg)
# displaying encoded and decoded text
print(enc_msg)
print(dec_msg)

'''
Note: supposed you received a message from your friend and you want to crack the message, initially you have to create
a CaesarEncryptor object with shift 0, then call decode method once, to see if the returned message makes sense. If not
increment the shift key, and try again. try all the shift values between 0 and 25, and one of them will return the plain
message if it is encrypted using CaesarEncryptor
'''

#output

Pix'w qiix!
Let's meet!

Add a comment
Know the answer?
Add Answer to:
Write the code in python that implements the Caesar Encryption algorithm according to the following specs:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT