From f1220dd49e0d7d48be4006140d294cc19164f313 Mon Sep 17 00:00:00 2001 From: "DESKTOP-DPA61F8\\Benedek" Date: Sun, 25 Apr 2021 16:38:46 +0200 Subject: [PATCH] Netwrapper key gen --- server/config_init.py | 4 ++-- server/netwrapper.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/config_init.py b/server/config_init.py index f2f9250..bf7ed08 100644 --- a/server/config_init.py +++ b/server/config_init.py @@ -12,7 +12,7 @@ def generatePrivateKeyForUser(auth: Authetication,username: str, user_passphrase private_key = RSA.generate(2048) public_key = private_key.publickey() - private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8)) + private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=user_passphrase, pkcs=8, protection="scryptAndAES128-CBC")) public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8)) ##Save private key in separate file @@ -44,7 +44,7 @@ def generatePrivateKeyForServer(auth: Authetication,passphrase: str) -> str: private_key = RSA.generate(2048) public_key = private_key.publickey() - private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=passphrase, pkcs=8)) + private_key_value = bytes.hex(private_key.exportKey('DER', passphrase=passphrase, pkcs=8, protection="scryptAndAES128-CBC")) public_key_value = bytes.hex(public_key.exportKey('DER', pkcs=8)) data['serverPrivateKey'] = private_key_value diff --git a/server/netwrapper.py b/server/netwrapper.py index 28516df..7148410 100644 --- a/server/netwrapper.py +++ b/server/netwrapper.py @@ -31,9 +31,9 @@ class NetWrapper: self.clientAddr = incommingJson['source'] self.currentUser = incommingJson['username'] self.currentClientPublicKey = self.clientPublicKey[self.currentUser] - retmsg = self.serverPrivateKey.decrypt(b64decode(incommingJson['message'])).decode('UTF-8') - rsakey = RSA.import_key(self.currentClientPublicKey) - cipher = PKCS1_OAEP.new(rsakey) + cipher_rsa = PKCS1_OAEP.new(self.serverPrivateKey) + retmsg = cipher_rsa.decrypt(b64decode(incommingJson['message'])).decode('UTF-8') + cipher = PKCS1_OAEP.new(self.currentClientPublicKey) identMsg = json.dumps( {'type': 'IDY', 'source': self.network.own_addr, 'message': b64encode(cipher.encrypt(retmsg.encode('UTF-8')))}).encode( @@ -51,8 +51,7 @@ class NetWrapper: def keyExchange(self) -> None: dh = pyDH.DiffieHellman() - rsakey = RSA.import_key(self.currentClientPublicKey) - cipher = PKCS1_OAEP.new(rsakey) + cipher = PKCS1_OAEP.new(self.currentClientPublicKey) 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') self.network.send_msg(self.clientAddr, jsonmsg) @@ -62,7 +61,8 @@ class NetWrapper: if not status: raise Exception('Network error during connection.') decodedmsg = json.loads(msg.decode('UTF-8')) - serverpubkey = int(self.serverPrivateKey.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8')) + cipher_rsa = PKCS1_OAEP.new(self.serverPrivateKey) + serverpubkey = int(cipher_rsa.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8')) self.cipherkey = dh.gen_shared_key(serverpubkey).encode('UTF-8') def login(self) -> bool: