Altm4nz's blog

WEB划水选手

密码学实验之手撸代码记录

前言

简单记录一下密码学实验的代码,不让使用引入包,写起来还挺麻烦的。

古典密码

古典密码只写了维吉尼亚

维吉尼亚

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# coding:utf-8
# author:altman
# Vigenere

def strlower(m):
for i in range(0,len(m)):
if( (ord(m[i])<=90) & (ord(m[i])>=65) ):
m = m[:i]+chr(ord(m[i])+32)+m[i+1:]
return m


def encrypto(m,key):
m = strlower(m)
key = strlower(key)
c=''
lenm = len(m)
lenk = len(key)
turns = lenm / lenk
Remaining = lenm % lenk
for i in range(0,turns):
for j in range(0,lenk):
c += chr(int(((ord(m[i*lenk+j])-97)+(ord(key[j])-97))%26 +97))
for i in range(0,Remaining):
c += chr(int(((ord(m[turns*lenk+i])-97)+(ord(key[i])-97))%26 +97))
return c

def decrypto(c,key):
c = strlower(c)
key = strlower(key)
m = ''
lenc =len(c)
lenk = len(key)
turns = lenc / lenk
Remaining = lenc % lenk
for i in range(0, turns):
for j in range(0, lenk):
m += chr(int(((ord(c[i * lenk + j]) - 97) - (ord(key[j]) - 97)) % 26 + 97))
for i in range(0, Remaining):
m += chr(int(((ord(c[turns * lenk + i]) - 97) - (ord(key[i]) - 97)) % 26 + 97))
return m


m='justatest'
key = 'altman'
c = encrypto(m,key) #jflfagedm
print c

m = decrypto(c,key)
print m

分组密码

DES

这个相对比较容易

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# coding:utf-8
# author:altman
# DES

#密文函数

def ip(str):
res=''
ip_box = [58, 50, 42, 34, 26, 18, 10, 2, 60, 52, 44, 36, 28, 20, 12, 4, 62, 54, 46, 38, 30, 22, 14, 6, 64, 56,48, 40, 32, 24, 16, 8, 57, 49, 41, 33, 25, 17, 9, 1, 59, 51, 43, 35, 27, 19, 11, 3, 61, 53, 45, 37,29, 21, 13, 5, 63, 55, 47, 39, 31, 23, 15, 7]
for i in ip_box:
res += str[i-1]
return res

def _ip(str):
x = ""
box_ip = [40,8,48,16,56,24,64,32,39,7,47,15,55,23,63,31,38,6,46,14,54,22,62,30,37,5,45,13,53,21,61,29,36,4,44,12,52,20,60,28,35,3,43,11,51,19,59,27,34,2,42,10,50,18,58,26,33,1,41,9,49,17,57,25]
for i in box_ip:
x += str[i-1]
return x


def e_box(str):
e = [32,1,2,3,4,5,4,5,6,7,8,9,8,9,10,11,12,13,12,13,14,15,16,17,16,17,18,19,20,21,20,21,22,23,24,25,24,25,26,27,28,29,28,29,30,31,32,1]
x = ""
for i in e:
x +=str[i-1]
return x

def xor(str_a,str_b):
x = ""
len_str= len(str_a)
for i in range(0,len_str):
res = int(str_a[i])^int(str_b[i])
if res == 1:
x +='1'
else:
x +='0'
return x

def s_box(str):
s = [[14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7, 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8, 4,
1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0, 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13],
[15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10, 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5, 0,
14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15, 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9],
[10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8, 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1, 13,
6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7, 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12],
[7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15, 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9, 10,
6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4, 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14],
[2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9, 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6, 4,
2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14, 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3],
[12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11, 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8, 9,
14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6, 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13],
[4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1, 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6, 1,
4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2, 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12],
[13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7,
11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11]]
k = 0
x = ''
for i in range(0,len(str),6):
a = str[i:i+6]
n = s[k][16*int((a[0]+a[5]),2)+int((a[1:5]),2)]
n = bin(n)[2:]
k += 1
while(4-len(n)):
n = '0' + n
x += n
return x


def p_box(str):
x = ""
p = [16,7,20,21,29,12,28,17,1,15,23,26,5,18,31,10,2,8,24,14,32,27,3,9,19,13,30,6,22,11,4,25]
for i in p:
x +=str[i-1]
return x

