From ddf2a49780c6dae6a5349b30a15c0a7b68a2e661 Mon Sep 17 00:00:00 2001 From: Suwako Moriya Date: Sun, 18 Nov 2018 00:03:51 +0100 Subject: [PATCH] [client] Added RSA Key generation --- Pipfile | 9 +++--- client/log_config.json | 3 +- client/main.py | 67 ++++++++++++++++++++++++++++++++---------- server/log_config.json | 3 +- server/main.py | 2 +- 5 files changed, 59 insertions(+), 25 deletions(-) diff --git a/Pipfile b/Pipfile index 33efa86..5e3f44f 100644 --- a/Pipfile +++ b/Pipfile @@ -1,12 +1,13 @@ [[source]] name = "pypi" -verify_ssl = true url = "https://pypi.org/simple" +verify_ssl = true + +[dev-packages] [packages] +pycryptodome = "*" pycrypto = "*" [requires] -python_version = "3.5" - -[dev-packages] +python_version = "3.6" diff --git a/client/log_config.json b/client/log_config.json index 4686e03..a595920 100644 --- a/client/log_config.json +++ b/client/log_config.json @@ -36,7 +36,6 @@ "server": { "level": "DEBUG", "handlers": [ - "console", "info_file_handler", "error_file_handler" ] @@ -50,4 +49,4 @@ "error_file_handler" ] } -} \ No newline at end of file +} diff --git a/client/main.py b/client/main.py index 0142fd6..303d9a7 100755 --- a/client/main.py +++ b/client/main.py @@ -12,6 +12,9 @@ import socket # json decoder for int keys import threading +from Crypto.PublicKey import RSA +from Crypto.Cipher import PKCS1_OAEP + class Decoder(json.JSONDecoder): def decode(self, s, **kwargs): @@ -60,25 +63,57 @@ critical = log_server.critical #### Variables #### HOST = '127.0.0.1' PORT = 8888 -BUFFER_SIZE = 16384 +BUFFER_SIZE = 4096 +CHUNK_SIZE = int(BUFFER_SIZE/8) +NAME = 'default' -clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -clientSocket.connect((HOST, PORT)) # ET ICI ON MET LE CLIENT +class MainThread(threading.Thread): + """Main client class, for each identity.""" + + def __init__(self, name): + threading.Thread.__init__(self) + self.name = name + self.rsa = None + + def run(self): + rsa = self.rsaGenThread(self) + rsa.start() + rsa.join() + clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + clientSocket.connect((HOST, PORT)) + + class rsaGenThread(threading.Thread): + + def __init__(self, client, difficulty=30, new=False): + threading.Thread.__init__(self) + self.client = client + self.difficulty = difficulty + + def run(self): + if os.path.isfile("private.pem"): + try: + with open("private.pem", "rb") as keyfile: + rsa = RSA.importKey(keyfile.read()) + if not rsa.has_private(): + warning("Le fichier clef ne contient pas de clef privée") + raise ValueError + self.client.rsa = rsa + except (IndexError, ValueError): + warning("Fichier clef corrompu") + debug("Suppression du fichier clef corromu") + os.remove("private.pem") + if not os.path.isfile("private.pem"): # We're not using if/else because we may delete the file in the previous if statement + debug("Génération de la clef RSA pour %s" % self.client.name) + self.client.rsa = RSA.generate(BUFFER_SIZE + 256*self.difficulty) + with open("private.pem", "wb") as keyfile: + keyfile.write(self.client.rsa.exportKey()) + with open("public.pem", "wb") as keyfile: + keyfile.write(self.client.rsa.publickey().exportKey()) - - - - - - - - - - - - - +if __name__ == "__main__": + client = MainThread(NAME) + client.start() diff --git a/server/log_config.json b/server/log_config.json index 4686e03..a595920 100644 --- a/server/log_config.json +++ b/server/log_config.json @@ -36,7 +36,6 @@ "server": { "level": "DEBUG", "handlers": [ - "console", "info_file_handler", "error_file_handler" ] @@ -50,4 +49,4 @@ "error_file_handler" ] } -} \ No newline at end of file +} diff --git a/server/main.py b/server/main.py index dcf8ab4..e62d4e9 100755 --- a/server/main.py +++ b/server/main.py @@ -60,7 +60,7 @@ critical = log_server.critical #### Variables #### HOST = '' PORT = 8888 -BUFFER_SIZE = 16384 +BUFFER_SIZE = 4096 #### Socket #### main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)