Question
Either modify or rewrite the Caesar Shift code that is provided in the resources to handle the following: # Copyright 2013, Michael H. Goldwasser #
Either modify or rewrite the Caesar Shift code that is provided in the resources to handle the following:
# Copyright 2013, Michael H. Goldwasser
#
# Developed for use with the book:
#
# Data Structures and Algorithms in Python
# Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser
# John Wiley & Sons, 2013
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
class CaesarCipher:
"""Class for doing encryption and decryption using a Caesar cipher."""
def __init__(self, shift):
"""Construct Caesar cipher using given integer shift for rotation."""
encoder = [None] * 26 # temp array for encryption
decoder = [None] * 26 # temp array for decryption
for k in range(26):
encoder[k] = chr((k + shift) % 26 + ord('A'))
decoder[k] = chr((k - shift) % 26 + ord('A'))
self._forward = ''.join(encoder) # will store as string
self._backward = ''.join(decoder) # since fixed
def encrypt(self, message):
"""Return string representing encripted message."""
return self._transform(message, self._forward)
def decrypt(self, secret):
"""Return decrypted message given encrypted secret."""
return self._transform(secret, self._backward)
def _transform(self, original, code):
"""Utility to perform transformation based on given code string."""
msg = list(original)
for k in range(len(msg)):
if msg[k].isupper():
j = ord(msg[k]) - ord('A') # index from 0 to 25
msg[k] = code[j] # replace this character
return ''.join(msg)
if __name__ == '__main__':
cipher = CaesarCipher(3)
message = "THE EAGLE IS IN PLAY; MEET AT JOE'S."
coded = cipher.encrypt(message)
print('Secret: ', coded)
answer = cipher.decrypt(coded)
print('Message:', answer)
The Shift Alphabet = a - z, A-Z, 0-9. (62 characters)
Prompt the user for a message.
randomly assign the shift number from 00 - 61, and embed this as the first two characters in the encrypted message.
Encrypt the message. You may use the same punctuation marks.
write the encrypted message out to a file name encrypt.txt
Write the Decryption algorithm
* Read in the data from the encrypt.txt file
* apply the decypher key and decrypt the program.
* print the message out to the screen
Hint: I would hard code a shift for the first two symbols of the file which will be the randomly generated number of the shift. After you write the first two characters to a file reapply the shift to the rest of the file with the random number. This way the encryption routine is totally encrypted. Just remember to reverse the first two hard-coded numbers when you read the file then apply the shift to decode the message as you read it from the file.
You may write the program where it is menu-based so that you can either encrypt or decrypt from within the same program.
Step by Step Solution
3.50 Rating (147 Votes )
There are 3 Steps involved in it
Step: 1
Modified version of the Caesar Cipher code import random class CaesarCipher def initself shift selfs...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started