def F(str,key):
e_res = e_box(str)
xor_res = xor(e_res,key)
s_res = s_box(xor_res)
p_res = p_box(s_res)
return p_res

#密钥函数
def pc_1(str):
pc1 = [57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4]
x = ""
for i in pc1:
x+=str[i-1]
return x

def pc_2(str):
pc2 = [14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32]
x = ""
for i in pc2:
x+=str[i-1]
return x


def left(str,n):
x = str[0:n]
x = str[n:len(str)] + x
return x


def key_16(str):
key = pc_1(str)
key_c = key[:28]
key_d = key[28:]
key_final=[]
for i in range(0,16):
k= i+1
if k in [1,2,9,16]:
key_c = left(key_c, 1)
key_d = left(key_d, 1)
else:
key_c = left(key_c, 2)
key_d = left(key_d, 2)
key_x = key_c + key_d
key_x = pc_2(key_x)
key_final.append(key_x)
return key_final



def str2bin(str):
res = ''
for i in str:
x = bin(ord(i))[2:]
for i in range(0,8-len(x)):
x = '0' + x
res += x
return res


def bin2str(str):
res = ''
for i in range(0,len(str),8):
x = str[i:i+8]
x = int(x,2)
res += chr(x)
return res

def bin2hex(str):
res = ''
for i in range(0,len(str),8):
x = str[i:i+8]
x = int(x,2)
res += chr(x)
res = res.encode('hex')
return res

def hex2bin(str):
str = str.decode('hex')
res = str2bin(str)
return res

def encrypto(str,key):#输入字符串,返回16进制密文
c = ''
k = 0
while(str[k:k+8]):
m = str2bin(str[0:8])
m = ip(m)
m_l = m[:32]
m_r = m[32:]
key = str2bin(key)
key = key_16(key)
for i in range(0, 15):
tmp = m_r
m_r = xor(m_l, F(m_r, key[i]))
m_l = tmp
m_l = xor(m_l, F(m_r, key[15]))
m_final = m_l + m_r
m_final = _ip(m_final)
c += m_final
c = bin2hex(c)
k += 8
return c

def decrypto(str,key):#输入16进制密文,返回字符串明文
m = ''
i = 0
while (str[i:i + 16]):
c = hex2bin(str[i:i + 16])
key = str2bin(key)
key = key_16(key)
c = ip(c)
c_l = c[:32]
c_r = c[32:]
for k in range(15,0,-1):
tmp = c_r
c_r = xor(c_l, F(c_r, key[k]))
c_l = tmp
c_l = xor(c_l, F(c_r, key[0]))
c_final = c_l + c_r
c_final = _ip(c_final)
m += c_final
i += 16
return bin2str(m)

m='cumtcumt'
key='altmannb'
c='b171ccf168f7c7dd'
print encrypto(m,key)
print decrypto(c,key)

AES

aes写的我有点恶心,而且最后的加密结果和网上在线加密的不太一样,但是也实现了加密解密

至今未找到问题,有大佬发现了可以教我下。。。

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# coding:utf-8
# author:altman
# 伽罗华域以及一些转换函数
def str2bin(str):
res = ''
for i in str:
x = bin(ord(i))[2:]
for i in range(0,8-len(x)):
x = '0' + x
res += x
return res

def str2hex(str):
res = ''
for i in str:
x = hex(ord(i))[2:]
for i in range(0,2-len(x)):
x = '0' + x
res += x
return res.upper()

def hex2bin(str):
str = str.decode('hex')
res = str2bin(str)
return res

def G2(str):
str = hex2bin(str)
str = str[1:len(str)]+'0'
str = hex(int(str,2))[2:].upper()
if len(str)!=2:
for i in range(0,2-len(str)):
str = '0'+str
return str

def G4(str):
str = hex2bin(str)
str = str[2:len(str)]+'00'
str = hex(int(str,2))[2:].upper()
if len(str)!=2:
for i in range(0,2-len(str)):
str = '0'+str
return str

def G8(str):
str = hex2bin(str)
str = str[3:len(str)]+'000'
str = hex(int(str,2))[2:].upper()
if len(str)!=2:
for i in range(0,2-len(str)):
str = '0'+str
return str

