2018-11-12 21:46:27 +01:00
|
|
|
#!/usr/bin/env python3
|
2018-11-12 20:42:44 +01:00
|
|
|
# coding: utf8
|
|
|
|
|
|
|
|
import json
|
|
|
|
import logging
|
2018-11-13 21:37:49 +01:00
|
|
|
import logging.config
|
2018-11-12 20:42:44 +01:00
|
|
|
import os
|
|
|
|
import socket
|
|
|
|
|
2018-11-18 12:52:55 +01:00
|
|
|
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
|
2018-11-12 20:42:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 ####
|
2018-11-13 21:37:49 +01:00
|
|
|
HOST = '127.0.0.1'
|
2018-11-12 20:42:44 +01:00
|
|
|
PORT = 8888
|
2018-11-15 20:29:32 +01:00
|
|
|
BUFFER_SIZE = 4096
|
2018-11-18 12:52:55 +01:00
|
|
|
CHUNK_SIZE = int(BUFFER_SIZE / 8)
|
2018-11-12 20:42:44 +01:00
|
|
|
|
|
|
|
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
clientSocket.connect((HOST, PORT))
|
|
|
|
|
2018-11-18 12:52:55 +01:00
|
|
|
|
2018-11-12 20:42:44 +01:00
|
|
|
# ET ICI ON MET LE CLIENT
|
|
|
|
|
2018-11-18 12:52:55 +01:00
|
|
|
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)
|
2018-11-12 20:42:44 +01:00
|
|
|
|
|
|
|
|
2018-11-15 20:29:32 +01:00
|
|
|
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
|
2018-11-18 12:52:55 +01:00
|
|
|
RSA_KEY_SIZE = CHUNK_SIZE * 2
|
|
|
|
key = RSA.generate(BUFFER_SIZE * 2)
|
2018-11-15 20:29:32 +01:00
|
|
|
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)
|
2018-11-16 21:54:30 +01:00
|
|
|
|
2018-11-15 20:29:32 +01:00
|
|
|
print('ok')
|
2018-11-18 12:52:55 +01:00
|
|
|
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]
|
2018-11-15 20:29:32 +01:00
|
|
|
|
2018-11-16 21:54:30 +01:00
|
|
|
# HEADER+AES key are encypted in same time, so decrypt time at same time
|
2018-11-15 20:29:32 +01:00
|
|
|
cipher_rsa = PKCS1_OAEP.new(key)
|
2018-11-16 21:54:30 +01:00
|
|
|
texte_to_decrypt = b''
|
|
|
|
|
2018-11-18 12:52:55 +01:00
|
|
|
for ligne in [chunk[i:i + BUFFER_SIZE] for i in range(0, len(chunk), BUFFER_SIZE)]:
|
2018-11-16 21:54:30 +01:00
|
|
|
to_decrypt = ligne[2:]
|
|
|
|
to_decrypt = to_decrypt[:int.from_bytes(ligne[:2], byteorder='big')]
|
|
|
|
texte_to_decrypt += to_decrypt
|
2018-11-18 12:52:55 +01:00
|
|
|
print(len(texte_to_decrypt))
|
2018-11-16 19:59:22 +01:00
|
|
|
texte = b''
|
2018-11-18 12:52:55 +01:00
|
|
|
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))
|
2018-11-16 19:59:22 +01:00
|
|
|
texte += cipher_rsa.decrypt(to_decrypt)
|
2018-11-16 21:54:30 +01:00
|
|
|
print(texte)
|