-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaesar.py
21 lines (21 loc) · 1.09 KB
/
caesar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#program to encrypt data using Caesar Cipher
str1=input("Enter the string which you want to encode:") #taking input of the string to be encrypted
n=int(input("Enter the number by which you want to shift it(1-26):")) #taking input of the number by which user wants to shift each character of the string
while n>26 or n<1:
print("Invalid Input!!!")
n=int(input("Enter the number by which you want to shift it(1-26):"))
l=list(str1) #splitting the input string
strf=''
#iterating through the list to shift the characters
for i in l:
p=ord(i)
if p<123 and p>96: #checking whether it is a small letter or a capital letter
p+=n
if p>=123:
p=(p-122)+96 #if the range is above the expected then it is shifted in a cycle such that if p+n>expected range then it starts from the beginning i.e. either 97 or 65
if p>=65 and p<=90:
p+=n
if p>91:
p=(p-90)+64
strf+=chr(p) #concatenating the string for the final answer
print(strf)