Esempio n. 1
0
import socket
import DiffieHellman
import nacl.secret
import nacl.utils
from binascii import hexlify

# Initialize Diffie Hellman object so private and public keys are generated.
client = DiffieHellman.D_H()

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('localhost', 5555)
sock.connect(server_address)
print("Connected to %s on port %s" % server_address)

# Send Client's public key to the server so the Diffie hellman key exchange can happen.
# And cast the key over to string so we can send it over the socket.
sock.sendall(str(client.pubKey))

# Receive servers public key so we can generate the final (secret) key.
data = sock.recv(9000)

# Generate the secret key and cast the incoming key to int from str.
client.genKey(int(data))
print("Secret key:", hexlify(client.getKey()))

# Initialize the SALT object and pass in the secret key.
box = nacl.secret.SecretBox(client.getKey())
Esempio n. 2
0
import socket
from binascii import hexlify
import DiffieHellman
import nacl.secret
import nacl.utils

while True:
    # Initialize Diffie Hellman object so private and public keys are generated.
    server = DiffieHellman.D_H()

    # This string we use to cat with the incoming msg and send to cat'd string back.
    echoString = "I have received | "

    # Create a TCP/IP socket.
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Bind the socket to a port
    server_address = ('localhost', 5555)
    sock.bind(server_address)

    # Listen for incoming connections.
    sock.listen(1)

    # Accepting the incoming traffic on server socket.
    connection, client_address = sock.accept()
    print('Connection from IP:', client_address)

    # Receive clients public key so we can generate the final (secret) key.
    client_Pubkey = connection.recv(9000)

    # Send our (server) public key.