def G(str1,str2):
if str1 == "01":
return str2.upper()
elif str1 == "02":
return G2(str2)
elif str1 == "03":
tmp = hex(int(G2(str2),16)^int(str2,16))[2:]
if len(tmp) != 2:
for i in range(0, 2 - len(tmp)):
tmp = '0' + tmp
return tmp.upper()
elif str1 == "09":
tmp = hex(int(G8(str2), 16) ^ int(str2, 16))[2:]
if len(tmp) != 2:
for i in range(0, 2 - len(tmp)):
tmp = '0' + tmp
return tmp.upper()
elif str1 == "0B":
tmp = hex(int(G8(str2), 16) ^ int(G2(str2), 16)^int(str2, 16))[2:]
if len(tmp) != 2:
for i in range(0, 2 - len(tmp)):
tmp = '0' + tmp
return tmp.upper()
elif str1 == "0D":
tmp = hex(int(G8(str2), 16) ^ int(G4(str2), 16) ^ int(str2, 16))[2:]
if len(tmp) != 2:
for i in range(0, 2 - len(tmp)):
tmp = '0' + tmp
return tmp.upper()
elif str1 == "0E":
tmp = hex(int(G8(str2), 16) ^ int(G4(str2), 16) ^ int(G2(str2), 16))[2:]
if len(tmp) != 2:
for i in range(0, 2 - len(tmp)):
tmp = '0' + tmp
return tmp.upper()

下面是主函数,需要import上面的文件

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# coding:utf-8
# author:altman
# AES (len(key)==128 & turn==10)

from galois import *
# 字节代换
def S_box(str):
x = ""
s = {"00":"63","01":"7C","02":"77","03":"7B","04":"F2","05":"6B","06":"6F","07":"C5","08":"30","09":"01","0A":"67","0B":"2B","0C":"FE","0D":"D7","0E":"AB","0F":"76","10":"CA","11":"82","12":"C9","13":"7D","14":"FA","15":"59","16":"47","17":"F0","18":"AD","19":"D4","1A":"A2","1B":"AF","1C":"9C","1D":"A4","1E":"72","1F":"C0","20":"B7","21":"FD","22":"93","23":"26","24":"36","25":"3F","26":"F7","27":"CC","28":"34","29":"A5","2A":"E5","2B":"F1","2C":"71","2D":"D8","2E":"31","2F":"15","30":"04","31":"C7","32":"23","33":"C3","34":"18","35":"96","36":"05","37":"9A","38":"07","39":"12","3A":"80","3B":"E2","3C":"EB","3D":"27","3E":"B2","3F":"75","40":"09","41":"83","42":"2C","43":"1A","44":"1B","45":"6E","46":"5A","47":"A0","48":"52","49":"3B","4A":"D6","4B":"B3","4C":"29","4D":"E3","4E":"2F","4F":"84","50":"53","51":"D1","52":"00","53":"ED","54":"20","55":"FC","56":"B1","57":"5B","58":"6A","59":"CB","5A":"BE","5B":"39","5C":"4A","5D":"4C","5E":"58","5F":"CF","60":"D0","61":"EF","62":"AA","63":"FB","64":"43","65":"4D","66":"33","67":"85","68":"45","69":"F9","6A":"02","6B":"7F","6C":"50","6D":"3C","6E":"9F","6F":"A8","70":"51","71":"A3","72":"40","73":"8F","74":"92","75":"9D","76":"38","77":"F5","78":"BC","79":"B6","7A":"DA","7B":"21","7C":"10","7D":"FF","7E":"F3","7F":"D2","80":"CD","81":"0C","82":"13","83":"EC","84":"5F","85":"97","86":"44","87":"17","88":"C4","89":"A7","8A":"7E","8B":"3D","8C":"64","8D":"5D","8E":"19","8F":"73","90":"60","91":"81","92":"4F","93":"DC","94":"22","95":"2A","96":"90","97":"88","98":"46","99":"EE","9A":"B8","9B":"14","9C":"DE","9D":"5E","9E":"0B","9F":"DB","A0":"E0","A1":"32","A2":"3A","A3":"0A","A4":"49","A5":"06","A6":"24","A7":"5C","A8":"C2","A9":"D3","AA":"AC","AB":"62","AC":"91","AD":"95","AE":"E4","AF":"79","B0":"E7","B1":"C8","B2":"37","B3":"6D","B4":"8D","B5":"D5","B6":"4E","B7":"A9","B8":"6C","B9":"56","BA":"F4","BB":"EA","BC":"65","BD":"7A","BE":"AE","BF":"08","C0":"BA","C1":"78","C2":"25","C3":"2E","C4":"1C","C5":"A6","C6":"B4","C7":"C6","C8":"E8","C9":"DD","CA":"74","CB":"1F","CC":"4B","CD":"BD","CE":"8B","CF":"8A","D0":"70","D1":"3E","D2":"B5","D3":"66","D4":"48","D5":"03","D6":"F6","D7":"0E","D8":"61","D9":"35","DA":"57","DB":"B9","DC":"86","DD":"C1","DE":"1D","DF":"9E","E0":"E1","E1":"F8","E2":"98","E3":"11","E4":"69","E5":"D9","E6":"8E","E7":"94","E8":"9B","E9":"1E","EA":"87","EB":"E9","EC":"CE","ED":"55","EE":"28","EF":"DF","F0":"8C","F1":"A1","F2":"89","F3":"0D","F4":"BF","F5":"E6","F6":"42","F7":"68","F8":"41","F9":"99","FA":"2D","FB":"0F","FC":"B0","FD":"54","FE":"BB","FF":"16"}
for i in range(0,len(str),2):
x += s[str[i:i+2]]
return x

