This commit is contained in:
parent
4854a71b32
commit
8eea92a714
@ -37,12 +37,14 @@ class NetWrapper:
|
|||||||
'message': b64encode(cipher.encrypt(randommsg.encode('UTF-8')))}).encode(
|
'message': b64encode(cipher.encrypt(randommsg.encode('UTF-8')))}).encode(
|
||||||
'UTF-8')
|
'UTF-8')
|
||||||
self.network.send_msg(self.serverAddr, identMsg)
|
self.network.send_msg(self.serverAddr, identMsg)
|
||||||
status, msg = self.network.receive_msg(blocking=True)
|
returnJson = {'source': '', 'type': ''}
|
||||||
if not status:
|
while not (returnJson['source'] == self.serverAddr and returnJson['type'] == 'IDY'):
|
||||||
raise Exception('Network error during connection.')
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
|
if not status:
|
||||||
|
raise Exception('Network error during connection.')
|
||||||
|
returnJson = json.loads(msg.decode('UTF-8'))
|
||||||
myrsakey = RSA.import_key(self.privateKey)
|
myrsakey = RSA.import_key(self.privateKey)
|
||||||
mycipher = PKCS1_OAEP.new(myrsakey)
|
mycipher = PKCS1_OAEP.new(myrsakey)
|
||||||
returnJson = json.loads(msg.decode('UTF-8'))
|
|
||||||
retmsg = mycipher.decrypt(b64decode(returnJson['message'])).decode('UTF-8')
|
retmsg = mycipher.decrypt(b64decode(returnJson['message'])).decode('UTF-8')
|
||||||
return retmsg == randommsg
|
return retmsg == randommsg
|
||||||
|
|
||||||
@ -53,10 +55,12 @@ class NetWrapper:
|
|||||||
mypubkey = b64encode(cipher.encrypt(str(dh.gen_public_key()).encode('UTF-8')))
|
mypubkey = b64encode(cipher.encrypt(str(dh.gen_public_key()).encode('UTF-8')))
|
||||||
jsonmsg = json.dumps({'type': 'DH', 'source': self.network.own_addr, 'message': mypubkey}).encode('UTF-8')
|
jsonmsg = json.dumps({'type': 'DH', 'source': self.network.own_addr, 'message': mypubkey}).encode('UTF-8')
|
||||||
self.network.send_msg(self.serverAddr, jsonmsg)
|
self.network.send_msg(self.serverAddr, jsonmsg)
|
||||||
status, msg = self.network.receive_msg(blocking=True)
|
decodedmsg = {'source': '', 'type': ''}
|
||||||
if not status:
|
while not (decodedmsg['source'] == self.serverAddr and decodedmsg['type'] == 'DH'):
|
||||||
raise Exception('Network error during connection.')
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
decodedmsg = json.loads(msg.decode('UTF-8'))
|
if not status:
|
||||||
|
raise Exception('Network error during connection.')
|
||||||
|
decodedmsg = json.loads(msg.decode('UTF-8'))
|
||||||
myrsakey = RSA.import_key(self.privateKey)
|
myrsakey = RSA.import_key(self.privateKey)
|
||||||
mycipher = PKCS1_OAEP.new(myrsakey)
|
mycipher = PKCS1_OAEP.new(myrsakey)
|
||||||
serverpubkey = int(mycipher.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8'))
|
serverpubkey = int(mycipher.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8'))
|
||||||
@ -70,12 +74,13 @@ class NetWrapper:
|
|||||||
ct = b64encode(ciphertext).decode('UTF-8')
|
ct = b64encode(ciphertext).decode('UTF-8')
|
||||||
sendjson = json.dumps({'type': 'AUT', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode(
|
sendjson = json.dumps({'type': 'AUT', 'source': self.network.own_addr, 'nonce': nonce, 'message': ct}).encode(
|
||||||
'UTF-8')
|
'UTF-8')
|
||||||
self.network.send_msg(self.serverAddr, sendjson)
|
b64 = {'source': '', 'type': ''}
|
||||||
status, msg = self.network.receive_msg(blocking=True)
|
while not (b64['source'] == self.serverAddr and b64['type'] == 'AUT'):
|
||||||
if not status:
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
raise Exception('Network error during connection.')
|
if not status:
|
||||||
|
raise Exception('Network error during connection.')
|
||||||
|
b64 = json.loads(msg.decode('UTF-8'))
|
||||||
try:
|
try:
|
||||||
b64 = json.loads(msg)
|
|
||||||
retnonce = b64decode(b64['nonce'])
|
retnonce = b64decode(b64['nonce'])
|
||||||
retciphertext = b64decode(b64['message'])
|
retciphertext = b64decode(b64['message'])
|
||||||
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
|
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
|
||||||
@ -104,11 +109,13 @@ class NetWrapper:
|
|||||||
self.network.send_msg(self.serverAddr, sendjson)
|
self.network.send_msg(self.serverAddr, sendjson)
|
||||||
|
|
||||||
def recieveMessage(self) -> bytes:
|
def recieveMessage(self) -> bytes:
|
||||||
status, msg = self.network.receive_msg(blocking=True)
|
b64 = {'source': '', 'type': ''}
|
||||||
if not status:
|
while not (b64['source'] == self.serverAddr and b64['type'] == 'AUT'):
|
||||||
raise Exception('Network error during connection.')
|
status, msg = self.network.receive_msg(blocking=True)
|
||||||
|
if not status:
|
||||||
|
raise Exception('Network error during connection.')
|
||||||
|
b64 = json.loads(msg.decode('UTF-8'))
|
||||||
try:
|
try:
|
||||||
b64 = json.loads(msg)
|
|
||||||
retnonce = b64decode(b64['nonce'])
|
retnonce = b64decode(b64['nonce'])
|
||||||
retciphertext = b64decode(b64['message'])
|
retciphertext = b64decode(b64['message'])
|
||||||
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
|
retcipher = ChaCha20.new(self.cipherkey, nonce=retnonce)
|
||||||
|
Loading…
Reference in New Issue
Block a user