-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar Cipher Implementation in Python.py
More file actions
40 lines (34 loc) · 1.06 KB
/
Caesar Cipher Implementation in Python.py
File metadata and controls
40 lines (34 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Caesar Cipher Implementation in Python
# Menu Initialization
print("Caesar Cipher Program")
print("Encrypt - 1")
print("Decrypt - 2")
# input from user
choice = int(input("Enter your choice of action: "))
if choice == 1:
text = input("Enter text to be encrypted: ")
shift = input("Enter shift for encryption (or leave blank) : ")
else:
text = input("Enter text to be decrypted: ")
shift = input("Enter shift for decryption (or leave blank) : ")
# Default value of Shifter
if shift == "":
shift = 13
# Initialization of resultant string
result = ""
# Conditional & Iterational Statements
for i in text:
if i.isalpha(): # Segregation of alphabets & other characters
if i.isupper(): # Segregation of upper & lowercase alphabets
base = ord("A")
else:
base = ord("a")
result += chr((ord(i) - base + shift) % 26 + base)
else:
result += i
# Output to user
if choice == 1:
print(f"Encrypted Text : {result}")
else:
print(f"Decrypted Text : {result}")
# End