From 10c481bc7243ad9f80ee98cf2c9f96af1bc40a57 Mon Sep 17 00:00:00 2001 From: "DESKTOP-DPA61F8\\Benedek" Date: Sun, 25 Apr 2021 16:38:57 +0200 Subject: [PATCH] Netwrapper key gen --- client/client.py | 8 ++++---- client/netwrapper.py | 16 ++++++++++------ 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/client/client.py b/client/client.py index f189116..f4cbdfc 100644 --- a/client/client.py +++ b/client/client.py @@ -24,7 +24,7 @@ def loadPublicKey() -> RsaKey: except ValueError: print('Invalid server public key!') sys.exit(1) - return RSA.import_key(key) + return rsaKey def loadPrivateKey(passphrase: str) -> RsaKey: @@ -88,7 +88,6 @@ SERVER_PUBLIC_KEY = loadPublicKey() CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE) CLIENT_ADDRESS = loadAddress() - while True: command = input("Type a command:") separatedCommand = command.split(" ") @@ -102,6 +101,7 @@ while True: network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, separatedCommand[1], SERVER_PUBLIC_KEY) if not network.identifyServer(): print('Server identification failed!') + continue network.createEncryptedChannel() network.authenticate(password=separatedCommand[2]) @@ -155,5 +155,5 @@ while True: printCommand() - except Exception: - print('Error: '+Exception.args) \ No newline at end of file + except Exception as e: + print('Error: '+str(e)) \ No newline at end of file diff --git a/client/netwrapper.py b/client/netwrapper.py index 0070f1b..7738380 100644 --- a/client/netwrapper.py +++ b/client/netwrapper.py @@ -4,7 +4,7 @@ import string import json from base64 import b64encode, b64decode import pyDH -from Crypto.Cipher import ChaCha20 +from Crypto.Cipher import ChaCha20, PKCS1_OAEP from Crypto.PublicKey.RSA import RsaKey from Crypto.Random import get_random_bytes @@ -30,24 +30,27 @@ class NetWrapper: def identifyServer(self) -> bool: randommsg = self.randomStringGenerator() + cipher_rsa = PKCS1_OAEP.new(self.serverPubKey) identMsg = json.dumps( {'type': 'IDY', 'source': self.network.own_addr, 'username': self.username, - 'message': b64encode(self.privateKey.encrypt(randommsg.encode('UTF-8')))}).encode( + 'message': b64encode(cipher_rsa.encrypt(randommsg.encode('UTF-8')))}).encode( 'UTF-8') self.network.send_msg(self.serverAddr, identMsg) returnJson = {'source': '', 'type': ''} - while not (returnJson['source'] == self.serverAddr and returnJson['type'] == 'IDY'): + while not (returnJson['source'] == self.serverAddr and returnJson['type'] == 'IDY'): status, msg = self.network.receive_msg(blocking=True) if not status: raise Exception('Network error during connection.') returnJson = json.loads(msg.decode('UTF-8')) - retmsg = self.privateKey.decrypt(b64decode(returnJson['message'])).decode('UTF-8') + cipher_rsa = PKCS1_OAEP.new(self.privateKey) + retmsg = cipher_rsa.decrypt(b64decode(returnJson['message'])).decode('UTF-8') return retmsg == randommsg def createEncryptedChannel(self): dh = pyDH.DiffieHellman() - mypubkey = b64encode(self.serverPubKey.encrypt(str(dh.gen_public_key()).encode('UTF-8'))) + cipher_rsa = PKCS1_OAEP.new(self.serverPubKey) + mypubkey = b64encode(cipher_rsa.encrypt(str(dh.gen_public_key()).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) decodedmsg = {'source': '', 'type': ''} @@ -56,7 +59,8 @@ class NetWrapper: if not status: raise Exception('Network error during connection.') decodedmsg = json.loads(msg.decode('UTF-8')) - serverpubkey = int(self.privateKey.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8')) + cipher_rsa = PKCS1_OAEP.new(self.privateKey) + serverpubkey = int(cipher_rsa.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8')) self.cipherkey = dh.gen_shared_key(serverpubkey).encode('UTF-8')