def cryproHandShake(sock):
    private_key = rsa.generate_private_key(  # new keys pair
        public_exponent=65537,
        key_size=2048,
        backend=default_backend())

    public_key = private_key.public_key()

    pem = public_key.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo)

    if settings.LogEncryptedData:
        Log.warning('client-public-key:')
        Log.warning(bytes2str(pem))

    sock.sendall(pem)

    while True:
        response = sock.recv(32768)
        response = filterForNullTerminators(response)
        if (len(response) > 0):
            break

    if settings.LogEncryptedData:
        Log.warning('server public key:')
        Log.warning(bytes2str(response))

    server_public_key = serialization.load_pem_public_key(
        response, backend=default_backend())

    return (server_public_key, private_key)
 def decrypt_data(self, data):
     original_message = server_private_key.decrypt(
         data,
         padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                      algorithm=hashes.SHA256(),
                      label=None))
     return bytes2str(original_message)
コード例 #3
0
def run_client():
    host = settings.SERVER_HOST
    port = settings.SERVER_PORT
    sock = create_connection((host, port))

    server_public_key, private_key = cryproHandShake(sock)

    # Send data to server
    try:
        while True:
            # Input data to send
            orig_data = input("Enter data you want to send: ")
            encrypted = server_public_key.encrypt(
                str2bytes(orig_data),
                padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                             algorithm=hashes.SHA256(),
                             label=None))

            if (settings.LogEncryptedData):
                Log.warning('encrypted sending data :')
                Log.warning(encrypted)
                print('\n\n')

            sock.sendall(encrypted)

            while True:
                response = sock.recv(32768)
                response = filterForNullTerminators(response)
                if (len(response) > 0):
                    break

            if settings.LogEncryptedData:
                Log.warning('encrypted response: ')
                Log.warning(response)
                print('\n\n')

            decrypted_message = private_key.decrypt(
                response,
                padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()),
                             algorithm=hashes.SHA256(),
                             label=None))
            Log.debug('Response: %s' % bytes2str(decrypted_message))

    except (KeyboardInterrupt, EOFError):
        sock.close()
        print()
        sys.exit(0)
コード例 #4
0
 def run(self):
     while self.signal:
         try:
             if self.socket.sendall(b'11111111') == 0:
                 raise ConnectionResetError
             while True:
                 try:
                     chunk = self.socket.recv(2048)
                 except OSError:
                     break
                 frame = bytes2str(chunk)
                 self.frames += str(frame)
             if self.frames == "":
                 continue
             err_msg = self.check_data()
             self.display(err_msg)
             self.return_data(err_msg)
         except (ConnectionResetError, BrokenPipeError):
             log("Client " + str(self.address) + " has disconnected")
             self.signal = False
             self.socket.close()
             connections.remove(self)
             break
コード例 #5
0
 def set_len_rec_frames(self, frame):
     num_frames_bits = [bytes2str(frame)]
     num_frames = server_dll(num_frames_bits)
     self.num_of_frames = int(num_frames)