-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
66 lines (50 loc) · 1.61 KB
/
app.py
File metadata and controls
66 lines (50 loc) · 1.61 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
62
63
64
65
66
def caesar_shift(text, shift, direction):
result = ""
# Direction Shifting
if direction == "L":
shift = -shift
elif direction == "R":
pass
else:
return ""
for char in text:
if char.isalpha():
base = ord("A") if char.isupper() else ord("a")
result += chr((ord(char) - base + shift) % 26 + base)
else:
result += char
return result
def english_score(text):
freq_bonus = "ETAOIN SHRDLU"
score = 0
for ch in text.upper():
if ch in freq_bonus:
score += 2
elif ch.isalpha():
score += 1
elif ch == " ":
score += 3
else:
score -= 1
return score
def brute_force_caesar(cipher_text):
results = []
seen_texts = set() # preventing from mirror duplication
for shift in range(1, 26):
for direction in ["L", "R"]:
plaintext = caesar_shift(cipher_text, shift, direction)
# Avoiding duplicate mirror results
if plaintext in seen_texts:
continue
seen_texts.add(plaintext)
score = english_score(plaintext)
results.append((score, shift, direction, plaintext))
# Ranking by score
results.sort(reverse=True)
return results
print("\n--- Caesar Cipher Brute Force ---\n")
cipher = input("Enter Caesar cipher text: ")
ranked_results = brute_force_caesar(cipher)
print("\nTop Possible Plaintexts:\n")
for score, shift, direction, text in ranked_results[:5]:
print(f"Shift {shift:2d} | Dir {direction} | Score {score:3d} | {text}")