-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaesar Cipher.py
129 lines (108 loc) · 3.11 KB
/
Caesar Cipher.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#Caesar Cipher.py
import time
import random
f = open("englishdict.txt","r")
words = f.read()
words = words.lower()
words = words.split()
def encrypt(text, key):
key = key % 26
arr = []
orded = []
enc = []
text = text.lower()
for letter in text:
arr.append(letter)
for letter in arr:
ordletter = ord(letter)
if ord("a") <= ordletter <= ord("z"):
ordletter += key
if ordletter > ord("z"):
ordletter -= 26
orded.append(ordletter)
for number in orded:
enc.append(chr(number))
return "".join(enc)
def decrypt(text,key):
key = key % 26
arr = []
orded = []
enc = []
text = text.lower()
for letter in text:
arr.append(letter)
for letter in arr:
ordletter = ord(letter)
if ord("a") <= ordletter <= ord("z"):
ordletter -= key
if ordletter < ord("a"):
ordletter += 26
orded.append(ordletter)
for number in orded:
enc.append(chr(number))
return "".join(enc)
def crack(text):
possible = []
start = time.clock()
for i in range(0,26):
possible.append(decrypt(text,i))
startingPunctuation = "(&\"'/#{["
endingPunctuation = "),./:;}]\"'"
for candidate in possible:
a = candidate.split()
matches = 0
length = len(a)
for word in a:
while word[0] in startingPunctuation:
word = word[1:]
while word[-1] in endingPunctuation:
word = word[:-1]
if word in words:
matches += 1
if (matches / length) > 0.5:
print(matches,"/",length, "words found in the dictionary")
print("Caesar shifted by", possible.index(candidate), "places.")
end = time.clock()
print("Cracking took", end-start, "seconds.")
return candidate
return "Text was not caesar ciphered or contained too many spelling errors or unrecognised words."
def bruteforce(text):
for i in range(1,26):
print(decrypt(text,i))
def choice():
choice = input("Choose action: (e)ncrypt, (r)andom encrypt, (d)ecrypt or (c)rack? ")
q = choice[0]
if q == "e":
text = input("Enter text to encrypt: ")
key = int(input("Enter key: "))
print(encrypt(text,key))
print()
again()
elif q == "r":
text = input("Enter text to encrypt: ")
key = random.randint(0,25)
print(encrypt(text,key))
print()
again()
elif q == "d":
text = input("Enter text to decrypt: ")
key = int(input("Enter key: "))
print(decrypt(text,key))
print()
again()
elif q == "c":
text = input("Enter text to crack: ")
print(crack(text))
print()
again()
else:
print("Option not recognised.")
print()
choice()
def again():
decision = input("Another (y/n): ")
if decision[0] == "y" or decision[0] == "Y":
choice()
else:
return
choice()