-
Notifications
You must be signed in to change notification settings - Fork 0
/
Echo-Server.py
51 lines (42 loc) · 1.51 KB
/
Echo-Server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import socket
import sys
from Crypto.Cipher import AES
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Port to listen on (non-privileged ports are > 1023)
# Create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
server_address = (HOST, PORT)
print >>sys.stderr, 'starting up on %s port %s' % server_address
s.bind(server_address)
# Listen for incoming connections
s.listen(1)
# Encryption of message
def do_encrypt(message):
obj = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
ciphertext = obj.encrypt(message)
return ciphertext
# Decryption of message
def do_decrypt(ciphertext):
obj2 = AES.new('This is a key123', AES.MODE_CBC, 'This is an IV456')
message = obj2.decrypt(ciphertext)
return message
while True:
# Wait for a connection
print >>sys.stderr, 'waiting for a connection'
connection, client_address = s.accept()
try:
print >> sys.stderr, 'connection from', client_address
# Receive the data in small chunks and retransmit it
while True:
data = connection.recv(16)
print >> sys.stderr, 'received "%s"' % data
if data:
print >> sys.stderr, 'sending data back to the client'
connection.sendall(data)
else:
print >> sys.stderr, 'no more data from', client_address
break
finally:
# Clean up the connection
connection.close()