6

I made a simple server program that is able to receive data from 4 different clients at a time. Now I want to send some data with AES-128 Encryption but that should be decoded at the server side. Here is my code for server:

from socket import *
from threading import Thread

def clientHandler():
    conn, addr = s.accept()
    print addr, "is connected"
    while 1:
        data = conn.recv(1024)
        if not data:
            break
        print "Received Message", repr(data)


HOST = "" #localhost
PORT = 15000


s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(4)

print "Server is runnig"
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()
#Thread(target=clientHandler).start()

for i in range(4):
    Thread(target=clientHandler).start()

s.close()

And I am sending data like this from my client side

from socket import *
s = socket()
s.connect(("localhost",15000))
s.send()

How should I modify my client code and server code to include AES-128 Encryption inside of it .. Kindly help me in this regard.

Asad Irfan
  • 133
  • 1
  • 3
  • 11

1 Answers1

14

Use Python's Crypto module which supports AES. You need a symmetric key (same key used to encrypt and decrypt). The same key can be generated in both server and client if the same passphrase and the initialization vector(IV) are used.

Summary: 1. Same key to be used to encrypt and decrypt 2. Use Crypto.Cipher.AES

AES has methods to generate key, encrypt and decrypt data. Following links have the actual code. pycrypto stackoverflow

Client - Call this method to encrypt your data and send the encrypted data

from Crypto.Cipher import AES

def do_encrypt(message):
    obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
    ciphertext = obj.encrypt(message)
    return ciphertext

Server - Receive data and call this method to decrypt the data

from Crypto.Cipher import AES

def do_decrypt(ciphertext):
    obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
    message = obj2.decrypt(ciphertext)
    return message

This is a sample code, make sure you choose a strong passphrase and IV.

Community
  • 1
  • 1
helloV
  • 50,176
  • 7
  • 137
  • 145
  • 1
    I am really new to this encryption business in Python. I have tried this code for encryption but it gives some key size error `def encryption(privateInfo):`
    `BLOCK_SIZE=16`
    `PADDING='{'`
    `pad = lambda s:s+(BLOCK_SIZE-len(s)%BLOCK_SIZE)*PADDING` `EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))`
    `secret="secret_key"`
    `print "encryption key:",secret`
    `cipher = AES.new(secret)`
    `encoded = EncodeAES(cipher,privateInfo)`
    `print"Encrypted string:",encoded`
    – Asad Irfan Dec 04 '14 at 06:56
  • Thank you very much. Now I successfully transfered the data but if size is not multiple of 16, it gives me an error.. Can you please help me with that issue as well ? – Asad Irfan Dec 04 '14 at 07:28
  • See the first answer in this [link](http://stackoverflow.com/questions/12524994/encrypt-decrypt-using-pycrypto-aes-256) which explains how to solve padding errors. Also I forgot to mention, you can use base64encode/base64decode to convert it the encrypted text to ascii. The first answer has everything you need. – helloV Dec 04 '14 at 07:43
  • Okay thank you very much. This problem resolved as well.. You mentioned that select strong passphrase and IV ... how to do that ? Is there any math involved ? What if I want to create a key on client and share it secretly with server using deffie hellman key exchange etc ? Are that implementable in Python ? – Asad Irfan Dec 04 '14 at 16:04