Netwrapper key gen
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
DESKTOP-DPA61F8\Benedek 2021-04-25 16:38:57 +02:00
parent c61994a3cf
commit 10c481bc72
2 changed files with 14 additions and 10 deletions

View File

@ -24,7 +24,7 @@ def loadPublicKey() -> RsaKey:
except ValueError: except ValueError:
print('Invalid server public key!') print('Invalid server public key!')
sys.exit(1) sys.exit(1)
return RSA.import_key(key) return rsaKey
def loadPrivateKey(passphrase: str) -> RsaKey: def loadPrivateKey(passphrase: str) -> RsaKey:
@ -88,7 +88,6 @@ SERVER_PUBLIC_KEY = loadPublicKey()
CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE) CLIENT_PRIVATE_KEY = loadPrivateKey(PASSPHRASE)
CLIENT_ADDRESS = loadAddress() CLIENT_ADDRESS = loadAddress()
while True: while True:
command = input("Type a command:") command = input("Type a command:")
separatedCommand = command.split(" ") separatedCommand = command.split(" ")
@ -102,6 +101,7 @@ while True:
network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, separatedCommand[1], SERVER_PUBLIC_KEY) network = NetWrapper(CLIENT_PRIVATE_KEY, CLIENT_ADDRESS, separatedCommand[1], SERVER_PUBLIC_KEY)
if not network.identifyServer(): if not network.identifyServer():
print('Server identification failed!') print('Server identification failed!')
continue
network.createEncryptedChannel() network.createEncryptedChannel()
network.authenticate(password=separatedCommand[2]) network.authenticate(password=separatedCommand[2])
@ -155,5 +155,5 @@ while True:
printCommand() printCommand()
except Exception: except Exception as e:
print('Error: '+Exception.args) print('Error: '+str(e))

View File

@ -4,7 +4,7 @@ import string
import json import json
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
import pyDH import pyDH
from Crypto.Cipher import ChaCha20 from Crypto.Cipher import ChaCha20, PKCS1_OAEP
from Crypto.PublicKey.RSA import RsaKey from Crypto.PublicKey.RSA import RsaKey
from Crypto.Random import get_random_bytes from Crypto.Random import get_random_bytes
@ -30,24 +30,27 @@ class NetWrapper:
def identifyServer(self) -> bool: def identifyServer(self) -> bool:
randommsg = self.randomStringGenerator() randommsg = self.randomStringGenerator()
cipher_rsa = PKCS1_OAEP.new(self.serverPubKey)
identMsg = json.dumps( identMsg = json.dumps(
{'type': 'IDY', 'source': self.network.own_addr, 'username': self.username, {'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') 'UTF-8')
self.network.send_msg(self.serverAddr, identMsg) self.network.send_msg(self.serverAddr, identMsg)
returnJson = {'source': '', 'type': ''} 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) status, msg = self.network.receive_msg(blocking=True)
if not status: if not status:
raise Exception('Network error during connection.') raise Exception('Network error during connection.')
returnJson = json.loads(msg.decode('UTF-8')) 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 return retmsg == randommsg
def createEncryptedChannel(self): def createEncryptedChannel(self):
dh = pyDH.DiffieHellman() 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') 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)
decodedmsg = {'source': '', 'type': ''} decodedmsg = {'source': '', 'type': ''}
@ -56,7 +59,8 @@ class NetWrapper:
if not status: if not status:
raise Exception('Network error during connection.') raise Exception('Network error during connection.')
decodedmsg = json.loads(msg.decode('UTF-8')) 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') self.cipherkey = dh.gen_shared_key(serverpubkey).encode('UTF-8')