[client] Added RSA Key generation

This commit is contained in:
Suwako Moriya 2018-11-18 00:03:51 +01:00
parent 3e11fec257
commit ddf2a49780
5 changed files with 59 additions and 25 deletions

View File

@ -1,12 +1,13 @@
[[source]] [[source]]
name = "pypi" name = "pypi"
verify_ssl = true
url = "https://pypi.org/simple" url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages] [packages]
pycryptodome = "*"
pycrypto = "*" pycrypto = "*"
[requires] [requires]
python_version = "3.5" python_version = "3.6"
[dev-packages]

View File

@ -36,7 +36,6 @@
"server": { "server": {
"level": "DEBUG", "level": "DEBUG",
"handlers": [ "handlers": [
"console",
"info_file_handler", "info_file_handler",
"error_file_handler" "error_file_handler"
] ]

View File

@ -12,6 +12,9 @@ import socket
# json decoder for int keys # json decoder for int keys
import threading import threading
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
class Decoder(json.JSONDecoder): class Decoder(json.JSONDecoder):
def decode(self, s, **kwargs): def decode(self, s, **kwargs):
@ -60,25 +63,57 @@ critical = log_server.critical
#### Variables #### #### Variables ####
HOST = '127.0.0.1' HOST = '127.0.0.1'
PORT = 8888 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 # 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()

View File

@ -36,7 +36,6 @@
"server": { "server": {
"level": "DEBUG", "level": "DEBUG",
"handlers": [ "handlers": [
"console",
"info_file_handler", "info_file_handler",
"error_file_handler" "error_file_handler"
] ]

View File

@ -60,7 +60,7 @@ critical = log_server.critical
#### Variables #### #### Variables ####
HOST = '' HOST = ''
PORT = 8888 PORT = 8888
BUFFER_SIZE = 16384 BUFFER_SIZE = 4096
#### Socket #### #### Socket ####
main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) main_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)