def send_auth_request_cl(sock: socket.socket, name: str, password: str, is_signing: bool, server_password: str, other_key: rsa.PublicKey, my_key: rsa.PrivateKey) -> None: """ standard is {'id': 'auth_cl', 'name': name, 'pass': password, 'sign': is_signing, 'srpass': server_password, 'email': mail_addr} :param sock: socket that hosts SimpleChat server :param name: authentication name :param password: password to authenticate :param is_signing: does user sign in SimpleChat or log in :param server_password: server password for more security :param other_key: other's public key :param my_key: your side's private key """ msg = dp.write_json({ 'id': 'auth_cl', 'name': name, 'pass': password, 'sign': is_signing, 'srpass': server_password }) msg = msg.encode() __send(sock, msg, other_key, my_key)
def send_keys_handshake(sock: socket.socket, sending_key: rsa.PublicKey) -> None: """ :param sock: socket to send to :param sending_key: this key will be send to client (server) :param ftp_session_key: encrypted key to encrypt files """ key = sending_key.save_pkcs1() msg = dp.write_json({'id': 'handshake', 'key': key.hex()}) send(sock, msg.encode())
def __send(sock: socket, msg: bytes, other_key: rsa.PublicKey, my_key: rsa.PrivateKey) -> None: """ Just sends msg bytes to a socket standard using crypt_sys.cipher() :param sock: socket to send to :param other_key: other's public key :param my_key: your side's private key :param my_key: private client's key """ res = cipher(msg, other_key, my_key) res['id'] = 'crypt' res = dp.write_json(res) res = res.encode() send(sock, res)
def send_chat_message_cl(sock: socket.socket, sender_name: str, text: str, other_key: rsa.PublicKey, my_key: rsa.PrivateKey) -> None: """ standard is {'id': 'msg', 'sender': name, 'text': name} :param sock: socket that hosts SimpleChat server :param sender_name: client's name :param text: message text :param other_key: other's public key :param my_key: your side's private key """ msg = dp.write_json({'id': 'msg', 'sender': sender_name, 'text': text}) msg = msg.encode() __send(sock, msg, other_key, my_key)
def send_auth_response_sr(sock: socket.socket, auth_return: bool, other_key: rsa.PublicKey, my_key: rsa.PrivateKey, returning_name: str = '') -> None: """ standard is {'id': 'auth_sr', 'auth_return': auth_return, 'name': returning_name} :param sock: socket that hosts SimpleChat server :param auth_return: just an answer is authentication successful :param other_key: other's public key :param my_key: your side's private key :param returning_name: name to save if authentication successful """ msg = dp.write_json({ 'id': 'auth_sr', 'auth_return': auth_return, 'name': returning_name }) msg = msg.encode() __send(sock, msg, other_key, my_key)
from Crypto.Cipher import AES from secrets import token_bytes import rsa from MessageHandlers import data_processing as dp message = 'super secret Alice\'s message' name = 'Alice' # Alice gen keys for signature a_pk, a_sk = rsa.newkeys(500) # Bob gen keys public_key, private_key = rsa.newkeys(500) # Alice digital signature msg = dp.write_json({'text': message, 'name': name}) # salt = bcrypt.gensalt() print('encoded msg:', msg.encode()) # msg_hash = bcrypt.hashpw(msg.encode(), salt) # dig_sign = rsa.encrypt(msg_hash, a_sk) dig_sign = rsa.sign(msg.encode(), a_sk, 'SHA-256') # Alice message crypt symmetric_key = token_bytes(16) print('symmetric_key:', symmetric_key) cipher = AES.new(symmetric_key, AES.MODE_EAX) encrypted = cipher.encrypt(msg.encode()) # Alice cipher symmetric key sym_key_encrypted = rsa.encrypt(symmetric_key, public_key)