In: Computer Science
PYTHON
Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user
For Encryption Process:
Take the string and reverse the string.
Encrypt the reverse string with each character replaced with distance value (x) given by the user.
For Decryption:
Take the string and reverse the string.
Decrypt the reverse string with each character replaced with distance value (x) given by the user.
Sample
String input - udc
Encryption process – udc -> xgf (encrypted)
The program should ask the user for input to encrypt, and then display the resulting encrypted output. Next your program should ask the user for input to decrypt, and then display the resulting decrypted output.
Example
Enter phrase to Encrypt (lowercase, no spaces): udc
Enter distance value: 3
Result: xgf
Enter pharse to Decrypt: (lowercase, no spaces): xgf
Enter distance value: 3
Result: udc
Python 3 code
============================================================================================
inputmsg=input('Enter phrase to Encrypt (lowercase, no spaces):
')
distance=int(input('Enter distance value: '))
#encryption
#reverse a string
revstr=""
for i in inputmsg:
revstr=i+revstr
#print('reverse string is:',revstr)
encryptmsg=""
for i in range(len(revstr)):
encryptmsg+=chr((ord(revstr[i]) + distance - 97) % 26 + 97)
print("Result: ",encryptmsg)
#decoding
inputmsg1=input('Enter phrase to Encrypt (lowercase, no spaces):
')
distance1=int(input('Enter distance value: '))
#decryption
#reverse a string
revstr1=""
for i in inputmsg1:
revstr1=i+revstr1
demsg=""
for i in range(len(revstr1)):
demsg+=chr((ord(revstr1[i]) - distance1 - 97) % 26 + 97)
print("Result: ",demsg)
============================================================================================
Output