Write a program in Python that has TWO functions: Function One is called: _encry
ID: 3574854 • Letter: W
Question
Write a program in Python that has TWO functions:
Function One is called: _encrypt
Function Two is called: _decrypt
Function one takes plain text, and assesses the indexed value of each letter in the string. For example, a=1, b=2. It then adds three to the indexed value, and produces encrypted text, based off of the plain text.
Function two reverses this.
Here is sample output:
_encrypt('how do you do')
Unsecured: howdoyoudo
Secured: lsahscsyhs
_decrypt('lsahscsyhs')
Unsecured: lsahscsyhs
Secured: howdoyoudo
Explanation / Answer
def _encrypt(s):
ans =""
for k in s:
a1 = ord(k)
a2 = ord('a')
a3 = ord("a")
if(a1-a2>=0 and a1-a2<=25):
b1 = (a1-a2+4)%26+a2
ans = ans+chr(b1)
elif(a1-a3>=0 and a1-a3<=25):
b1 = (a1-a3+4)%26+a3
ans = ans+chr(b1)
return ans
def _decrypt(s):
ans =""
for k in s:
a1 = ord(k)
a2 = ord('a')
a3 = ord("a")
if(a1-a2>=0 and a1-a2<=25):
b1 = (a1-a2+22)%26+a2
ans = ans+chr(b1)
elif(a1-a3>=0 and a1-a3<=25):
b1 = (a1-a3+22)%26+a3
ans = ans+chr(b1)
return ans
s = _encrypt("how do you do")
print(s)
print(_decrypt(s))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.