Connecting client - netwrapper
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-04-25 16:08:04 +02:00
parent 284cfff0a0
commit c61994a3cf
2 changed files with 77 additions and 64 deletions

View File

@@ -4,17 +4,15 @@ import string
import json
from base64 import b64encode, b64decode
import pyDH
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Cipher import ChaCha20
from Crypto.PublicKey.RSA import RsaKey
from Crypto.Random import get_random_bytes
from netsim import network_interface
class NetWrapper:
def __init__(self, privateKey: str, clientAddress: str, username: str, serverPubKey: str = "",
def __init__(self, privateKey: RsaKey, clientAddress: str, username: str, serverPubKey: RsaKey,
serverAddr: str = 'A'):
# Create network_interface: network_interface(path, addr) path root is shared with network / addr is own address
self.network = network_interface('./', clientAddress)
@@ -24,17 +22,17 @@ class NetWrapper:
self.serverPubKey = serverPubKey
self.cipherkey = "".encode('UTF-8')
def randomStringGenerator(self, str_size: int = 512,
allowed_chars: str = string.ascii_letters + string.punctuation) -> str:
return ''.join(random.choice(allowed_chars) for x in range(str_size))
def identifyServer(self) -> bool:
randommsg = self.randomStringGenerator()
rsakey = RSA.import_key(self.serverPubKey)
cipher = PKCS1_OAEP.new(rsakey)
identMsg = json.dumps(
{'type': 'IDY', 'source': self.network.own_addr, 'username': self.username,
'message': b64encode(cipher.encrypt(randommsg.encode('UTF-8')))}).encode(
'message': b64encode(self.privateKey.encrypt(randommsg.encode('UTF-8')))}).encode(
'UTF-8')
self.network.send_msg(self.serverAddr, identMsg)
returnJson = {'source': '', 'type': ''}
@@ -43,16 +41,13 @@ class NetWrapper:
if not status:
raise Exception('Network error during connection.')
returnJson = json.loads(msg.decode('UTF-8'))
myrsakey = RSA.import_key(self.privateKey)
mycipher = PKCS1_OAEP.new(myrsakey)
retmsg = mycipher.decrypt(b64decode(returnJson['message'])).decode('UTF-8')
retmsg = self.privateKey.decrypt(b64decode(returnJson['message'])).decode('UTF-8')
return retmsg == randommsg
def createEncryptedChannel(self):
dh = pyDH.DiffieHellman()
rsakey = RSA.import_key(self.serverPubKey)
cipher = PKCS1_OAEP.new(rsakey)
mypubkey = b64encode(cipher.encrypt(str(dh.gen_public_key()).encode('UTF-8')))
mypubkey = b64encode(self.serverPubKey.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': ''}
@@ -61,11 +56,10 @@ class NetWrapper:
if not status:
raise Exception('Network error during connection.')
decodedmsg = json.loads(msg.decode('UTF-8'))
myrsakey = RSA.import_key(self.privateKey)
mycipher = PKCS1_OAEP.new(myrsakey)
serverpubkey = int(mycipher.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8'))
serverpubkey = int(self.privateKey.decrypt(b64decode(decodedmsg['message'])).decode('UTF-8'))
self.cipherkey = dh.gen_shared_key(serverpubkey).encode('UTF-8')
def authenticate(self, password: str):
message = f"LIN {self.username} {password}".encode('UTF-8')
cipher = ChaCha20.new(self.cipherkey, get_random_bytes(12))
@@ -91,6 +85,7 @@ class NetWrapper:
except Exception:
print("Incorrect decryption")
def connectToServer(self):
identStatus = self.identifyServer()
if not identStatus:
@@ -100,6 +95,7 @@ class NetWrapper:
pw = input()
self.authenticate(pw)
def sendMessage(self, message: bytes):
cipher = ChaCha20.new(self.cipherkey, get_random_bytes(12))
ciphertext = cipher.encrypt(message)
@@ -109,9 +105,10 @@ class NetWrapper:
'UTF-8')
self.network.send_msg(self.serverAddr, sendjson)
def recieveMessage(self) -> bytes:
b64 = {'source': '', 'type': ''}
while not (b64['source'] == self.serverAddr and b64['type'] == 'AUT'):
while not (b64['source'] == self.serverAddr and b64['type'] == 'AUT'):
status, msg = self.network.receive_msg(blocking=True)
if not status:
raise Exception('Network error during connection.')