1. 程式人生 > 實用技巧 >攻防世界-密碼學-shanghai

攻防世界-密碼學-shanghai

1. 題目資訊

題目給出提示“維吉利亞密碼”,密文在附件中。

2. 分析

由於維吉利亞密碼加密時金鑰重用,導致可通過分析密文進行破解;由於明文中存在一些出現頻率很高的單詞:the,and,for,with等等,因此很有可能這些單詞再次被同樣的金鑰加密,從而生成同樣的密文,其間隔一定是金鑰長度的倍數,因此可通過對密文進行分析得到金鑰的長度;另外,由於這些密文對應著那些出現頻率高的明文,因此可破解出金鑰;自己寫了一個工具來求解這類問題。

3. 解題

如下是我使用工具的解題過程,註釋是我自己加的,並非程式的輸出

$ python solve.py shanghai.txt
# 先尋找密文中多次出現(本程式設定的至少3次)的詞彙,並計算它們之間的距離,這有助於分析出金鑰的長度
word:  bju
distance:  [1078, 2222, 154, 814, 132, 264]
the minimum distance:  132

word:  glv
distance:  [660, 408, 186, 374, 88, 66, 836, 44, 649, 110, 880, 88, 209, 66]
the minimum distance:  44

word:  vxz
distance:  [1936, 154, 264, 253, 121, 517, 154, 440, 11, 220, 66]
the minimum distance:  11

word:  bni
distance:  [22, 979, 2002, 583, 165, 132, 22]
the minimum distance:  22

word:  xpg
distance:  [11, 396, 660, 2409, 11, 374, 37, 51, 572]
the minimum distance:  11

word:  jcm
distance:  [1595, 88, 99, 297, 781, 11, 22, 110, 66, 231, 627]
the minimum distance:  11

word:  zlr
distance:  [286, 583, 583, 154, 22, 154, 22, 407, 55, 396, 1155]
the minimum distance:  22

word:  klm
distance:  [594, 308, 605, 2145, 231, 297, 44, 44, 198]
the minimum distance:  44

word:  mfr
distance:  [44, 99, 99, 715, 1884, 536]
the minimum distance:  44

word:  gvtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  vtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  tyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  yiz
distance:  [165, 968, 550, 275, 220, 2167, 246, 106]
the minimum distance:  106

word:  opk
distance:  [66, 1342, 385, 11, 1771, 583, 374]
the minimum distance:  11

word:  tyi
distance:  [165, 968, 488, 62, 275, 220, 37, 1639, 491, 352]
the minimum distance:  37

word:  gvtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  vtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  pkv
distance:  [297, 33, 407, 1221, 572, 88]
the minimum distance:  33

word:  gvtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  vtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  xui
distance:  [638, 682, 77, 154, 55, 649, 363, 143]
the minimum distance:  55

word:  tui
distance:  [55, 1628, 154, 286, 787, 797]
the minimum distance:  55

word:  gvtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165

word:  vtyiz
distance:  [165, 968, 550, 275, 220, 2167, 352]
the minimum distance:  165
#到此我們應該也能看出,金鑰的長度為11
#接下來,進行已知明文攻擊
#密文中有一段:frxnimp 1914 qil 1940,猜測是between 1914 and 1940
plaintext:betweenand
cipher:frxnimpqil
possible key:enereicqvi len:10
#如果需要更多的明-密文對分析,則輸入y;否則,已計算出金鑰,則可以進行解密你需要解密的內容
try more plain-cipher(y) or start decrypt(n)y
plaintext:the
cipher:opk
possible key:vig len:3
try more plain-cipher(y) or start decrypt(n)n
key:enereicqvig
cipher:jtcw, '{' vvj 'zvkvrmtudabiecveaaxpp' grq '}'
fgyf, '{' rnh 'jacpnzpdzszsjupanwglh' ebv '}'
dpps, '{' erb 'xfpnligqmwtgohnywntyl' ypa '}'
bnyj, '{' ref 'rtuajgphzjxacmawuwkly' cjo '}'
olws, '{' irs 'vnifwenqqwkewafjsutcl' pni '}'
tyuq, '{' rif 'irctbrloznxrautofsrlc' cam '}'
hdho, '{' prw 'vegnpwymxwoenynckfpjl' tnz '}'
brmb, '{' npf 'mrtrjkdzvuxvalrwykchj' cem '}'
flag, '{' and 'vigenereisveryeasyhuh' and '}'
spuu, '{' fab 'trxrailsnftcaprnwsvzu' ylm '}'
fcyo, '{' tfo 'rpginvpmbkgayyiajwpnz' ljk '}'
wpls, '{' ntt 'enereicqvylnwwrrwjthn' qwi '}'

解出flag為flag{vigenereisveryeasyhuh}。