-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
executable file
·248 lines (186 loc) · 6.51 KB
/
Copy pathuser.py
File metadata and controls
executable file
·248 lines (186 loc) · 6.51 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
sys.path.append('/home/Py/encript')
from ECPoint import ECPoint
from DSGost import DSGost
from stribog import GOST
from MySQL import DB
class User():
RSALenghtKey = 2048
def __init__(self, login, port):
self.login = login
self.port = port
sql = """SELECT passwd from Users
WHERE login='%(l)s'
"""%{"l":self.login}
d = DB('localhost','root','messenger','root')
q = d.execute(sql)
print q
if q is None:
self.password = q
else:
self.password = q[0]
self.__pswd = hex(GOST(256).hash(self.password.encode('hex')))[:32]
self.signature()
def signature(self):
p = 6277101735386680763835789423207666416083908700390324961279
a = -3
b = 0x64210519e59c80e70fa7e9ab72243049feb8deecc146b9b1
xG = '03188da80eb03090f67cbf20eb43a18800f4ff0afd82ff1012'
n = 0xffffffffffffffffffffffff99def836146bc9b1b4d22831
#n = primeNum().get()
self.DS = DSGost(p, a, b, n, xG)
self.b = ECPoint(1791047505851178572875763820410146702188686496182717679368L, 3671238363959597127547523270057700245059762540500241556950L, -3, 2455155546008943817740293915197451784769108058161191238065L, 6277101735386680763835789423207666416083908700390324961279L)
def start(self):
self.setPort(self.port)
self.decriptRSAKey()
self.decriptDSKey()
def reg(self, p):
self.password = hex(GOST(256).hash(p.encode('hex') + self.login.encode('hex')))
self.__pswd = hex(GOST(256).hash(self.password.encode('hex')))[:32]
sql = """INSERT INTO Users(login, passwd)
VALUES ('%(l)s', '%(p)s')
"""%{"l":self.login, "p": self.password}
d = DB('localhost','root','messenger','root')
d.execute(sql)
self.genRSAKey()
self.genDSKey()
return self.password
def genRSAKey(self):
key = RSA.generate(self.RSALenghtKey)
pub = key.publickey().exportKey()
priv = key.exportKey()
AES.block_size = 32
iv = os.urandom(8).encode('hex')
# Encryption
cipher = AES.new(self.__pswd, AES.MODE_CFB, iv)
priv = cipher.encrypt(priv)
sql = """INSERT INTO RSA(login, pub, priv, iv)
VALUES ('%(l)s', '%(pub)s', '%(priv)s', '%(iv)s')
"""%{"l":self.login,"pub":pub.encode('hex'), "priv":priv.encode('hex'), "iv":iv.encode('hex')}
d = DB('localhost','root','messenger','root')
d.execute(sql)
def updateRSAKey(self):
key = RSA.generate(self.RSALenghtKey)
pub = key.publickey().exportKey()
priv = key.exportKey()
AES.block_size = 32
iv = os.urandom(8).encode('hex')
# Encryption
cipher = AES.new(self.__pswd, AES.MODE_CFB, iv)
priv = cipher.encrypt(priv)
sql = """UPDATE RSA
SET pub='%(pub)s', priv='%(priv)s', iv='%(iv)s'
WHERE login = '%(l)s'
"""%{"l":self.login,"pub":pub.encode('hex'), "priv":priv.encode('hex'), "iv":iv.encode('hex')}
d = DB('localhost','root','messenger','root')
d.execute(sql)
def genDSKey(self):
priv = self.DS.GenPrivateKey()
pub = self.DS.GenPublicKey(priv)
pub = pub.getAttr()
AES.block_size = 32
iv = os.urandom(8).encode('hex')
# Encryption
cipher = AES.new(self.__pswd, AES.MODE_CFB, iv)
priv = cipher.encrypt(hex(priv))
sql = """INSERT INTO DS(login, x, y, a, b, fieldchar, priv, iv)
VALUES ('%(l)s', '%(x)s', '%(y)s', '%(a)s', '%(b)s', '%(f)s', '%(priv)s', '%(iv)s')
"""%{"l":self.login,"x":hex(pub[0]), "y":hex(pub[1]), "a":hex(pub[2]), "b":hex(pub[3]), "f":hex(pub[4]), "priv":priv.encode('hex'), "iv":iv.encode('hex')}
d = DB('localhost','root','messenger','root')
d.execute(sql)
def updateDSKey(self):
priv = self.DS.GenPrivateKey()
pub = self.DS.GenPublicKey(priv)
pub = pub.getAttr()
AES.block_size = 32
iv = os.urandom(8).encode('hex')
# Encryption
cipher = AES.new(self.__pswd, AES.MODE_CFB, iv)
priv = cipher.encrypt(hex(priv))
sql = """UPDATE DS
SET x='%(x)s', y ='%(y)s', priv='%(priv)s', iv='%(iv)s'
WHERE login = '%(l)s'
"""%{"x":hex(pub[0]), "y":hex(pub[1]), "priv":priv.encode('hex'), "iv":iv.encode('hex')}
d = DB('localhost','root','messenger','root')
d.execute(sql)
def checkSign(self, login):
sql = """SELECT x, y from DS
WHERE login='%(l)s'
"""%{"l":login}
d = DB('localhost','root','messenger','root')
E = d.execute(sql)
E = map(lambda h: int(h[2:-1], 16), E)
Q = ECPoint(E[0], E[1], a, b, p)
return DS.SingVer(H, sign, Q)
#print map(lambda b: int(b, 16), E)
def decriptDSKey(self):
sql = """SELECT priv, iv from DS
WHERE login='%(l)s'
"""%{"l":self.login}
d = DB('localhost','root','messenger','root')
key, iv = d.execute(sql)
key = key.decode('hex')
iv = iv.decode('hex')
cipher = AES.new(self.__pswd, AES.MODE_CFB, iv)
key = cipher.decrypt(key)
self.__PrivDSKey = key
def decriptRSAKey(self):
sql = """SELECT priv, iv from RSA
WHERE login='%(l)s'
"""%{"l":self.login}
d = DB('localhost','root','messenger','root')
key, iv = d.execute(sql)
key = key.decode('hex')
iv = iv.decode('hex')
cipher = AES.new(self.__pswd, AES.MODE_CFB, iv)
key = cipher.decrypt(key)
self.__PrivRSAKey = RSA.importKey(key)
def encryptMsg(self, login, msg):
sql = """SELECT pub from RSA
WHERE login='%(l)s'
"""%{"l":login}
d = DB('localhost','root','messenger','root')
key = d.execute(sql)[0]
key = key.decode('hex')
PubKey = RSA.importKey(key)
iv = os.urandom(4).encode('hex')
H = GOST(256).hash(msg.encode('hex'))
print self.__PrivDSKey
self.DS.GenPublicKey(self.__PrivDSKey,self.b)
sign = self.DS.SingGen(32, self.__PrivDSKey)
pack = "#---#".join(msg, sign)
print pack
cipher_text = ''.join(PubKey.encrypt(pack, iv)).encode('hex')
print 'messenge encrypt'
return
def decryptMsg(self, msg):
cipher_text = str(msg).decode('hex')
msg = self.__PrivRSAKey.decrypt(cipher_text)
print 'messenge decrypt'
return msg
def setPort(self, port):
sql = """UPDATE Users
SET port='%(port)s'
WHERE login = '%(l)s'
"""%{"l":self.login,"port":port}
d = DB('localhost','root','messenger','root')
d.execute(sql)
if __name__ == '__main__':
login = 'user'
password = 'password'
password = hex(GOST(256).hash(password.encode('hex') + login.encode('hex')))
sql = """INSERT INTO Users(login, passwd)
VALUES ('%(l)s', '%(p)s')
"""%{"l":login, "p": password}
d = DB('localhost','root','messenger','root')
d.execute(sql)
#Q = newUser.genRSAKey()
#newUser.genRSAKey()
#newUser.decriptDSKey()
#print Q
#print Q.decode('hex')