transpositionHacher
对于换位加密算法,我们知道,破译密文最重要的是搞到正确的key
所以我们重点关注key的取值
key的所有取值是 1 到 len(message)
为什么不是0到 len(message)呢?
第一是因为默认密文是加密后的,第二如果key = 0, 那么在执行计算多少个格子时使用了
numOfColumns = math.ceil(len(message) / key)
结果就是会出现除以0错误!
。。。。。。
File "d:\PiaYie\jczhang\密码学\py密码学编程教学代码\transpositionHacker.py", line 11, in main hackedMessage = hackTransposition(myMessage) File "d:\PiaYie\jczhang\密码学\py密码学编程教学代码\transpositionHacker.py", line 32, in hackTransposition decryptedText = transpositionDecrypt.decryptMessage(key, message) File "d:\PiaYie\jczhang\密码学\py密码学编程教学代码\transpositionDecrypt.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 用来做好的用户体验
# 灵活输入即可(而非严格的输入)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~