def _S_box(str):
x = ""
s = {"00":"52","01":"09","02":"6A","03":"D5","04":"30","05":"36","06":"A5","07":"38","08":"BF","09":"40","0A":"A3","0B":"9E","0C":"81","0D":"F3","0E":"D7","0F":"FB","10":"7C","11":"E3","12":"39","13":"82","14":"9B","15":"2F","16":"FF","17":"87","18":"34","19":"8E","1A":"43","1B":"44","1C":"C4","1D":"DE","1E":"E9","1F":"CB","20":"54","21":"7B","22":"94","23":"32","24":"A6","25":"C2","26":"23","27":"3D","28":"EE","29":"4C","2A":"95","2B":"0B","2C":"42","2D":"FA","2E":"C3","2F":"4E","30":"08","31":"2E","32":"A1","33":"66","34":"28","35":"D9","36":"24","37":"B2","38":"76","39":"5B","3A":"A2","3B":"49","3C":"6D","3D":"8B","3E":"D1","3F":"25","40":"72","41":"F8","42":"F6","43":"64","44":"86","45":"68","46":"98","47":"16","48":"D4","49":"A4","4A":"5C","4B":"CC","4C":"5D","4D":"65","4E":"B6","4F":"92","50":"6C","51":"70","52":"48","53":"50","54":"FD","55":"ED","56":"B9","57":"DA","58":"5E","59":"15","5A":"46","5B":"57","5C":"A7","5D":"8D","5E":"9D","5F":"84","60":"90","61":"D8","62":"AB","63":"00","64":"8C","65":"BC","66":"D3","67":"0A","68":"F7","69":"E4","6A":"58","6B":"05","6C":"B8","6D":"B3","6E":"45","6F":"06","70":"D0","71":"2C","72":"1E","73":"8F","74":"CA","75":"3F","76":"0F","77":"02","78":"C1","79":"AF","7A":"BD","7B":"03","7C":"01","7D":"13","7E":"8A","7F":"6B","80":"3A","81":"91","82":"11","83":"41","84":"4F","85":"67","86":"DC","87":"EA","88":"97","89":"F2","8A":"CF","8B":"CE","8C":"F0","8D":"B4","8E":"E6","8F":"73","90":"96","91":"AC","92":"74","93":"22","94":"E7","95":"AD","96":"35","97":"85","98":"E2","99":"F9","9A":"37","9B":"E8","9C":"1C","9D":"75","9E":"DF","9F":"6E","A0":"47","A1":"F1","A2":"1A","A3":"71","A4":"1D","A5":"29","A6":"C5","A7":"89","A8":"6F","A9":"B7","AA":"62","AB":"0E","AC":"AA","AD":"18","AE":"BE","AF":"1B","B0":"FC","B1":"56","B2":"3E","B3":"4B","B4":"C6","B5":"D2","B6":"79","B7":"20","B8":"9A","B9":"DB","BA":"C0","BB":"FE","BC":"78","BD":"CD","BE":"5A","BF":"F4","C0":"1F","C1":"DD","C2":"A8","C3":"33","C4":"88","C5":"07","C6":"C7","C7":"31","C8":"B1","C9":"12","CA":"10","CB":"59","CC":"27","CD":"80","CE":"EC","CF":"5F","D0":"60","D1":"51","D2":"7F","D3":"A9","D4":"19","D5":"B5","D6":"4A","D7":"0D","D8":"2D","D9":"E5","DA":"7A","DB":"9F","DC":"93","DD":"C9","DE":"9C","DF":"EF","E0":"A0","E1":"E0","E2":"3B","E3":"4D","E4":"AE","E5":"2A","E6":"F5","E7":"B0","E8":"C8","E9":"EB","EA":"BB","EB":"3C","EC":"83","ED":"53","EE":"99","EF":"61","F0":"17","F1":"2B","F2":"04","F3":"7E","F4":"BA","F5":"77","F6":"D6","F7":"26","F8":"E1","F9":"69","FA":"14","FB":"63","FC":"55","FD":"21","FE":"0C","FF":"7D"}
for i in range(0, len(str), 2):
x += s[str[i:i+2]]
return x

