【python密碼學編程】7.暴力破解凱撒加密法

 1 # _*_ coding:utf-8 _*_
 2 #Caeser Ciper
 3 import pyperclip
 4 messgae = 'GUVF VF ZL FRPERG ZRFFTNR.'
 5 nums = range(1,27)
 6 mode = 'decrypt'    #模式是加密而非解密(decrypt)
 7 LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'    #常量要大寫
 8 translated = ''
 9 for key in nums:
10     translated =''    
11     for symbol in messgae:
12         if symbol in LETTERS:
13             num = LETTERS.find(symbol)    #find()返回的是該字符在字符串中的位置
14 
15             if mode == 'encrypt':#判斷當前是加密仍是解密過程
16                 num = num+key
17             elif mode == 'decrypt':
18                 num = num-key
19 
20             if num > len(LETTERS):#當處理後的數字脫離26個字母的數字範圍後的處理
21                 num = num - len(LETTERS)
22             elif num < 0:
23                 num = num + len(LETTERS)
24 
25             translated = translated + LETTERS[num]#空字符串一個個加上翻譯後獲得的字符
26 
27         else:
28             #若是要翻譯的字符不在26個大寫字母的範圍內
29             translated =translated + symbol
30 
31     print key ,translated.lower()

 

1 ftue ue yk eqodqf yqeesmq.
2 estd td xj dpncpe xpddrlp.
3 drsc sc wi combod woccqko.
4 cqrb rb vh bnlanc vnbbpjn.
5 bpqa qa ug amkzmb umaaoim.
6 aopz pz tf zljyla tlzznhl.
7 znoy oy se ykixkz skyymgk.
8 ymnx nx rd xjhwjy rjxxlfj.
9 xlmw mw qc wigvix qiwwkei.
10 wklv lv pb vhfuhw phvvjdh.
11 vjku ku oa ugetgv oguuicg.
12 uijt jt nz tfdsfu nftthbf.
13 this is my secret messgae.
14 sghr hr lx rdbqds ldrrfzd.
15 rfgq gq kw qcapcr kcqqeyc.
16 qefp fp jv pbzobq jbppdxb.
17 pdeo eo iu oaynap iaoocwa.
18 ocdn dn ht nzxmzo hznnbvz.
19 nbcm cm gs mywlyn gymmauy.
20 mabl bl fr lxvkxm fxllztx.
21 lzak ak eq kwujwl ewkkysw.
22 kyzj zj dp jvtivk dvjjxrv.
23 jxyi yi co iushuj cuiiwqu.
24 iwxh xh bn htrgti bthhvpt.
25 hvwg wg am gsqfsh asgguos.
26 guvf vf zl frperg zrfftnr.
[Finished in 0.2s]

   僅容許非商業轉載,轉載請註明出處ui

相關文章
相關標籤/搜索