#!/usr/bin/env python3 # coding: utf8 import json import logging import logging.config import os import socket try: # noinspection PyUnresolvedReferences from Crypto.PublicKey import RSA as RSA # noinspection PyUnresolvedReferences from Crypto.Cipher import PKCS1_OAEP pycryptodome = False except ModuleNotFoundError: # Pycryptodomex from Cryptodome.PublicKey import RSA as RSA from Cryptodome.Cipher import PKCS1_OAEP pycryptodome = True def setup_logging(default_path='log_config.json', default_level=logging.INFO, env_key='LOG_CFG'): """Setup logging configuration """ path = default_path value = os.getenv(env_key, None) if value: path = value if os.path.exists(path): with open(path, 'rt') as f: config = json.load(f) logging.config.dictConfig(config) else: logging.basicConfig(level=default_level) setup_logging() log_server = logging.getLogger('server') debug = log_server.debug info = log_server.info warning = log_server.warning error = log_server.error critical = log_server.critical #### Variables #### HOST = '127.0.0.1' PORT = 8888 BUFFER_SIZE = 4096 CHUNK_SIZE = int(BUFFER_SIZE / 8) clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clientSocket.connect((HOST, PORT)) # ET ICI ON MET LE CLIENT def send(to_send): clientSocket.send(BEGIN_MESSAGE) i = 0 for to_send_text in [to_send[i:i + BUFFER_SIZE - 2] for i in range(0, len(to_send), BUFFER_SIZE - 2)]: print((len(to_send_text)).to_bytes(2, byteorder='big')) print((len(to_send_text)).to_bytes(2, byteorder='big') + to_send_text.ljust(BUFFER_SIZE, b";")) clientSocket.send((len(to_send_text)).to_bytes(2, byteorder='big') + to_send_text.ljust(BUFFER_SIZE - 2, b";")) i += 1 print(i) clientSocket.send(END_MESSAGE) BEGIN_MESSAGE = bytes("debut", "ascii").ljust(BUFFER_SIZE, b";") END_MESSAGE = bytes("fin", "ascii").ljust(BUFFER_SIZE, b";") HEADER_TXT = """\ EICP2P2 V1 type: RSASend""" HEADER = bytes(HEADER_TXT, "ascii").ljust(BUFFER_SIZE, b";") ## création des clef RSA RSA_KEY_SIZE = CHUNK_SIZE * 2 key = RSA.generate(BUFFER_SIZE * 2) private_key = key.export_key() file_out = open("private.pem", "wb") file_out.write(private_key) public_key = key.publickey().export_key() file_out = open("receiver.pem", "wb") file_out.write(public_key) to_send = public_key.ljust(BUFFER_SIZE, b';') clientSocket.send(BEGIN_MESSAGE) clientSocket.send(HEADER) clientSocket.send(to_send) clientSocket.send(END_MESSAGE) print('ok') chunk = bytes("", "ascii") while chunk != BEGIN_MESSAGE: chunk = clientSocket.recv(BUFFER_SIZE) last_chunk = chunk while last_chunk != END_MESSAGE: last_chunk = clientSocket.recv(BUFFER_SIZE) chunk += last_chunk print(len(chunk[BUFFER_SIZE:-BUFFER_SIZE])) chunk = chunk[BUFFER_SIZE:-BUFFER_SIZE] # HEADER+AES key are encypted in same time, so decrypt time at same time cipher_rsa = PKCS1_OAEP.new(key) texte_to_decrypt = b'' for ligne in [chunk[i:i + BUFFER_SIZE] for i in range(0, len(chunk), BUFFER_SIZE)]: to_decrypt = ligne[2:] to_decrypt = to_decrypt[:int.from_bytes(ligne[:2], byteorder='big')] texte_to_decrypt += to_decrypt print(len(texte_to_decrypt)) texte = b'' for to_decrypt in [texte_to_decrypt[i:i + RSA_KEY_SIZE] for i in range(0, len(texte_to_decrypt), RSA_KEY_SIZE)]: print(len(to_decrypt)) print(to_decrypt) print(texte) print(len(texte)) texte += cipher_rsa.decrypt(to_decrypt) print(texte)