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
|
class Solution:
def decryptXORAdvanced(self, hex_str: str) -> str:
if not hex_str:
return ""
bytes_data = self._hexToBytes(hex_str)
if not bytes_data:
return ""
best_result = ""
best_score = -1000.0
for key in range(256):
candidate = self._tryKey(bytes_data, key)
score = self._advancedScore(candidate)
if score > best_score:
best_score = score
best_result = candidate
# Early termination for high confidence
if score > 50.0 and self._isLikelyEnglish(candidate):
break
return best_result
def _advancedScore(self, text: str) -> float:
score = 0.0
# Printability score
printable = sum(1 for c in text if 32 <= ord(c) <= 126)
score += (printable / len(text)) * 20.0 if text else 0
# Common words bonus
common_words = ["the", "and", "for", "are", "but", "not", "you", "all",
"can", "had", "her", "was", "one", "our", "out", "day",
"get", "has", "him", "his", "how", "man", "new", "now",
"old", "see", "two", "way", "who", "boy", "did", "its",
"let", "put", "say", "she", "too", "use"]
text_lower = text.lower()
for word in common_words:
if f" {word} " in text_lower or text_lower.startswith(f"{word} ") or text_lower.endswith(f" {word}"):
score += 3.0
# Enhanced character frequency analysis
english_freq = [8.12, 1.49, 2.78, 4.25, 12.02, 2.23, 2.02, 6.09, 6.97,
0.15, 0.77, 4.03, 2.41, 6.75, 7.51, 1.93, 0.10, 5.99,
6.33, 9.06, 2.76, 0.98, 2.36, 0.15, 1.97, 0.07]
letter_count = [0] * 26
total_letters = 0
for c in text.lower():
if c.isalpha():
letter_count[ord(c) - ord('a')] += 1
total_letters += 1
if total_letters > 0:
freq_score = 0.0
for i in range(26):
observed = (letter_count[i] / total_letters) * 100.0
expected = english_freq[i]
freq_score -= abs(observed - expected) * 0.1
score += freq_score
# Space frequency (English text typically has 10-15% spaces)
spaces = text.count(' ')
if len(text) > 0:
space_ratio = spaces / len(text)
if 0.08 <= space_ratio <= 0.20: # Reasonable space ratio
score += 5.0
else:
score -= abs(space_ratio - 0.13) * 10.0
return score
def _isLikelyEnglish(self, text: str) -> bool:
if not text:
return False
letters = sum(1 for c in text if c.isalpha())
spaces = text.count(' ')
return letters > len(text) * 0.6 and spaces > 0
def _hexToBytes(self, hex_str: str) -> List[int]:
bytes_data = []
for i in range(0, len(hex_str), 2):
if i + 1 < len(hex_str):
try:
bytes_data.append(int(hex_str[i:i+2], 16))
except ValueError:
continue
return bytes_data
def _tryKey(self, bytes_data: List[int], key: int) -> str:
return ''.join(chr(byte_val ^ key) for byte_val in bytes_data)
|