#行移位
def move(str):
x = ''
j=0
for i in range(0,len(str),8):
x += (str[i+j*2:i+8]+str[i:i+j*2])
if j == 3:
j = 0
else:
j += 1
return x

def _move(str):
x = ''
j=0
for i in range(0,len(str),8):
x += (str[i+(8-j*2):i+8]+str[i:i+(8-j*2)])
if j == 3:
j = 0
else:
j += 1
return x

def str2bin(str):
res = ''
for i in str:
x = bin(ord(i))[2:]
for i in range(0,8-len(x)):
x = '0' + x
res += x
return res


def hex2bin(str):
str = str.decode('hex')
res = str2bin(str)
return res

#列混合

def str2matrix(str):#转矩阵
matrix = [[0 for i in range(4)] for i in range(4)]
for i in range(0,4):
for j in range(0,4):
matrix[i][j] = str[(i*4+j)*2:(i*4+j)*2+2]
return matrix


def mix(str):
col = [["02", "03", "01", "01"], ["01", "02", "03", "01"], ["01", "01", "02", "03"], ["03", "01", "01", "02"]]
s = str2matrix(str)
res = ""
for i in range(0,len(s)): #i是行 j是列
for j in range(0,len(s[i])):
tmp_ascii = 0x00
for n in range(0,len(s[i])):
tmp_ascii = tmp_ascii^int(G(col[i][n],s[n][j]),16)
tmp = hex(tmp_ascii)[2:]
if len(tmp) != 2:
for sb in range(0, 2 - len(tmp)):
tmp = "0" + tmp
res += tmp
return res.upper()

def _mix(str):
col = [["0E", "0B", "0D", "09"], ["09", "0E", "0B", "0D"], ["0D", "09", "0E", "0B"], ["0B", "0D", "09", "0E"]]
s = str2matrix(str)
res = ""
for i in range(0,len(s)): #i是行 j是列
for j in range(0,len(s[i])):
tmp_ascii = 0x00
for n in range(0,len(s[i])):
tmp_ascii = tmp_ascii^int(G(col[i][n],s[n][j]),16)
tmp = hex(tmp_ascii)[2:]
if len(tmp) != 2:
for sb in range(0, 2 - len(tmp)):
tmp = "0" + tmp
res += tmp
return res.upper()

def ni_key_col_mix(str):
col = [["0E", "0B", "0D", "09"], ["09", "0E", "0B", "0D"], ["0D", "09", "0E", "0B"], ["0B", "0D", "09", "0E"]]
s = str2matrix(str)
res = [["0E", "0B", "0D", "09"], ["09", "0E", "0B", "0D"], ["0D", "09", "0E", "0B"], ["0B", "0D", "09", "0E"]]
for i in range(0,len(s)): #i是行 j是列
for j in range(0,len(s[i])):
tmp_ascii = 0x00
for n in range(0,len(s[i])):
tmp_ascii = tmp_ascii ^ int(G(col[i][n], s[n][j]), 16)
tmp = hex(tmp_ascii)[2:]
if len(tmp) != 2:
for sb in range(0, 2 - len(tmp)):
tmp = "0" + tmp
res[i][j] = tmp.upper()
return res

