-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcipher.py
More file actions
61 lines (45 loc) · 1.5 KB
/
Copy pathcipher.py
File metadata and controls
61 lines (45 loc) · 1.5 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def encrypt(text, s):
result = ""
# traverse text
for i in range(len(text)):
char = text[i]
# Encrypt uppercase characters
if char.isupper():
result += chr((ord(char) + s - 65) % 26 + 65)
# Encrypt lowercase characters
elif char.islower():
result += chr((ord(char) + s - 97) % 26 + 97)
# Add non-alphabet characters unchanged
else:
result += char
return result
def decrypt(text, s):
result = ""
# traverse text
for i in range(len(text)):
char = text[i]
# Decrypt uppercase characters
if char.isupper():
result += chr((ord(char) - s - 65) % 26 + 65)
# Decrypt lowercase characters
elif char.islower():
result += chr((ord(char) - s - 97) % 26 + 97)
# Add non-alphabet characters unchanged
else:
result += char
return result
# Main logic
choice = input("Want to encrypt or decrypt (e/d): ").lower()
if choice not in ('e', 'd'):
print("Wrong input")
else:
text = input("Enter the message: ")
s = int(input("Enter the shift value: "))
if choice == 'e':
print("Text: " + text)
print("Shift: " + str(s))
print("Cipher: " + encrypt(text, s))
elif choice == 'd':
print("Text: " + text)
print("Shift: " + str(s))
print("Cipher: " + decrypt(text, s))