1. 程式人生 > 實用技巧 >Poem Codes - 攻防世界(Decrypt-the-Message)

Poem Codes - 攻防世界(Decrypt-the-Message)

⭐Poem Codes

Poem Code 最顯著的特點就是一首詩歌。 詳情請戳這裡


讓我們一起來過濾一遍這個神奇的加密過程~

① 給出一首詩歌

for my purpose holds to sail beyond the sunset, and the baths of all the western stars until I die.

② 給出5個關鍵單詞。

“for”, “sail”, “all”, “stars”, “die.”

對其進行拆散:

f o r s a i l a l l s t a r s d i e

接下來按照 字母表順序 進行編號,若遇相同字母,則繼續 +1

f o r s a i l a
6 12 13 15 1 7 9 2
l l s t a r s d
10 11 16 18 3 14 17 4
i e
8 5

③ 將要傳遞的訊息進行加密。

We have run out of cigars, situation desperate。

先對對其進行編碼。因為給出的5個關鍵詞,其長度為18.所以以18為一組。

若一組長度不滿18,則用abc(不要求有序)進行補充。



將排好的訊息,按照之前給出的詩歌字母編號寫下密文。

for my purpose holds to sail beyond the sunset, and the baths of all the western stars until I die.

如, for --> eud tdk oek 那麼得到的又可以按照5個(適當個數)為一組進行重新分組,得到最後密文。


⭐例題

接下來我們來看一題。 【攻防世界】題目連結 【Decrypt-the-Message】


審題解題

首先我們得到一首詩歌

The life that I have
Is all that I have
And the life that I have
Is yours.

The love that I have
Of the life that I have
Is yours and yours and yours.

A sleep I shall have
A rest I shall have
Yet death will be but a pause.

For the peace of my years
In the long green grass
Will be yours and yours and yours.

以及 decrypted message (解密訊息)


emzcf sebt yuwi ytrr ortl rbon aluo konf ihye cyog rowh prhj feom ihos perp twnb tpak heoc yaui usoa irtd tnlu ntke onds goym hmpq


解題指令碼


已知原理,我們可以運用網上大佬的 解密工具 ,解密指令碼如下:

import sys
import itertools
from os import listdir
from os.path import isfile, join

abc='abcdefghijklmnopqrstuvwxyz'

def loadlist(infile):
	tlist = []
	for line in open(infile,'r'):
		for w in line.split(): tlist.append(w.lower())
	return tlist

def encrypt(code, poem, msg):
	# Load all words of the poem into a temporary list
	twords = loadlist(poem)

	# Select only those words specified in the code in a new list
	pwords = ''
	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 here
	if plen > len(abc): sys.exit(3)

	# Assign an index for each letter in the key based on the alphabet
	pcode = [None] * plen
	count = 0
	while(count<plen):
		for al in abc:
			for pc, pl in enumerate(pwords):
				if al!=pl: continue
				pcode[pc]=count
				count+=1

	# Load all words of the message into a string
	mwords = ''
	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 necessary
	cpairs = []
	curlen = plen
	while(curlen<mlen):
		cpairs.append(mwords[curlen-plen:curlen])
		curlen+=plen
	rword = mwords[curlen-plen:curlen]
	rlen = len(rword)
	if rlen < plen: rword += abc[:plen-rlen]
	cpairs.append(rword)

	# Encrypt the message according to the key
	cip = ''
	for i in code: cip+=abc[i]
	cip+=' '
	for i in pcode:
		for pair in cpairs:
			cip += pair[i]
		cip+=' '
	return cip

def decrypt(poem, cip):
	# Load all words of the poem into a temporary list
	twords = loadlist(poem)

	# Load all cipher chunks of the ciphertext into a list
	cwords = loadlist(cip)

	# Get the code rom the first chunk and remove it from the ciphertext list
	code = []
	for i in cwords.pop(0):
		code.append(abc.index(i))
	
	# Select only those words specified in the code in a new multi-arrayed list
	xwords = [[] for x in range(len(code))]
	for xcount, c in enumerate(code):
		tlen = c
		while(c<len(twords)):
			xwords[xcount].append(twords[c].lower())
			c+=26

	# Get all possible combinations
	for comb in itertools.product(*xwords):
		pwords = ''
		for c in comb: pwords+=c
		plen = len(pwords)

		# Rearrange the chunks according to the key
		pcode = [None] * plen
		count = 0
		while(count<plen):
			for al in abc:
				for pc, pl in enumerate(pwords):
					if al!=pl: continue
					pcode[count]=cwords[pc]
					count+=1

		# Decrypt the ciphertext
		msg = ''
		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])

【注意】 該指令碼是python2哦!把詩歌命名為 poem 資訊命名為 msg

python poemcode.py poem msg

或者直接下載整份github原始檔:執行如下命令:

python poemcode.py examples/2/ctfpoem  examples/2/ctfcip


最終找到flag

ifyouthinkcryptographyistheanswertoyourproblemthenyoudonotknowwhatyourproblemisabcdefghijklmnopqrstu

【侵權刪】【參考連結】:https://blog.csdn.net/dchua123/article/details/105470394/