def ni_key_mix(str1,str2):
my_str1 = ni_key_col_mix(str1)
my_str2 = ni_key_col_mix(str2)
res = ""
for i in range(0,len(my_str1)):
for j in range(0,len(my_str2)):
tmp = hex(int(my_str1[i][j],16)^int(my_str2[i][j],16))[2:]
if len(tmp) != 2:
for j in range(0, 2 - len(tmp)):
tmp = "0" + tmp
res +=tmp
return res.upper()


def xor(str1,str2):
x = ''
for i in range(0,len(str1),2):
tmp = hex(int(str1[i:i+2],16)^int(str2[i:i+2],16))[2:]
if len(tmp)!=2 :
tmp = '0' + tmp
x += tmp
return x.upper()

def F(str):
str = S_box(str)
str = move(str)
str = mix(str)
return str

# 密钥处理
def R(str,i):
Rcon = [0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x1B,0x36]
str = hex(int(str[0:2],16)^Rcon[i-1])[2:]+str[2:]
return str.upper()

def T(str,i):
str = str[2:]+str[0:2]
str = S_box(str)
str = R(str,i)
return str.upper()

def key_10(str):
key_final=[]
key_final.append(str)
for i in range(0,10):
key = xor(str[i * 32:i * 32+ 8],T(str[i * 32+24:i * 32+32],i+1))
str += key
for j in range(1,4):
tmp = str[i * 32+j*8:i * 32+j*8 + 8]
tmp1 = str[i * 32+j*8+24:i * 32+j*8 + 32]
tmp = xor(tmp, tmp1)
key += tmp
str += tmp
key_final.append(key.upper())
return key_final


# 加密程序

def encrypto(str,key):
m = str2hex(str)
key = str2hex(key)
key = key_10(key)
m = xor(m,key[0])
for i in range(1,10):
m = F(m)
m = xor(m,key[i])
m = S_box(m)
m = move(m)
m = xor(m,key[10])
return m

def decrypto(str,key):#16进制输入
key = str2hex(key)
key = key_10(key)
str = xor(str, key[10])
i = 9
while i>0:
str = _S_box(str)
str = _move(str)
str = ni_key_mix(str,key[i])
i = i - 1
str = _S_box(str)
str = _move(str)
str = xor(str, key[0])
return str.decode('hex')


key='zydzydzydzydzydq'
str='abcdefghjigklmno'
print encrypto(str,key)
print decrypto(encrypto(str,key),key)

序列密码

RC4

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
36
37
38
39
40
41
42
43
44
# coding:utf-8
# author:altman
# RC4

def S_box(k):
S = [] * 255
T = [] * 256
for i in range(0,256):
S.append(i)
T.append(ord(k[i % len(k)]))
j = 0
for i in range(0,256):
j = (j + S[i] + T[i]) % 256
tmp = S[i]
S[i] = S[j]
S[j] = tmp
return S


def RC4(m,k):
c = ''
i = j = 0
S = S_box(k)
for n in range(len(m)):
i = (i + 1) % 256
j = (j + S[i]) % 256
tmp = S[i]
S[i] = S[j]
S[j] = tmp
t = (S[i] + S[j]) % 256
c += hex( (ord(m[n]) ^ S[t]) )[2:]
return c



key = 'altman'
message = 'justatest'
c = RC4(message,key)
print "key: "+key
print "密文: "+c

c='8684737f9e4ff9b1b8'
c = RC4(c.decode('hex'),key).decode("hex")
print "明文: "+c

hash

md5

这个写起来也很麻烦,目前只实现了明文长度在一个分组内的加密,超过512bit的加密有点小问题,有时间再改吧。

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# coding:utf-8
# author:altman
# md5
import math

A = '0x67452301'
B = '0xefcdab89'
C = '0x98badcfe'
D = '0x10325476'

def str2hex(str):
res = ''
for i in str:
x = hex(ord(i))[2:]
for i in range(0,2-len(x)):
x = '0' + x
res += x
return res
# 填充
def appd(str):
str = str2hex(str)
lenth = len(str)*4
str += '80'
while (len(str) * 4 + 64) % 512 != 0:
str += '00'
len_hex = (hex(lenth))[2:]
if len(len_hex)%2!=0:
len_hex ='0'+len_hex
while (len(len_hex) * 4) != 64:
len_hex += '00'
str += len_hex
return str



