JSchmoe

Diffie-Hellman in python

Mar 6th, 2016
94
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import socket, Crypto, hashlib, string, base64
  2. from Crypto import Random
  3. from Crypto.Cipher import AES
  4. from Crypto.Random import random
  5.  
  6. #Diffie-Hellman in python
  7.  
  8. modulus = 8669
  9. base = 3300 #These are small because otherwise I had problems with identical keys
  10.  
  11. def server(ip, port):
  12.     s = socket.socket()
  13.     s.bind((ip,port)) #Listen for client
  14.     s.listen(1)
  15.     print("Listening...")
  16.     client, addr = s.accept() #Accept a client
  17.  
  18.     print("Creating key pair...")
  19.     privateKey = sum([ord(x) for x in Random.new().read(100)]) #Get 100 random bytes and sum them to get private key
  20.     publicKey = pow(base, privateKey, modulus) #Calculate (base^private)%modulus to get public key
  21.     print("Private key: "+str(privateKey))
  22.     print("Public key:  "+str(publicKey))
  23.  
  24.     clientKey = client.recv(2048) #Get public key from client
  25.     client.sendall(str(publicKey)) #Send own public key
  26.  
  27.     print("Calculating shared secret...")
  28.     sharedSecret = pow(int(clientKey), privateKey, modulus) #calculate (public^private)%modulus to get shared secret
  29.     print("Common secret is: "+str(sharedSecret))
  30.  
  31.     print("Calculating shared encryption key...")
  32.     key = hashlib.sha256(str(sharedSecret)).hexdigest()[:32] #Hash the shared secret and use the first 32 bytes as key
  33.     eoe = ''.join([chr((ord(x)+8)%256) for x in key[::2]]) #Take every other element and add 8 to it
  34.     key = ''.join([x for t in zip(key, eoe) for x in t]) #Interleave key and eoe
  35.  
  36.     print("Calculating initialization vector...")
  37.     IV = ''.join([random.choice(string.ascii_letters + string.digits) for n in range(16)]) #Generate the IV
  38.  
  39.     print("Sending IV to client...")
  40.     client.sendall(IV) #Send IV to client
  41.     print("Key: "+str(key))
  42.     print("IV:  "+str(IV))
  43.     aesobj = AES.new(key, AES.MODE_CFB, IV) #Create AES object using key and IV
  44.    
  45.     return aesobj
  46.    
  47. def client(ip, port):
  48.    
  49.     s = socket.socket()
  50.     s.connect((ip, port)) #Connect to server
  51.    
  52.     privateKey = sum([ord(x) for x in Random.new().read(100)]) #100 random bytes
  53.     publicKey = pow(base, privateKey, modulus)
  54.     s.sendall(str(publicKey)) #Create key pair and send public key
  55.    
  56.     serverKey = s.recv(2048)
  57.     sharedSecret = pow(int(serverKey), privateKey, modulus) #Calculate shared secret
  58.    
  59.     key = hashlib.sha256(str(sharedSecret)).hexdigest()[:32] #Hash the shared secret and use the first 32 bytes as key
  60.     eoe = ''.join([chr((ord(x)+8)%256) for x in key[::2]]) #Take every other element and add 8 to it
  61.     key = ''.join([x for t in zip(key, eoe) for x in t]) #Interleave key and eoe
  62.    
  63.     IV = s.recv(256) #Get IV and create AES object
  64.     aesobj = AES.new(key, AES.MODE_CFB, IV)
  65.    
  66.     return aesobj
RAW Paste Data