forked from AdrainYe/CTF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sha-1(notes).py
182 lines (98 loc) · 3.74 KB
/
Sha-1(notes).py
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
#!/usr/bin/env python
#-*- coding:utf-8 -*-
'''
Sha1类接收inputText参数(明文)
'''
class Sha1:
#接收方法
def __init__(self,inputText):
self.inputText = inputText
self.WtList = []
#扩充字符串到固定位数(不够就补0)
def FillBin(self,bin,num):
need = num - len(bin)
if need != 0:
bin = '0'*need + bin
return bin
#初始话字符串的时候需要扩充到512位
def FillInitialBin(self,firstText):
need = 448 - len(firstText)
if need >= 1:
firstText = firstText + '1' + (need-1)*'0' + self.FillBin(bin(len(self.inputText)*8)[2:],64)
return firstText
#调用字符串扩充方法,将处理后得到的结果返回
def InitialText(self):
return self.FillInitialBin(''.join([self.FillBin(bin(int(ord(i)))[2:],8) for i in self.inputText]))
#返回Kt常量
def ReturnKt(self):
return ['5a827999','6ed9eba1','8f1bbcdc','ca62c1d6']
#初始化寄存器
def InitialRegister(self):
return ['67452301','efcdab89','98badcfe','10325476','c3d2e1f0']
#十六进制转二进制
def HexToBin(self,hexString):
return ''.join([self.FillBin(bin(int(i,16))[2:],4) for i in hexString])
#二进制转十六进制
def BinToHex(self,binString):
return ''.join([hex(int(binString[4*i:4*(i+1)],2))[2:] for i in range(len(binString)/4)])
#讲bin字符串左移num个位数
def LeftMove(self,bin,num):
for i in range(num):
bin = bin[1:] + bin[0]
return bin
#F函数操作
def OperatorF(self,BCD,t):
BCD = int((BCD),2)
f = [[0,1,0,1,0,0,1,1],[0,1,1,0,1,0,0,1],[0,0,0,1,0,1,1,1],[0,1,1,0,1,0,0,1]]
return str(f[t/20][BCD])
#对number取余数
def GetMod(self,number,mod):
return number%mod
#对这一轮的E寄存器操作并将结果返回个A寄存器
def EToA(self,f,E,LeftMoveA,w,k):
first = self.GetMod(int(f,2)+int(E,2),2**32)
second = self.GetMod(first+int(LeftMoveA,2),2**32)
third = self.GetMod(second+int(w,2),2**32)
forth = self.GetMod(third+int(k,2),2**32)
return self.FillBin(bin(forth)[2:],32)
#根据轮数返回Wt
def Wt(self,num):
allInitialText = self.InitialText()
if num <= 15:
return allInitialText[num*32:(num+1)*32]
else:
number = ''.join([str(int(a)^int(b)^int(c)^int(d)) for a,b,c,d in zip(self.WtList[num-16],self.WtList[num-14],self.WtList[num-8],self.WtList[num-3])])
return self.LeftMove(number,1)
#根据轮数返回Kt
def Kt(self,num):
return self.HexToBin(self.ReturnKt()[num/20])
#加密控制器
def Control(self):
getInitialRegister = [self.HexToBin(i) for i in self.InitialRegister()]
A,B,C,D,E = getInitialRegister[0],getInitialRegister[1],getInitialRegister[2],getInitialRegister[3],getInitialRegister[4]
initalA,initalB,initalC,initalD,initalE = A,B,C,D,E
for num in range(80):
print '-'*50
w = self.Wt(num)
print num
self.WtList.append(w)
k = self.Kt(num)
f = ''.join([self.OperatorF(b+c+d,num) for b,c,d in zip(list(B),list(C),list(D))])
A,B,C,D,E = self.EToA(f,E,self.LeftMove(A,5),w,k),A,self.LeftMove(B,30),C,D
print 'A:' + self.BinToHex(A)
print 'B:' + self.BinToHex(B)
print 'C:' + self.BinToHex(C)
print 'D:' + self.BinToHex(D)
print 'E:' + self.BinToHex(E)
A = self.BinToHex(self.FillBin(bin(self.GetMod(int(A,2)+int(initalA,2),2**32))[2:],32))
B = self.BinToHex(self.FillBin(bin(self.GetMod(int(B,2)+int(initalB,2),2**32))[2:],32))
C = self.BinToHex(self.FillBin(bin(self.GetMod(int(C,2)+int(initalC,2),2**32))[2:],32))
D = self.BinToHex(self.FillBin(bin(self.GetMod(int(D,2)+int(initalD,2),2**32))[2:],32))
E = self.BinToHex(self.FillBin(bin(self.GetMod(int(E,2)+int(initalE,2),2**32))[2:],32))
print '-'*50
print 'The secrect text is:' + A+B+C+D+E
def main():
sha1 = Sha1('abc')
sha1.Control()
if __name__ == '__main__':
main()