├── requirements.txt ├── README.md ├── client.py └── server.py /requirements.txt: -------------------------------------------------------------------------------- 1 | simple-crypt 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Encrypted-Python-Chat 2 | --- 3 | ##### This script is built on python version 3 4 | ##### It is chat application based on encryption with password 5 | ##### It works only in terminal or command prompt 6 | ##### Independent of operating system 7 | --- 8 | ### Requirements 9 | Only single python module `simple-crypt` required 10 | 11 | Installation: 12 | 13 | `pip3 install simple-crypt` 14 | 15 | --- 16 | ### Usage 17 | 18 | For server: run `python server.py` and continue 19 | 20 | For client: run `python client.py` and continue 21 | 22 | --- 23 | -------------------------------------------------------------------------------- /client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | # client script to connect to the server 4 | # Note : please ask server for IP and port address 5 | 6 | import socket, getpass 7 | from time import sleep 8 | from os import system 9 | from simplecrypt import encrypt,decrypt 10 | 11 | 12 | # chat method to start cennecting to server and 13 | # start chatting 14 | def chat(host,port): 15 | server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # creating socket 16 | system("clear") 17 | print("Connecting to server...") 18 | sleep(5) 19 | server.connect((host, port)) 20 | print("Connected to server {}:{}".format(host,port)) 21 | name = input("Your name : ") # name to identify your self 22 | passwd = getpass.getpass("Enter password set by client for encrypted chat : ") # password to encrypt chat msgs 23 | while True: 24 | msg = input("Me : ") 25 | 26 | encMsg = encrypt(passwd,(name+" : "+msg)) # encrypting msg 27 | if msg.lower() == "bye": 28 | server.send(encMsg) # sending encrypted msg 29 | server.close() 30 | exit(0) 31 | else: 32 | server.send(encMsg) # sending encrypted msg 33 | print(decrypt(passwd,server.recv(1024)).decode('utf-8')) # receive msg and decrypt 34 | 35 | if __name__ == '__main__': 36 | print("Ask the server maintainer for server IP and PORT") 37 | host = input("Enter the server IP address : ") 38 | port = int(input("Enter the server PORT : ")) 39 | try: 40 | chat(host,port) 41 | except KeyboardInterrupt: 42 | print("\nKeyboard Interrupted ! \nBye bye...") 43 | exit() -------------------------------------------------------------------------------- /server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | # server script to initialize the server with 4 | # internal or external network 5 | # Note : please share the IP Address and Port with the 6 | # client(s) to start chatting. 7 | 8 | import socket, getpass 9 | import sys, os 10 | from os import system, path 11 | from time import sleep 12 | from platform import system as systemos, architecture 13 | from simplecrypt import encrypt,decrypt 14 | 15 | 16 | # using serveo.net to forward port for external network communication 17 | def runServeo(): 18 | print("Connecting to External network....") 19 | system('ssh -R 9568:0.0.0.0:9568 serveo.net > /dev/null &') 20 | sleep(5) 21 | ip = socket.gethostbyname('serveo.net') 22 | print("IP: {} \t PORT: 9568".format(ip)) 23 | print("Share the above IP and PORT number with client.") 24 | 25 | # checking if user needs to connect through Internal Network or External Network 26 | def InternalExternal(): 27 | mode = input("Chat on Internal or External Network ? (I/E): ") 28 | if mode.lower() == 'e': 29 | return True 30 | elif mode.lower() == 'i': 31 | return False 32 | else: 33 | print("Choose correct option !") 34 | InternalExternal() 35 | 36 | # actual code to chat over the network 37 | # the message sent and received are encoded as 'UTF-8' 38 | # change the 42 line for utilizing number of connections to accept 39 | def chat(host,port): 40 | server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) # creating of socket 41 | server.bind((host, port)) # binding the host and port 42 | server.listen(5) 43 | print("Waiting for connection from the client...") 44 | client, address = server.accept() #accepting connection form client 45 | system('clear') 46 | print('Got connection from : ', address) 47 | name = input("Your name : ") # name to identify yourself 48 | # set password to encrypt and decrypt the chat 49 | passwd = getpass.getpass("Enter password for encrypted chat : ") # password to encrypt chat msgs 50 | while True: 51 | print(decrypt(passwd,client.recv(1024)).decode('utf-8')) # decrypt the received encrypted msg 52 | msg = input("Me : ") 53 | encMsg = encrypt(passwd,(name+" : "+msg)) # encrypt the msg 54 | if msg.lower() == "bye": 55 | client.send(encMsg) # send encrypted msg 56 | client.close() 57 | server.close() 58 | system("pkill -f 'ssh -R 9568:0.0.0.0:9568 serveo.net'") 59 | exit(0) 60 | else: 61 | client.send(encMsg) # send encrypted msg 62 | 63 | if __name__ == '__main__': 64 | host = '' 65 | if InternalExternal(): 66 | runServeo() 67 | host = "0.0.0.0" 68 | port = 9568 69 | else: 70 | print("Your local IP/host IP is set to :") 71 | str(list(str(os.system("hostname -I"))).remove('0')) 72 | host = input("Enter the host : ") 73 | port = int(input("Enter the port : ")) 74 | try: 75 | chat(host,port) 76 | except KeyboardInterrupt: 77 | print("\nKeyboard Interrupted ! \nBye bye..") 78 | system("pkill -f 'ssh -R 9568:0.0.0.0:9568 serveo.net'") 79 | exit() 80 | --------------------------------------------------------------------------------