15,992,922 members
1.00/5 (1 vote)
See more:
```Hi Below are my codes for encryption and decryption.

import ast
a = ast.literal_eval(input())
n1 = a[0]
n2 = a[1]
def encryption(string,step):
result = ''
for i in string:
if i == "":
result = result+i
elif i.isupper():
result = result + chr((ord(i) + step - 65) % 26 + 65)
else:
result = result + chr((ord(i) + step - 97) % 26 + 97)
return result
print(encryption(n1,n2))

Input: n1 = upHrae, n2 = 4
Output: ytLvei

#Program for Decryption

def decryption(string1,step):
result1 = ''
for j in string1:
if j == "":
result1 = result1+i
elif j.isupper():
result1 = result1 + chr((ord(i) + step + 65) % 26 + 65)
else:
result1 = result1 + chr((ord(i) + step + 97) % 26 + 97)

return result1
print(drcryption(n1,n2))

input: n1 = banana, n2 = 7
output: utgtgt

I want to combine both encryption and decryption function in one function which generates output as 'ytLvei' when input is 'n1 = upHrae, n2 = 4' and generates output as 'utgtgt' when input is 'n1 = banana, n2 = 7.'

What I have tried:

```I tried Below code but it is not working as expected.

import ast
n = ast.literal_eval(input())
n1 = n[0]
step = n[1]
def enc_dec(string,step):
result = ''
for i in string:
if ((ord(i) + step)>=65) or ((ord(i) + step)<=97):
if i=='':
result = result+i
elif i.isupper():
result = result + chr((ord(i) + step - 65) % 26 + 65)
else:
result = result + chr((ord(i) + step - 97) % 26 + 97)

if ((ord(i) + step)>=97) or ((ord(i) + step)<=122):
if i=='':
result = result+i
elif i.isupper():
result = result + chr((ord(i) + step + 65) % 26 + 65)
else:
result = result + chr((ord(i) + step + 97) % 26 + 97)
return result

print(enc_dec(n1,step))```
Posted
Updated 5-Dec-21 21:07pm
Richard Deeming 14-Apr-20 11:04am
`if ((ord(i) + step)>=65) or ((ord(i) + step)<=97):`

That looks like a bug in your code. Can you name a single number which is not either greater than or equal to `65` OR less than or equal to `97`?

Solution 1

You already have that: it's a Caesar cypher, so you just have to pass it a negative step to decrypt an encrypted message.
Python
`decrypted = encryption("ytLvei", -4)`

Nikki 2000 12-Apr-20 15:19pm
Hi, I know the decrypt part too but the question I have with me is given below.

You write all your passwords in a diary so that you don't forget them. But clearly this is too risky, so you came up with a simple plan, you will simply write it by shifting all the alphabets by a certain step. For eg: if you decide your step to be 3, then 'a' will become 'd', and 'k' will become 'n' and so for all alphabets. The last alphabets will simply circle back to 'a'. In this case, 'y' will become 'b' and so on. Now you just have to remember the step size, can then you can check the password anytime you want. You decided to write code to do this, now that you have learned to code in python. Your code will take in the step size and what is written in the diary and give out the real password.

So as per the question if Alphabet (ord(alphabet) + step is greater than z or Z) then it must loop back to previous letter. To achieve this I have implemented the if if ((ord(i) + step)>=65) or ((ord(i) + step)<=97) condition but it is not working as expected.

Solution 2

import ast
mylist=ast.literal_eval(input())
stringMessage=mylist[0]
shift_value=mylist[1]
#start writing your code from here
def shift_string(message, n):
list1=[]
for i in message:
ch = i
base = ord('a' if ch.islower() else 'A')
x = chr((ord(ch) - base - n) % 26 + base)
list1.append(x)
return("".join(list1))
print(shift_string(stringMessage,shift_value))