[client] Added RSA Key generation
This commit is contained in:
parent
3e11fec257
commit
ddf2a49780
9
Pipfile
9
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"
|
||||
|
@ -36,7 +36,6 @@
|
||||
"server": {
|
||||
"level": "DEBUG",
|
||||
"handlers": [
|
||||
"console",
|
||||
"info_file_handler",
|
||||
"error_file_handler"
|
||||
]
|
||||
|
@ -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()
|
||||
|
@ -36,7 +36,6 @@
|
||||
"server": {
|
||||
"level": "DEBUG",
|
||||
"handlers": [
|
||||
"console",
|
||||
"info_file_handler",
|
||||
"error_file_handler"
|
||||
]
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user