0

您将如何编写程序(最好使用 Java 或 Python)来破坏随机密文,其中密钥无法通过移位确定,即密钥替换是随机的。

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

我必须通过频率分析来做到这一点(https://en.wikipedia.org/wiki/Frequency_analysis

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

请指导我如何解决这个问题

谢谢哈基德

4

1 回答 1

0

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


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: \n\t{}".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: \n\t{}".format(decrypted_plaintext))

旁注:该程序在大多数情况下可以成功解密最常用的字母,e, t, a, o但无法成功映射较少使用的字母(因为频率差异开始减少,结果难以预测)。th通过分析英语最常用的二元组(如)并使用结果进行更准确的预测,可以稍微克服这个问题。您可以利用的另一个注意事项是,该字母a很容易被打破,从而减少了打破字母i的痛苦,因为任何中间有一个密文字符的句子都可能对应于a(例如:一本书)或i(例如:我去了)(和我们已经推断出a任何其他单个密文字符都可能是i

于 2020-10-31T14:39:15.873 回答