zoukankan      html  css  js  c++  java
  • 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  用来做好的用户体验
                # 灵活输入即可(而非严格的输入)
     
  • 相关阅读:
    【C#4.0图解教程】笔记(第19章~第25章)
    【C#4.0图解教程】笔记(第9章~第18章)
    【C#4.0图解教程】笔记(第1章~第8章)
    平衡二叉树(AVL Tree)
    整数中1出现的次数(从1到n整数中1出现的次数)
    java实现回溯算法
    输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
    怎么用Markdown在github上写书,并用pages展示
    二叉搜索树与双向链表
    复杂链表的复制
  • 原文地址:https://www.cnblogs.com/PiaYie/p/13472596.html
Copyright © 2011-2022 走看看