-
Notifications
You must be signed in to change notification settings - Fork 1
/
Caesar Cypher Algorithm (E&D).py
80 lines (59 loc) · 1.68 KB
/
Caesar Cypher Algorithm (E&D).py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import string
import time
def caes_encrypt():
cipherText = ""
text = input("Enter Plain Text: ")
shft = int(input("Enter Key: "))
for i in range(len(text)):
char = text[i]
if (char.isupper()):
cipherText += chr((ord(char)-65 + shft) % 26 + 65)
else:
cipherText += chr((ord(char)-97 + shft) % 26 + 97)
print("\nCipher Text :", cipherText)
def caes_decrypt():
alpha_dict = [i for i in string.ascii_uppercase]
print("\n1. Decrypt Using BruteForce")
print("2. Decrypt Using Key")
cho_dec=int(input("Enter Your Choice: "))
if cho_dec==1:
cipherText = input("\nEnter Cipher Text: ")
print("\nApplying Brute Force in Cipher Text\n")
time.sleep(2)
for i in range(0, 26):
plainText = ''
for j in cipherText:
letterIndex = (ord(j.upper()) - 65 - i) % 26
if j.islower():
plainText += alpha_dict[letterIndex].lower()
else:
plainText += alpha_dict[letterIndex]
print("Key:",i,"-> Plain-Text:",plainText)
elif cho_dec==2:
plainText = ""
cipherText = input("\nEnter Cipher Text: ")
shft = int(input("Enter Key: "))
for i in range(len(cipherText)):
char = cipherText[i]
if (char.isupper()):
plainText += chr((ord(char)-65 - shft) % 26 + 65)
else:
plainText += chr((ord(char)-97 - shft) % 26 + 97)
print("\nPlain Text :", plainText)
else:
print("\nInvalid Choice")
while True:
print("\n----- Caesar Cipher Algorithm -----")
print("1. Encrypt Text")
print("2. Decrypt Text")
print("3. Exit")
ch = int(input("\nEnter Your Choice: "))
if ch == 1:
caes_encrypt()
elif ch == 2:
caes_decrypt()
elif ch == 3:
print("\nThank You For Using This Tool !")
exit(0)
else:
print("\nInvalid Option")