0

I'm trying to make a client which gets an AES key from a sever over socket.

both client and server have this code:

import base64
from Crypto.Cipher import AES
from Crypto import Random

BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[:-ord(s[len(s)-1:])]

class AESCipher:
 def __init__( self, key ):
    self.key = key

 def encrypt( self, raw ):
    raw = pad(raw)
    iv = Random.new().read( AES.block_size )
    cipher = AES.new( self.key, AES.MODE_CBC, iv )
    return base64.b64encode( iv + cipher.encrypt( raw ) ) 

 def decrypt( self, enc ):
    enc = base64.b64decode(enc)
    iv = enc[:16]
    cipher = AES.new(self.key, AES.MODE_CBC, iv )
    return unpad(cipher.decrypt( enc[16:] ))

How do I exchange AES key from the server to the client?

iYonatan
  • 916
  • 3
  • 10
  • 26
  • Are you asking how to accomplish key exchange? That can be rather complicated, and isn't really a programming question. You should probably take this question over to the crypto stack exchange, or better yet, do some research into basic cryptography. – zindorsky Feb 17 '16 at 16:13
  • @zindorsky Thanks for answering. I'll do more research if you say. Will I be able to contact you again when I have more questions? – iYonatan Feb 17 '16 at 16:16
  • Sure. But I'm not an expert on key exchange protocols. – zindorsky Feb 18 '16 at 15:17

1 Answers1

0

I am making something similar. I made a random key with RSA and then i have send the public key from client to server (Assuming that the client is the one that generate the AES key). So the server encrypts the key and send it to client. The client Decrypts the AES key with is private RSA key.

Anagnostou John
  • 498
  • 5
  • 14