2.a. A particular program performs encryption and decryption of text strings. It
ID: 3598763 • Letter: 2
Question
2.a. A particular program performs encryption and decryption of text strings. It prompts a user to enter 'E' or 'e' for encryption, and 'D' or 'd' for decryption. No other initial input is valid. Given the dictionary OPERATIONS = { 'e' : (1, "Encrypted") , 'd' : (-1, "Decrypted") } Write a predicate function called validateOperation ) that - Receives parameter opstr (the operation string input by the user) .Returns True when it is a valid operation and False otherwise Here is the documentation that should precede the function: . # Check that requested operation is valid # param opstr (str) -operation requested # return True when valid, False otherwise (b 01) 2.b. Within the same program described in #2a, text is encrypted using a Caeser Cipher: Each letter of the text is rotated backward or forward through the alphabet according to a rotation key input by the usen - This rotation key can only be made up of positive or negative whole numbers. . Regardless of the operation requested, the rotation key supplied is always the one originally used to encrypt the information Given the string constant KEY PREFIX- Write a predicate function called validateRotationKey) that Receives parameter rotationKeyStr (the rotation key string input by the user) Returns True when it is a valid rotation key and False otherwise .Here is the documentation that should precede the function: # Check that rotation key is of the form or- #param rotationkeyStr (str) # invokes # returns: True when valid, False otherwise (bool) ? (what method can it invoke to validate its characters?) 2.c. Within the same program, if the operation requested is encryption, then the rotation key can be used as is However, if the operation requested is decryption, then the rotation key given is the one used to encrypt the text, and its sign must be flipped in order to decrypt the text Using the same OPERATIONS dictionary as above, write a predicate function called convertRotationKey () that . Receives the parameters opStr (validated operation requested) and rotationKeystr (validated rotation key still in string form) . Returns the numeric key to be used for the given operation(.e., converted to int with flipped sign if operation is decrypt Here is the documentation that should precede the function: # Convert rotation key to value usable for requested operation # param opstr (str) -operation requested # param rotationkeyStr (str) -the original encryption key # functions invoked # return encryption or decryption rotation key (int) ? (i.e. , make it numeric with correct sign)Explanation / Answer
Hi,
The python encryption and decryption code to meet all the above subtasks is as follows:
operations={'e':{1,"Encrypted"},'d':{-1,"Decrypted"}}
def validateOperation(str):
if str in list(operations.keys()):
return True
else:
return False
def validateRotationKey(str):
if isinstance(int(str),int)==True:
return True
else:
return False
def convertRotationKey(op,key):
if(op == "e" or op=="E"):
key=int(key)
return key
elif(op == "d" or op=="D"):
key = -int(key)
return key
x=input("Enter an operation:")
result1=validateOperation(x)
print (result1)
y=input("Enter a rotation key:")
result2=validateRotationKey(y)
print (result2)
if result1==True and result2==True:
print ("The Rotation Key is %d" %convertRotationKey(x,y))
else:
print("Invalid Operation Requested or Invalid Rotation key is given as input! ")
Thanks and Regards,
Hema.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.