-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.py
More file actions
90 lines (72 loc) · 2.56 KB
/
analyzer.py
File metadata and controls
90 lines (72 loc) · 2.56 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
analyzer.py — Core Caesar Cipher Logic
CipherShift: Because manually testing 25 shifts is a war crime.
"""
from frequency import compute_confidence, ENGLISH_FREQ
from double_encode import detect_double_encoding, detect_rot13, detect_vigenere
def caesar_shift(text: str, shift: int) -> str:
"""
Apply a Caesar cipher shift to text.
Positive shift = encrypt, negative shift = decrypt (or shift 26-n).
Preserves case, non-alpha characters untouched.
"""
result = []
shift = shift % 26 # normalize, because someone will definitely pass in 27
for char in text:
if char.isalpha():
base = ord('A') if char.isupper() else ord('a')
shifted = (ord(char) - base + shift) % 26
result.append(chr(base + shifted))
else:
result.append(char)
return ''.join(result)
def encrypt(text: str, shift: int) -> str:
"""Encrypt plaintext with a given shift."""
return caesar_shift(text, shift)
def decrypt(text: str, shift: int) -> str:
"""Decrypt ciphertext with a known shift."""
return caesar_shift(text, -shift)
def brute_force(ciphertext: str) -> list[dict]:
"""
Try all 25 shifts and rank them by confidence score.
Returns a list of dicts sorted by confidence descending:
[{ 'shift': int, 'text': str, 'confidence': float }, ...]
Shift 0 is technically valid but boring — we include it anyway.
"""
results = []
for shift in range(26):
decoded = caesar_shift(ciphertext, -shift)
confidence = compute_confidence(decoded)
results.append({
'shift': shift,
'text': decoded,
'confidence': confidence
})
# Sort by confidence, highest first
results.sort(key=lambda x: x['confidence'], reverse=True)
return results
def auto_crack(ciphertext: str) -> dict:
"""
Automatically determine the most likely shift using frequency analysis.
Returns the best result with full metadata:
{
'shift': int,
'decoded': str,
'confidence': float,
'all_results': list,
'is_rot13': bool,
'double_encode': dict or None,
'vigenere_flag': bool
}
"""
all_results = brute_force(ciphertext)
best = all_results[0]
return {
'shift': best['shift'],
'decoded': best['text'],
'confidence': best['confidence'],
'all_results': all_results,
'is_rot13': detect_rot13(ciphertext),
'double_encode': detect_double_encoding(ciphertext),
'vigenere_flag': detect_vigenere(ciphertext)
}