基于python实现暴力破解凯撒密码

一、凯撒密码

        关于凯撒密码请参考之前的文章,链接如下:

        https://blog.csdn.net/weixin_52351575/article/details/120742012

二、暴力破解凯撒加密

原理是使用密码轮和凯撒加密的本质进行爆破

message = 'oknqdbqmoq{kag_tmhq_xqmdzqp_omqemd_qzodkbfuaz}'
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'

# Loop through every possible key:循环遍历所有可能的关键字
for key in range(len(SYMBOLS)):
    # It is important to set translated to the blank string so that the将translated定义成空字符很重要,这样可以清除上一个迭代中
    # previous iteration's value for translated is cleared.     translated的值
    translated = ''

    # The rest of the program is almost the same as the original program:程序的其余部分与凯撒程序基本相同

    # Loop through each symbol in `message`:循环遍历message中的每一个字符
    for symbol in message:
        if symbol in SYMBOLS:
            symbolIndex = SYMBOLS.find(symbol)
            translatedIndex = symbolIndex - key

            # Handle the wrap-around:执行回环
            if translatedIndex < 0:
                translatedIndex = translatedIndex + len(SYMBOLS)

            # Append the decrypted symbol:添加解密的字符
            translated = translated + SYMBOLS[translatedIndex]

        else:
            # Append the symbol without encrypting/decrypting:添加未解密/加密的字符
            translated = translated + symbol

    # Display every possible decryption:显示每一个可能的值
    print('Key #%s: %s' % (key, translated))

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码

)">
< <上一篇
下一篇>>