常见的古典密码与常用脚本

栅栏密码

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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

a = open(r'crypto4.txt','r')
ciphertext = a.read()
b = len(ciphertext)

print('The ciphertext :',ciphertext)
print('The length of ciphertext is',b)

#将字符串转化为数组形式
ciphertext = [ciphertext[i:i+1] for i in range(0,b,1)]

#获取可以解密的栅栏数
c =[]
num = 1
while num <= b:
if b%num == 0:
c.append(num)
else:
pass
num += 1
print('每栏个数可为:',c)
#将密文按照栅栏数进行分组
for step in c:
d = [ciphertext[i:i+step] for i in range(0,b,step)]
flag = ''
for i in range(step):
for x in d:
e = x[i]
flag += e
print('解密时每栏个数:',step,'\n',d,'\n','result:',flag)

培根密码

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
#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import re

#密文转化为指定格式
s = 'AAAAABAABBBAABBAAAAAAAABAABABAAAAAAABBABAAABBAAABBAABAAAABABAABAAABBABAAABAAABAABABBAABBBABAAABABABBAAABBABAAABAABAABAAAABBABBAABBAABAABAAABAABAABAABABAABBABAAAABBABAABBA'
a = s.lower()

#字典
CODE_TABLE = {
'a':'aaaaa','b':'aaaab','c':'aaaba','d':'aaabb','e':'aabaa','f':'aabab','g':'aabba',
'h':'aabbb','i':'abaaa','j':'abaab','k':'ababa','l':'ababb','m':'abbaa','n':'abbab',
'o':'abbba','p':'abbbb','q':'baaaa','r':'baaab','s':'baaba','t':'baabb','u':'babaa',
'v':'babab','w':'babba','x':'babbb','y':'bbaaa','z':'bbaab'
}

#5个一组进行切割并解密
def peigendecode(peigen):
msg =''
codes = re.findall(r'.{5}', a)
for code in codes:
if code =='':
msg += ' '
else:
UNCODE =dict(map(lambda t:(t[1],t[0]),CODE_TABLE.items()))
msg += UNCODE[code]
return msg

flag = peigendecode(a)
print('flag is ',flag)

轮转机密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import re 
sss='1: < ZWAXJGDLUBVIQHKYPNTCRMOSFE < 2: < KPBELNACZDTRXMJQOYHGVSFUWI < 3: < BDMAIZVRNSJUWFHTEQGYXPLOCK < 4: < RPLNDVHGFCUKTEBSXQYIZMJWAO < 5: < IHFRLABEUOTSGJVDKCPMNZQWXY < 6: < AMKGHIWPNYCJBFZDRUSLOQXVET < 7: < GWTHSPYBXIZULVKMRAFDCEONJQ < 8: < NOZUTWDCVRJLXKISEFAPMYGHBQ < 9: < XPLTDSRFHENYVUBMCQWAOIKZGJ < 10: < UDNAJFBOWTGVRSCZQKELMXYIHP < 11 < MNBVCXZQWERTPOIUYALSKDJFHG < 12 < LVNCMXZPQOWEIURYTASBKJDFHG < 13 < JZQAWSXCDERFVBGTYHNUMKILOP < '
m="NFQKSEVOQOFNP"
content=re.findall(r'< (.*?) <',sss,re.S)
iv=[2,3,7,5,13,12,9,1,8,10,4,11,6]
vvv=[]
ans=""
for i in range(13):
index=content[iv[i]-1].index(m[i])
vvv.append(index)

for i in range(0,26):
flag=""
for j in range(13):
flag+=content[iv[j]-1][(vvv[j]+i)%26]
print flag

仿射密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# coding:utf-8
# a,b为y=ax+b的系数
# y = 17*x-8 flag{szzyfimhyzd} 答案格式:flag{********}
# Crypto为密文,res为明文,暴力破解得到,不用求取逆元
import string
crypto = "szzyfimhyzd"
a = 17
b = -8
word = string.ascii_lowercase
# print word
res = ''
for c in crypto:
for w in word:
if ord(c)-97 == ((ord(w)-97)*a + b)%26:
res += w
print res
# affineshift

进制转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import binascii

