• transpositionHacher


    对于换位加密算法,我们知道,破译密文最重要的是搞到正确的key


    所以我们重点关注key的取值

    key的所有取值是   1 到 len(message) 

      为什么不是0到 len(message)呢?

      第一是因为默认密文是加密后的,第二如果key = 0, 那么在执行计算多少个格子时使用了

        numOfColumns = math.ceil(len(message) / key)

      结果就是会出现除以0错误!

        。。。。。。
    File
    "d:PiaYiejczhang密码学py密码学编程教学代码 ranspositionHacker.py", line 11, in main hackedMessage = hackTransposition(myMessage) File "d:PiaYiejczhang密码学py密码学编程教学代码 ranspositionHacker.py", line 32, in hackTransposition decryptedText = transpositionDecrypt.decryptMessage(key, message) File "d:PiaYiejczhang密码学py密码学编程教学代码 ranspositionDecrypt.py", line 30, in decryptMessage numOfColumns = math.ceil(len(message) / key) ZeroDivisionError: division by zero

    破译算法:

    # Transposition Cipher Hacker
    # http://inventwithpython.com/hacking (BSD Licensed)
    
    import pyperclip, detectEnglish, transpositionDecrypt
    
    def main():
        # You might want to copy & paste this text from the source code at
        # http://invpy.com/transpositionHacker.py
        myMessage = """Cb b rssti aieih rooaopbrtnsceee er es no npfgcwu  plri ch nitaalr eiuengiteehb(e1  hilincegeoamn fubehgtarndcstudmd nM eu eacBoltaeteeoinebcdkyremdteghn.aa2r81a condari fmps" tad   l t oisn sit u1rnd stara nvhn fsedbh ee,n  e necrg6  8nmisv l nc muiftegiitm tutmg cm shSs9fcie ebintcaets h  aihda cctrhe ele 1O7 aaoem waoaatdahretnhechaopnooeapece9etfncdbgsoeb uuteitgna.rteoh add e,D7c1Etnpneehtn beete" evecoal lsfmcrl iu1cifgo ai. sl1rchdnheev sh meBd ies e9t)nh,htcnoecplrrh ,ide hmtlme. pheaLem,toeinfgn t e9yce da' eN eMp a ffn Fc1o ge eohg dere.eec s nfap yox hla yon. lnrnsreaBoa t,e eitsw il ulpbdofgBRe bwlmprraio po  droB wtinue r Pieno nc ayieeto'lulcih sfnc  ownaSserbereiaSm-eaiah, nnrttgcC  maciiritvledastinideI  nn rms iehn tsigaBmuoetcetias rn"""
    
        hackedMessage = hackTransposition(myMessage)
    
        if hackedMessage == None:
            print('Failed to hack encryption.')
        else:
            print('Copying hacked message to clipboard:')
            print(hackedMessage)
            pyperclip.copy(hackedMessage)
    
    
    def hackTransposition(message):
        print('Hacking...')
    
        # Python programs can be stopped at any time by pressing Ctrl-C (on
        # Windows) or Ctrl-D (on Mac and Linux)
        print('(Press Ctrl-C or Ctrl-D to quit at any time.)')
    
        # brute-force by looping through every possible key
        for key in range(1, len(message)):
            print('Trying key #%s...' % (key))
    
            decryptedText = transpositionDecrypt.decryptMessage(key, message)
    
            if detectEnglish.isEnglish(decryptedText):
                # Check with user to see if the decrypted key has been found.
                print()
                print('Possible encryption hack as you check:')
                print('Key %s: %s' % (key, decryptedText[:100]))
                print()
                print('Enter D for done, or just press Enter to continue hacking:')
                response = input('> ')
    
                if response.strip().upper().startswith('D'):
                    return decryptedText
    
        return None
    
    if __name__ == '__main__':
        main()

    看起来这么短,实际上有

    import pyperclip, detectEnglish, transpositionDecrypt 

    调用了大量的方法。

     

    strip()方法

      返回字符串首尾去掉空白(空格字符、制表符、换行符)后的版本

                # response.strip().upper().startswith('D')返回true or Flase  用来做好的用户体验
                # 灵活输入即可(而非严格的输入)
     
  • 相关阅读:
    iframeWin For Easy UI. 为 Easy UI 扩展的支持IFrame插件
    mac系统及xcode使用的SVN客户端安装升级
    泛型List的一点建议
    redis-setbit理解
    zpkin sql语句
    idea 使用 RunDashboard启动
    (转)Spring事务不生效的原因大解读
    (转)Lock和synchronized比较详解
    springboot整合HttpAsyncClient简单实用
    mysql 数据库表迁移复制
  • 原文地址:https://www.cnblogs.com/PiaYie/p/13472596.html
Copyright © 2020-2023  润新知