This commit is contained in:
parent
c61994a3cf
commit
10c481bc72
@ -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)
|
||||
except Exception as e:
|
||||
print('Error: '+str(e))
|
@ -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,9 +30,10 @@ 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': ''}
|
||||
@ -41,13 +42,15 @@ class NetWrapper:
|
||||
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')
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user