#!/usr/bin/python # Server program from socket import * # Set the socket parameters host = "localhost" port = 21567 buf = 2048 addr = (host,port) # Create socket and bind to address UDPSock = socket(AF_INET,SOCK_DGRAM) UDPSock.bind(addr) #decrypt def decrypt(a): length = len(a) for position in range (length): temp = ord(a[position]) temp = temp - 1 temp = chr(temp) data = a[:position] + temp + a[position + 1:] return data print "Waiting message..." # Receive messages while 1: data,addr = UDPSock.recvfrom(buf) data1,addr = UDPSock.recvfrom(buf) if not data: print "Client has died!" break else: data, key = data.split(';') print "\nReceived message '", data1,"'" clave = raw_input('Give me the key to decrypt: ') if (clave == key): decrypt(data1) print "Mensaje: ",data else: break # Close socket UDPSock.close()
#!/usr/bin/python # Client program from socket import * # Set the socket parameters host = "localhost" port = 21567 buf = 2048 addr = (host,port) # Create socket UDPSock = socket(AF_INET,SOCK_DGRAM) def_msg = "Write something "; print "\n",def_msg #encrypt def encrypt(data): length = len(data) for position in range (length): temp = ord(data[position]) temp = temp + 1 temp = chr(temp) data = data[:position] + temp + data[position + 1:] return data # Send messages while (1): data = raw_input('Introduce Message: ') clave = raw_input('Give me the key: ') data = data+";"+clave if not data: break else: if(UDPSock.sendto(data,addr)): print "Sending message.....\n" data1 = encrypt(data); UDPSock.sendto(data1,addr) # Close socket UDPSock.close()
Now i'm try to explain the code:
The next part take the parameters and data sending, then separate the string in data (Information) and key (key to decrypt the text), then we compare between key to clave and if this is correct show the message and in the last part close the socket.
Execution:
host = "localhost" port = 21567 buf = 2048 addr = (host,port)In this part we set the parameters, we can change, for example port to another, the size of the buffer, and the host, but if we change in server or client, we need to change too in the other code. The parameter is used to do a connection between client and server.
# Create socket and bind to address UDPSock = socket(AF_INET,SOCK_DGRAM) UDPSock.bind(addr)In this part we create the socket connection and bind the address, AF_INET & SOCK_DGRAM, represents the address family INET for the internet sockets.
#decrypt def decrypt(a): length = len(a) for position in range (length): temp = ord(a[position]) temp = temp - 1 temp = chr(temp) data = a[:position] + temp + a[position + 1:] return dataThe next part, is a function of decrypt, we saw this in other publication, is the same but we use to decrypt the message.
# Receive messages while 1: data,addr = UDPSock.recvfrom(buf) data1,addr = UDPSock.recvfrom(buf) if not data: print "Client has died!" break else: data, key = data.split(';') print "\nReceived message '", data1,"'" clave = raw_input('Give me the key to decrypt: ') if (clave == key): decrypt(data1) print "Mensaje: ",data else: break # Close socket UDPSock.close()
The next part take the parameters and data sending, then separate the string in data (Information) and key (key to decrypt the text), then we compare between key to clave and if this is correct show the message and in the last part close the socket.
TUDPSock.sendto(data1, addr)
Execution:
Este es el código de lo presentado, supongo. Pongo +1 por haberlo incluido como entrada.
ResponderEliminar