一、题目
收到一首英文诗歌和一段密文,要求很简单,就是解密这个密文
二、解题
1、背景知识PoemCode
参考文章:https://blog.csdn.net/xiao__1bai/article/details/120250452
2、解密
了解加密原理即可,解密过程很复杂,可以直接用现成的脚本
- 脚本地址:https://github.com/abpolym/crypto-tools/tree/master/poemcode
- 脚本用法:`python poemcode.py poem.txt msg.txt
- 脚本用法参考:https://www.cnblogs.com/zhengna/p/14763913.html
3、答案
从输出中找到有实际意义的语段,即为明文
flag:ifyouthinkcryptographyistheanswertoyourproblemthenyoudonotknowwhatyourproblemisabcdefghijklmnopqrstu
3、脚本代码
import sys
import itertools
from os import listdir
from os.path import isfile, joinabc = 'abcdefghijklmnopqrstuvwxyz'def loadlist(infile):tlist = []for line in open(infile, 'r'):for w in line.split(): tlist.append(w.lower())return tlistdef encrypt(code, poem, msg):# Load all words of the poem into a temporary listtwords = loadlist(poem)# Select only those words specified in the code in a new listpwords = ''for c in code: pwords += twords[c].lower()plen = len(pwords)# We can only support encoding all alphabetical letters, a key length greater len(abc) is not reasonable hereif plen > len(abc): sys.exit(3)# Assign an index for each letter in the key based on the alphabetpcode = [None] * plencount = 0while (count < plen):for al in abc:for pc, pl in enumerate(pwords):if al != pl: continuepcode[pc] = countcount += 1# Load all words of the message into a stringmwords = ''for line in open(msg, 'r'):for w in line.split(): mwords += w.lower()mlen = len(mwords)# Split message into chunks of size plen, append random (here alphabet) characters to fill the last chunk, if necessarycpairs = []curlen = plenwhile (curlen < mlen):cpairs.append(mwords[curlen - plen:curlen])curlen += plenrword = mwords[curlen - plen:curlen]rlen = len(rword)if rlen < plen: rword += abc[:plen - rlen]cpairs.append(rword)# Encrypt the message according to the keycip = ''for i in code: cip += abc[i]cip += ' 'for i in pcode:for pair in cpairs:cip += pair[i]cip += ' 'return cipdef decrypt(poem, cip):# Load all words of the poem into a temporary listtwords = loadlist(poem)# Load all cipher chunks of the ciphertext into a listcwords = loadlist(cip)# Get the code rom the first chunk and remove it from the ciphertext listcode = []for i in cwords.pop(0):code.append(abc.index(i))# Select only those words specified in the code in a new multi-arrayed listxwords = [[] for x in range(len(code))]for xcount, c in enumerate(code):tlen = cwhile (c < len(twords)):xwords[xcount].append(twords[c].lower())c += 26# Get all possible combinationsfor comb in itertools.product(*xwords):pwords = ''for c in comb: pwords += cplen = len(pwords)# Rearrange the chunks according to the keypcode = [None] * plencount = 0while (count < plen):for al in abc:for pc, pl in enumerate(pwords):if al != pl: continuepcode[count] = cwords[pc]count += 1# Decrypt the ciphertextmsg = ''wlen = len(pcode[0])for c in range(0, wlen):for word in pcode:msg += word[c]print(msg)# first argument = poem
# second argument = ciphertxt or msg
if len(sys.argv) != 3:sys.exit(2)# print encrypt([0, 5, 13, 16, 19], sys.argv[1], sys.argv[2])
decrypt(sys.argv[1], sys.argv[2])