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

Python ver 2.7 File Encryption/Decryption Program: You are to design and write a

ID: 3885820 • Letter: P

Question

Python ver 2.7

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 generate a 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 “l”)

aA0bB1cC2dD3eE4fF5gG6hH7iI8jJ9kK lL,mM.nN?oO/pP;qQ:rR'sS"tT!uU@vV$wW%xX&yY-zZ= sequece position: 012345678901234567890123456789012345678901234567890123456789012345678901234567

01234567

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 ‘Z’ 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 (e.g., ‘<’, ‘{’) 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:

Your program also needs to: (See www.cs.uni.edu/~fienup/cs1520f17/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. If it

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.

Four Line Message to Encrypt with Key = 5 in file message.txt

Encrypted Message in file message.zzz

Your program's interaction should looks something like: (Student input shown in bold.)

Welcome to the Encryption/Decryption Program
Would you like to (e)ncrypt a file, (d)ecrypt a file, or e(x)it (enter e, d, or x)? encrypt

Sorry, that’s an invalid choice. Please enter only e, d, or x: e What positive integer key would you like to use for encryption? five

Sorry, that’s an invalid choice. Please enter a positive integer: 5

Enter the text-file name to encrypt: mesage.txt
Sorry the file ‘mesage.txt’ does NOT exist -- please try again! Enter the text-file name to encrypt: messaeg.txt
Sorry the file ‘messaeg.txt’ does NOT exist -- please try again! Enter the text-file name to encrypt: message.txt

The file ‘message.txt’ was successfully encrypted using a key of 5 to the file ‘message.zzz’ Would you like to (e)ncrypt a file, (d)ecrypt a file, or e(x)it (enter e, d, or x)? d
What positive integer key would you like to use for decryption? 5
Enter the text-file name to decrypt: message.zzz

WARNING: The file ‘message.txt’ already exists!
Is it okay to wipe it out (y/n)? n
Enter the file name that should be used (.txt extension will automatically be added): decryptedMessage

Would you like (e)ncrypt a file, (d)ecrypt a file, or e(x)it (enter e, d, or x)? x Bye!

When you write your program, be sure you:

! save your program in a file called hw2.py

! make your program robust by validating the correctness of user input: verify that user-entered file names exists

(import os.path) before opening them, and check for correct menu-option, etc.

! think about the functional-decomposition (top-down) design before you start to write code! You will need to

turn in a design document (see lab 1 for an example: diagram and sentence about each function).

! don’t use global variables, except for global constants with good style (ALL_CAPS_AND_UNDERSCORES).

Variables should be passed as parameters into functions and returned from functions

! use meaningful variable names with good style (i.e., useCamelCase)

! use comments (""" Multi-line Comment """) at the start of the program and immediately after each function

definition describing what they do (see lab1 diceOutcomes.py program)

! use a main function (see lab1 diceOutcomes.py program) located at the top of program with a call to it at the

bottom to start execution

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!  
  <We'll study for Data Structures...>  - Sam  
  dF/,z83al/fHvwb& t3RRa1g  
  <qA$2w$2lnxb8;tf1oDtt dLklwvnlvw4ZZ>  LJdSm  

Explanation / Answer

# Caesar Cipher

MAX_KEY_SIZE = 26


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))