Bugku-托马斯.杰斐逊-转轮密码

题目:

1: <ZWAXJGDLUBVIQHKYPNTCRMOSFE <
2: <KPBELNACZDTRXMJQOYHGVSFUWI <
3: <BDMAIZVRNSJUWFHTEQGYXPLOCK <
4: <RPLNDVHGFCUKTEBSXQYIZMJWAO <
5: <IHFRLABEUOTSGJVDKCPMNZQWXY <
6: <AMKGHIWPNYCJBFZDRUSLOQXVET <
7: <GWTHSPYBXIZULVKMRAFDCEONJQ <
8: <NOZUTWDCVRJLXKISEFAPMYGHBQ <
9: <QWATDSRFHENYVUBMCOIKZGJXPL <
10: <WABMCXPLTDSRJQZGOIKFHENYVU <
11: <XPLTDAOIKFZGHENYSRUBMCQWVJ <
12: <TDSWAYXPLVUBOIKZGJRFHENMCQ <
13: <BMCSRFHLTDENQWAOXPYVUIKZGJ <
14: <XPHKZGJTDSENYVUBMLAOIRFCQW <

密钥: 2,5,1,3,6,4,9,7,8,14,10,13,11,12

密文:HCBTSXWCRQGLES

flag格式 flag{你解密的内容}

转轮密码解密过程:

1.根据秘钥将字符串排序

2.根据密文字符将字符串分成两部分,将后面一部分(含密文字符)与前面一部分进行交换顺序;如,

KPBELNACZDTRXMJQOYHGVSFUWI

按照秘钥字符H进行排序后为:

HGVSFUWIKPBELNACZDTRXMJQOY

3.观察转换后的字符串的列


解密思路

1.首先要将字符串部分提取出来,使用了正则表达式

2.由于需要先对照秘钥,按照顺序进行字符串操作,因此用循环遍历秘钥;在遍历秘钥前对其进行转换成列表,且不含逗号

3.由于需要将字符串分成两部分,因此需要对字符串进行切片;由于切片需要字符在字符串中的下标,用到了python的index()函数,返回字符在字符串中首次出现的下标


脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
key_index = '2,5,1,3,6,4,9,7,8,14,10,13,11,12'
key_index = key_index.split(',')

key_letter = 'HCBTSXWCRQGLES'

data = '''
1: <ZWAXJGDLUBVIQHKYPNTCRMOSFE <
2: <KPBELNACZDTRXMJQOYHGVSFUWI <
3: <BDMAIZVRNSJUWFHTEQGYXPLOCK <
4: <RPLNDVHGFCUKTEBSXQYIZMJWAO <
5: <IHFRLABEUOTSGJVDKCPMNZQWXY <
6: <AMKGHIWPNYCJBFZDRUSLOQXVET <
7: <GWTHSPYBXIZULVKMRAFDCEONJQ <
8: <NOZUTWDCVRJLXKISEFAPMYGHBQ <
9: <QWATDSRFHENYVUBMCOIKZGJXPL <
10: <WABMCXPLTDSRJQZGOIKFHENYVU <
11: <XPLTDAOIKFZGHENYSRUBMCQWVJ <
12: <TDSWAYXPLVUBOIKZGJRFHENMCQ <
13: <BMCSRFHLTDENQWAOXPYVUIKZGJ <
14: <XPHKZGJTDSENYVUBMLAOIRFCQW <
'''

import re
content = re.findall('<(.*?) <',data)

indexs = 0
for i in key_index:
i = int(i) - 1
mes = content[i]
position = mes.index('%s' % key_letter[indexs])
splits = mes[position:]
origin = mes[:position]
new_mes = splits + origin
print(new_mes)
indexs += 1

输出为:

HGVSFUWIKPBELNACZDTRXMJQOY
CPMNZQWXYIHFRLABEUOTSGJVDK
BVIQHKYPNTCRMOSFEZWAXJGDLU
TEQGYXPLOCKBDMAIZVRNSJUWFH
SLOQXVETAMKGHIWPNYCJBFZDRU
XQYIZMJWAORPLNDVHGFCUKTEBS
WATDSRFHENYVUBMCOIKZGJXPLQ
CEONJQGWTHSPYBXIZULVKMRAFD
RJLXKISEFAPMYGHBQNOZUTWDCV
QWXPHKZGJTDSENYVUBMLAOIRFC
GOIKFHENYVUWABMCXPLTDSRJQZ
LTDENQWAOXPYVUIKZGJBMCSRFH
ENYSRUBMCQWVJXPLTDAOIKFZGH
SWAYXPLVUBOIKZGJRFHENMCQTD

发现倒数第6列为

XSXSBUGKUADMIN