text = "d87 x65 x6c x63 o157 d109 o145 b100000 d116 b1101111 o40 x6b b1100101 b1101100 o141 d105 x62 d101 b1101001 d46 o40 d71 x69 d118 x65 x20 b1111001 o157 b1110101 d32 o141 d32 d102 o154 x61 x67 b100000 o141 d115 b100000 b1100001 d32 x67 o151 x66 d116 b101110 b100000 d32 d102 d108 d97 o147 d123 x31 b1100101 b110100 d98 d102 b111000 d49 b1100001 d54 b110011 x39 o64 o144 o145 d53 x61 b1100010 b1100011 o60 d48 o65 b1100001 x63 b110110 d101 o63 b111001 d97 d51 o70 d55 b1100010 d125 x20 b101110 x20 b1001000 d97 d118 o145 x20 d97 o40 d103 d111 d111 x64 d32 o164 b1101001 x6d o145 x7e"
solution = ''
text2 = text.split(' ')
for x in text2:
print x
if x[0] == 'b': #binary
solution += chr(int(x[1:],2))
elif x[0] == 'x': # hexadecimal
solution += x[1:].decode("hex")
elif x[0] == 'd': # decimal
solution += chr(int(x[1:]))
elif x[0] == 'o': # octal
solution += chr(int(x[1:],8))
print solution

16进制10进制2进制转字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import libnum
import binascii
s="flag{pcat}"
#print libnum.s2n(s)
print binascii.hexlify(s)


n=0x666c61677b706361747d
#print libnum.n2s(n)
print binascii.unhexlify(str(n))


b='01110000011000110110000101110100'
print libnum.b2s(b)

unicode 转换

1
2
3
4
5
6
7
8
9
10
11
a='\u76;\u122;\u69;\u120;\u79;\u83;\u56;\u120;\u77;\u68;\u69;\u118;\u77;\u84;\u65;\u52;\u76;\u122;\u107;\u53;\u76;\u122;\u69;\u120;\u77;\u83;\u56;\u120;\u77;\u68;\u107;\u118;\u77;\u84;\u65;\u120;\u76;\u122;\u69;\u120;\u78;\u105;\u56;\u120;\u77;\u84;\u69;\u118;\u79;\u84;\u99;\u118;\u77;\u84;\u69;\u50;\u76;\u122;\u69;\u120;\u78;\u105;\u56;\u53;\u78;\u121;\u56;\u53;\u79;\u83;\u56;\u120;\u77;\u68;\u99;\u118;\u79;\u84;\u99;\u118;\u77;\u84;\u69;\u119;\u76;\u122;\u69;\u119;\u77;\u67;\u56;\u120;\u77;\u68;\u65;\u118;\u77;\u84;\u65;\u120;\u76;\u122;\u69;\u119;\u77;\u105;\u56;\u120;\u77;\u68;\u69;\u118;\u77;\u84;\u69;\u119;\u76;\u122;\u107;\u53;\u76;\u122;\u69;\u119;\u77;\u83;\u56;\u120;\u77;\u84;\u107;\u118;\u77;\u84;\u69;\u120;\u76;\u122;\u69;\u120;\u78;\u67;\u56;\u120;\u77;\u68;\u103;\u118;\u77;\u84;\u65;\u119;'
b=a.decode("utf-8")
print(b)

方法:
s_unicode = u'\u810f\u4e71'
s_str = s_unicode.encode('unicode-escape').decode('string_escape')

问题二:
将'\u810f\u4e71'转换为u'\u810f\u4e71'
方法:s_str = '\u810f\u4e71's_unicode = s_str.decode('unicode-escape')

数字转字符串

1
2
3
4
5
6
7
8
9
10
11
12
13
14
a=[119,101,108,99,111,109,101,116,111,97,116,116,97,99,107,97,110,100,100,101,102,101,110,99,101,119,111,114,108,100]
s=''
for i in a:
s+=chr(i)
print(s)


s = '102 108 97 103 123 115 113 108 109 52 112 95 49 53 95 112 48 119 101 114 102 117 108 125'
ls = s.split(' ')
# print ls
flag = ''
for i in ls:
flag += chr(int(i))
print flag

字符串转16进制

1
2
import binascii
print(binascii.b2a_hex('addddaasasabcd'))
-------------本文结束感谢您的阅读-------------
/*
*/