【问题标题】:Breaking Cipher text using frequency analysis / crypt analysis technique使用频率分析/密码分析技术破解密文
【发布时间】:2020-08-20 01:01:54
【问题描述】:

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

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

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

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

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

谢谢 哈基德

【问题讨论】:

  • 1.只是直方图字母频率。如果你看过命运之轮,你就会知道 RSTLN 是英语中最常见的 5 个辅音,而 E 是最常见的元音。您可以抓取任何大型文本语料库来构建英语的“正确”直方图(参见古腾堡项目) 2. n-gram 频率。对于 n=2,(称为“bi-gram”)直方图是字母对的频率。例如,在英语中,“le”会比“lz”更频繁地出现。将“n”推高会为该语言产生更具特征的直方图,但数据集开始变得庞大。从技术上讲,第一个建议是“1-gram”

标签: security encryption cryptography caesar-cipher cryptanalysis


【解决方案1】:

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


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多