Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE PYTHON PROGRAMING. File Encryption/Decryption Program: You are to design and

ID: 3871602 • Letter: U

Question

USE PYTHON PROGRAMING.

File Encryption/Decryption Program: You are to design and write a program that allows a user to interactively select between encrypting or decrypting files encrypting a user-specified text-file (.txt extension) using the encryption scheme described below to generatea new encoded file with the same name, except with a .zzz extension. decrypting a user-specified encrypted file (. zzz extension) using a user-specified keyword to generate a new text-file with the same name, except with a .txt extension. * The encryption scheme is a form of substitution cipher based on a user-specified positive integer key and the follow sequence of 78 characters: (Note: blank-space character between the "K" and sequece position: 012345678901234567890123456789012345678901234567890123456789012345678901234567 0 The integer key is used to encrypt only the first letter in the message by "shifting" down the above sequence by that amount. For example, an integer key of 5 shifts 'B' to the encrypted ‘d, Shifting past the right end of the sequence wraps back to the beginning of the sequence. For example, shifting 5 from encrypts as*b. The sequence position of the first letter in the message is used as the shift amount for the second letter in the message If the first letter was a 'B,' then 4 is used to shift the second letter in the message since 'B' is at position 4 (start with 'a' at position 0, 'A' at position 1, etc.). The sequence position of the second letter in the message is used as the shift amount for the third letter, etc. Any message character not in the above sequence (eg, ‘,'f") is copied to the encrypted message without modification with the previous shift amount carrying over to the next letter in the message to be encrypted. Consider the following example with key 5 Four Line Message to Encrypt with Key-5 in file message.txt Encrypted Message in file message.zzz Be by the Union at 6 PM! dF/, z83al/fHvwb& t3RRalg - Sam LJdSm Your program also needs to: (See www.cs.uni.edu/-fienup/cs1520f1 7/homeworks/example_ programs_hw2.zip) validate a correct selection whether to encrypt, decrypt, or exit. validate a correct positive integer for the key validate that the user specified file to encrypt/decrypt exists and force the user to reenter until they specify an existing file. Feel free to make the program more user-friendly by listing all files of the appropriate type .txt or . zzz extensions check if the user-specified file name for the encrypting (or decrypting) exists before opening it for writing. Ifit already exists, ask the user if they are okay with it being wiped out. If they are not, ask them to pick a different file name to receive the encrypted (or decrypted) text. *

Explanation / Answer

def getMode():

     while True:

         print('Do you wish to encrypt or decrypt a message?')

         mode = input().lower()

         if mode in 'encrypt e decrypt d'.split():

             return mode

         else:

             print('Enter either "encrypt" or "e" or "decrypt" or "d".')

def getMessage():

     print('Enter your message:')
     return input()

def getKey():

     key = 0

     while True:

         print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))

         key = int(input())

         if (key >= 1 and key <= MAX_KEY_SIZE):

             return key


def getTranslatedMessage(mode, message, key):

     if mode[0] == 'd':

         key = -key

     translated = ''

     for symbol in message:

         if symbol.isalpha():

             num = ord(symbol)

             num += key

            if symbol.isupper():
                 if num > ord('Z'):
                    num -= 26
                elif num < ord('A'):
                     num += 26

             elif symbol.islower():
                 if num > ord('z'):
                     num -= 26
                 elif num < ord('a'):

                    num += 26

             translated += chr(num)
         else:
             translated += symbol
     return translated

mode = getMode()

message = getMessage()

key = getKey()

print('Your translated text is:')

print(getTranslatedMessage(mode, message, key))