# 移位
def lift(str,n):
res = (((str << n) | (str >> (32 - n))) & 0xffffffff)
return res

# 步函数
def F(x,y,z):
res = (x & y) | ((~x) & z)
return res

def G(x,y,z):
res = ( x & z) | (y & (~z))
return res

def H(x,y,z):
return (x^y^z)

def I(x,y,z):
res = y ^ (x | (~z))
return res


def T(i):
result = (int(4294967296 * abs(math.sin(i)))) & 0xffffffff
return hex(result)[2:]

def re(str):
res = ''
for i in range(0,len(str),2):
res += str[len(str)-i-2:len(str)-i]
return res




shi_1 = (7, 12, 17, 22) * 4
shi_2 = (5, 9, 14, 20) * 4
shi_3 = (4, 11, 16, 23) * 4
shi_4 = (6, 10, 15, 21) * 4

m_1 = (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
m_2 = (1, 6, 11, 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12)
m_3 = (5, 8, 11, 14, 1, 4, 7, 10, 13, 0, 3, 6, 9, 12, 15, 2)
m_4 = (0, 7, 14, 5, 12, 3, 10, 1, 8, 15, 6, 13, 4, 11, 2, 9)


def FF(abcd,fun,m,shi):
global t_count
count =0
while count<16:
tmp = int(abcd[0],16) + fun(int(abcd[1], 16), int(abcd[2], 16), int(abcd[3], 16))
tmp += int(m[count],16)
tmp += int(T(t_count),16)
tmp = tmp & 0xffffffff
tmp2 = lift(tmp,shi[count])
abcd[0] = hex((int(abcd[1], 16) + tmp2) & 0xffffffff)
abcd = [abcd[3], abcd[0], abcd[1], abcd[2]]
count +=1
t_count +=1
return abcd

def M_16(m,str,t):
count = 0
M = [0] * 16
for i in m:
i=i*8
M[count] = re(str[i + t:i + t+2] + str[i + 2 + t:i + 4 + t] + str[i + 4 + t:i + 6 + t] + str[i + 6 + t:i + 8 + t])
count +=1
return M

t_count=1
def md5(str):
str = appd(str)
abcd=[A,B,C,D]
for i in range(0, len(str) / 128):
M_1 = M_16(m_1, str, i)
abcd = FF(abcd, F, M_1, shi_1)
M_2 = M_16(m_2, str, i)
abcd = FF(abcd, G, M_2, shi_2)
M_3 = M_16(m_3, str, i)
abcd = FF(abcd, H, M_3, shi_3)
M_4 = M_16(m_4, str, i)
abcd = FF(abcd, I, M_4, shi_4)
final_a = re(hex(int(abcd[0], 16) + int(A, 16))[2:])
final_b = re(hex(int(abcd[1], 16) + int(B, 16))[2:])
final_c = re(hex(int(abcd[2], 16) + int(C, 16))[2:])
final_d = re(hex(int(abcd[3], 16) + int(D, 16))[2:])
abcd =[final_a,final_b,final_c,final_d]
final= final_a+final_b+final_c+final_d
return final



str = 'a'
print md5(str)

公钥密码

RSA

这个最简单了,随便找了对pq。

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
36
37
38
# coding:utf-8
# author:altman
# RSA

def hex2str(str):
res=''
if len(str)%2 != 0:
str = str +'0'
for i in range(0,len(str),2):
res += chr(int(str[i:i+2],16))
return res

# 求模反
def egcd(a,b):
if b==0:
return a,1,0
else:
g,x,y=egcd(b,a%b)
return g,y,x-a//b*y

def encrypto(str,n,e):
str = int(str.encode("hex"),16)
c = pow(str,e,n)
return c

def decrypto(c,n,d):
m = pow(c,d,n)
m = hex2str(hex(m)[2:].replace("L",""))
return m


p=275127860351348928173285174381581152299
q=319576316814478949870590164193048041239
n=p*q
e=65537
d=egcd(e,(p-1)*(q-1))[1]
print encrypto('altman',n,e)
print decrypto(encrypto('altman',n,e),n,d)

//以后再补充修改