Last active
June 13, 2018 13:36
-
-
Save Mohamedemad4/131f7519f878f1f088e7f2b53586de72 to your computer and use it in GitHub Desktop.
A Secure Messenger Application that uses python Sockets and AES encryption
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#python msg.py server port keyfile | |
#python msg.py client server:port keyfile | |
#the key must be 32 chars long | |
import sys | |
import socket | |
from Crypto.Cipher import AES | |
from multiprocessing import Process | |
obj = AES.new(open(sys.argv[3],'r').read().replace('\n',''), AES.MODE_CFB, 'This is an IV456') #replace the IV | |
def server(): | |
port=int(sys.argv[2]) | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.bind(('',port)) | |
server.listen(2) | |
print 'Massgeing server Running on 0.0.0.0:',port | |
while True: | |
conn,addr=server.accept() | |
client('localhost',port,conn) | |
def onmsg(conn): | |
while True: | |
data=conn.recv(2000) | |
if len(data)==0:print '\rSomeone closed the connection.';break | |
print '\r##> \033[94m',obj.decrypt(data),'\033[0m\n' | |
def client(host,port,conn): | |
print '\033[95m Connected, you are on secure\033[0m' | |
Process(target=onmsg,args=(conn,)).start() | |
while True: | |
input=raw_input('\033[92m > ') | |
conn.send(obj.encrypt(input)) | |
if __name__=='__main__': | |
if sys.argv[1]=='server': | |
server() | |
else: | |
conn=socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
conn.connect((sys.argv[2].split(':')[0],int(sys.argv[2].split(':')[1]))) | |
client(sys.argv[2].split(':')[0],sys.argv[2].split(':')[1],conn) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment