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

The aim of this exercise is to implement a function for decrypting a given messa

ID: 667767 • Letter: T

Question

The aim of this exercise is to implement a function for decrypting a given message. You are given a lower-case alphabetic string comprising letters in the range a to z, which has been encrypted using a modified form of a Caesar cipher. Your goal is to write a function that decrypts the given string using the given key.

In a traditional Caesar cipher, a given string is encrypted by replacing each alphabetic character in the string by its corresponding encrypted character according to a mapping rule, and a given key k, which is a positive integer k>0. The mapping rule for a traditional Caesar cipher replaces a character c with an encrypted character c, which is the letter that is k positions later in the alphabet than c. For example, if k=4 then the given stringyuck becomes the encrypted string cygo. Note that if k is greater than 26, then you should wrap around to the start of the alphabet again, so a key k=27 is equivalent to a key k=1.

In our modified Caesar cipher, the mapping rule is modified so that characters at even index positions 0, 2, 4, … in the original string are replaced by the letter k positions later in the alphabet, while characters at odd index positions 1, 3, 5, … in the original string are replaced by the letter k positions earlier in the alphabet. For example, if k=4 then the string yuck becomes the encrypted string cqgg.

Thus, decryption in our modified Caesar cipher involves replacing characters at even index positions 0, 2, 4, ... in the encrypted string with the corresponding letter k positions earlier in the alphabet, while characters at odd index positions 1, 3, 5, ... in the encrypted string are placed with the letter k positions later in the alphabet.

Write a function decrypt(s, k) that takes a string argument s(containing the encrypted code word) and a positive integer argument k (containing the key used for encryption) and returns thestring value of the decrypted code word.

Example calls to the function are:

Explanation / Answer

#!/usr/bin/env python def banner(): print ''' ___ + +++ ++++++++++++++++++++++++++++++++++++++ ------- =|This is a message encoder and decoder |By z3r0 > ++++++++++++++++++++++++++++++++++++++ ------- | ===== |--\-------// | ===== |___\_____// |Choose an option // |----------------- |1. Encode| |2. Decode| ----------- ============= 3. Encryption and Decryption (!!!NEW!!!) ''' def bye(): print ''' Thank you for using this tool! Please report any bugs or suggestions to z3r0.mhu@gmail.com Good Bye, Have a nice day! By the author z3r0! Greetingz to : |MHU| cr3w ''' def encrypt_decrypt(): print'' print'This is a newly added feature that can encrypt or decrypt' print'your string into a caesar cipher' print ''' First of all, choose whether you will encrypt or decrypt 1. Encrypt 2. Decrypt 3. Exit ''' ans = raw_input("Please Enter choice : ") if ans == '1': print("Enter the message you want to encrypt below") msg = raw_input(">> : ") key_value = eval(raw_input("Enter the key value to encrypt : ")) print'' encrypted = "" for a in msg: encrypted = encrypted + chr(ord(a) + key_value) print'Encrypted message become : ', encrypted elif ans == '2': print'Enter the encrypted message to decrypt below' msg = raw_input('>> : ') key_value = eval(raw_input("Enter the key value to decrypt : ")) print'' decrypted = "" for a in msg: decrypted = decrypted + chr(ord(a) - key_value) print'Decrypted message is : ', decrypted elif ans == '3': bye() def main(): option = input("Enter choice : ") if option == 1: print'' print'This program converts a textual message into a sequence' print'of numbers representing the Unicode encoding of the message' print'' #Get the message to encode message = raw_input("Please enter the message to encode : ") encoded = '' # Loop through the message and print out the Unicode values for ch in message: encoded = encoded + str(ord(ch)) + " " print'' print'--------------------------------------' print'Encoded message is : ', encoded print'--------------------------------------' bye() elif option == 2: #Get the message to encode print("") print("This program converts back the unicode number to message") instring = raw_input("Please enter the code : ") decoded = "" # Loop through the message and print out the Unicode values for numStr in instring.split(): codeNum = eval(numStr) # convert digits to a number decoded = decoded + chr(codeNum) # concatentate character to message # or you can use The following code in place of the above code # chars.append(chr(codeNum)) # but if you do use above, you need to create a list name "chars" print'' print'--------------------------------------' print'Decoded message is : ', decoded print'--------------------------------------' bye() elif option == 3: encrypt_decrypt() else : print("Input Error, invaild option!") bye() banner() main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote