首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:解码用Affine cipher编码的Vigenere密码

Python:解码用Affine cipher编码的Vigenere密码
EN

Stack Overflow用户
提问于 2021-01-09 22:03:13
回答 1查看 435关注 0票数 1

我如何解码一些像这样编码的文本:affine(vigenere(text, vigenere_key), *affine_key)?他们两个我都不知道钥匙。起初我认为我可以尝试所有可能的组合来强行破解它,但后来我意识到破解Vigenere是基于找到关键字,可以是字面上的任何东西,既然解码后的消息在破解Vigenere之后没有任何意义,因为它仍然是用Affine编码的。我有点困惑,我怎么才能找到正确的Vigenere密钥并破解它?

EN

回答 1

Stack Overflow用户

发布于 2021-01-11 10:05:47

好吧,我猜这不能回答你的问题,但我希望它能有所帮助。在你的情况下,如果你真的想破解这个密文,你可以尝试暴力破解仿射密文(我希望它不会很长,因为暴力破解具有指数时间复杂度),那么对于每个解密的密文,你可以通过利用vignere密文的属性来测试特定的解密密文是否有额外的一层vigenere加密层,keep traces of Index of coincidence of the明文语言,这可以用来猜测那段解密的密文是否有点像英语(除了字母被替代),尽管这只能在vigenere密钥很短(大约5或6个字符)的情况下工作。然后,在有了一个有前途的候选人之后,你可以使用暴力破解vigenere密钥。

这段python代码计算符合的索引,并尝试猜测密钥长度。

代码语言:javascript
复制
def index_of_coincidence(ciphertext: str) -> float:
    alphabet = "qwertyuioplkjhgfdsazxcvbnm"
    ciphertext = ciphertext.lower()
    ciphertext_length = 0

    for char in ciphertext:
        if char in alphabet:
            ciphertext_length += 1

    lc = 0
    denomiator = (ciphertext_length * (ciphertext_length-1))
    for char in alphabet:
        letter_count = ciphertext.count(char)
        lc += (letter_count * (letter_count-1)) / denomiator
    return lc

def vigenere_key_length(ciphertext: str) -> int:

    lc_ranges = ["x", 0.0639, 0.0511, 0.0468, 0.0446, 0.0438, 0.0426]
    lc = index_of_coincidence(ciphertext)

    print(lc)
    if lc >= lc_ranges[1]:
        print("The Key length is probably {}".format(1))
        key = 1
        return key

    key = 1
    for key_length in range(1, 6):
        if key_length == 6:
            print("Cant determine Key length with accuracy")
            return None
            
        if lc_ranges[key_length] >= lc >= lc_ranges[key_length + 1]:
            key += key_length
            break

    print("The Key Length is probably {}".format(key))
    return key

请注意,对于较小的密钥长度,破解vigenere密钥的预期时间复杂度不会很大,例如,5字符的密钥长度将是7893600 = 26*25*24*23*22尝试,这对于每个解密的密文来说都可以很容易地完成,但如果仿射密钥很大,那么在蛮力强行之前使用上面的代码来测试它是否可能是英语候选者会更好。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65643521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档