使用频率分析/密码分析技术破解密码文本



如果密钥不能通过移位确定,即密钥替换是随机的,您将如何编写程序(最好使用Java或Python(来破解随机密文

此网站(https://www.guballa.de/substitution-solver)已经做到了。

我必须通过频率分析(https://en.wikipedia.org/wiki/Frequency_analysis)

我面临的主要问题是在我替换时检查单词是否看起来像英语单词。

请指导我如何处理这个问题

谢谢hakid

这可能是一个迟来的答案,但这段代码可以作为您的开始。


from operator import itemgetter
letterFrequency = [
[12.00, 'E'], [9.10, 'T'],
[8.12, 'A'], [7.68, 'O'],
[7.31, 'I'], [6.95, 'N'],
[6.28, 'S'], [6.02, 'R'],
[5.92, 'H'], [4.32, 'D'],
[3.98, 'L'], [2.88, 'U'],
[2.71, 'C'], [2.61, 'M'],
[2.30, 'F'], [2.11, 'Y'],
[2.09, 'W'], [2.03, 'G'],
[1.82, 'P'], [1.49, 'B'],
[1.11, 'V'], [0.69, 'K'],
[0.17, 'X'], [0.11, 'Q'],
[0.10, 'J'], [0.07, 'Z']]

plain_to_cipher = {
"a": "l", "b": "f",
"c": "w", "d": "o",
"e": "a", "f": "y",
"g": "u", "h": "i",
"i": "s", "j": "v",
"k": "z", "l": "m",
"m": "n", "n": "x",
"o": "p", "p": "b",
"q": "d", "r": "c",
"s": "r", "t": "j",
"u": "t", "v": "q",
"w": "e", "x": "g",
"y": "h", "z": "k",
}
cipher_to_plain = {v: k for k, v in plain_to_cipher.items()}
alphabet = "qwertyuioplkjhgfdsazxcvbnm"

message = input("Enter message to encrypt: ")
message = message.lower()
ciphertext = ""

for c in message:
if c not in alphabet:
ciphertext += c
else:
ciphertext += plain_to_cipher[c]
print("nRandom substitution Encryption is: nt{}".format(ciphertext))
# .......................................................................
# calculate letter frequency of ciphertext
letter_list = []
cipher_len = 0
for c in ciphertext:
if c in alphabet:
cipher_len += 1
if c not in letter_list:
letter_list.append(c)
letter_freq = []
for c in letter_list:
letter_freq.append([round(ciphertext.count(c) / cipher_len * 100, 2), c])
# ....................................................................................
# Now sort list and decrypt each instance of ciphertext according to letter frequency
letter_freq = sorted(letter_freq, key=itemgetter(0), reverse=True)
decrypted_plaintext = ciphertext
index = 0
for f, c in letter_freq:
print("Replacing {} of freq {} with {}.".format(c, f, letterFrequency[index][1]))
decrypted_plaintext = decrypted_plaintext.replace(c, letterFrequency[index][1])
index += 1
print("nThe Plaintext after decryption using frequency analysis: nt{}".format(decrypted_plaintext))

旁注:该程序在大多数情况下可以成功解密像e, t, a, o这样使用最多的字母,但无法成功映射使用较少的字母(因为频率差异开始减少,结果不太可预测(。这个问题可以通过分析英语中最常用的bigram(如th(并使用结果进行更准确的预测来稍微解决。你可以利用的另一个注意点是,字母a很容易断开,这使得断开字母i不那么痛苦,因为任何中间有一个密文字符的句子都可能对应于a(例如:一本书(或i(例如:我去了((我们已经推导出a,所以任何其他单个密文字符都可能是i(

最新更新