diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 index b6e4761..1ae7480 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__/ *.py[cod] *$py.class +config.yaml # C extensions *.so diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/src/client/cli.py b/src/client/cli.py new file mode 100644 index 0000000..074ebe1 --- /dev/null +++ b/src/client/cli.py @@ -0,0 +1,42 @@ +import websocket +import _thread +import time +import ssl +import yaml + +def on_message(ws, message): + print(f"<< {message}") + +def on_error(ws, error): + print(error) + +def on_close(ws, close_status_code, close_msg): + print(f"### closed. reason: {close_msg} ###") + +def on_open(ws): + def run(*args): + running = True + while running: + outbound = input(">") + if outbound == 'q': + running = False + else: + ws.send(outbound) + ws.close() + _thread.start_new_thread(run, ()) + +def open_socket(url, port): + websocket.enableTrace(True) + ws = websocket.WebSocketApp(f"ws://{url}:{port}", + on_open=on_open, + on_message=on_message, + on_error=on_error, + on_close=on_close) + ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) + + +if __name__ == "__main__": + with open('config.yaml', 'r') as f: + conf = yaml.safe_load(f) + open_socket(conf['ws']['url'], conf['ws']['port']) + diff --git a/src/client/test-cli.py b/src/client/test-cli.py new file mode 100644 index 0000000..02daae0 --- /dev/null +++ b/src/client/test-cli.py @@ -0,0 +1,43 @@ +import websocket +import _thread +import ssl +import json + +def on_message(ws, message): + print(f"<< {message}") + +def on_error(ws, error): + print(error) + +def on_close(ws, close_status_code, close_msg): + print(f"### closed. reason: {close_msg} ###") + +def on_open(ws): + def run(*args): + running = True + uname = input('input username:') + msg = { + "user":uname, + "content":None + } + ws.send(json.dumps(msg)) + while running: + outbound = input(">") + if outbound == 'q': + running = False + else: + msg = { + "user":uname, + "content":outbound + } + ws.send(json.dumps(msg)) + ws.close() + _thread.start_new_thread(run, ()) + +if __name__ == "__main__": + ws = websocket.WebSocketApp("wss://hornetfighter.com:9443", + on_open=on_open, + on_message=on_message, + on_error=on_error, + on_close=on_close) + ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE}) diff --git a/src/client/test_config.yaml b/src/client/test_config.yaml new file mode 100644 index 0000000..23ac0a0 --- /dev/null +++ b/src/client/test_config.yaml @@ -0,0 +1,3 @@ +ws: + url: localhost + port: 6873 diff --git a/src/py-cli/encrypt_decrypt.py b/src/py-cli/encrypt_decrypt.py new file mode 100755 index 0000000..b24c438 --- /dev/null +++ b/src/py-cli/encrypt_decrypt.py @@ -0,0 +1,38 @@ +""" +Encrypting and decrypting a message + +https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/ + +@author Todorov-Lachezar +""" + +import rsa + +class encrypt_decrypt(): + + # the message we want to encrypt + message = "Hello World" + + def make_keys(key_length): + publicKey, privateKey = "" + # Generates public and private keys + # Method takes in key length as a parameter + # Note: the key length should be >16 + if(key_length < 16): + publicKey, privateKey = rsa.newkeys(key_length) + return publicKey, privateKey + else: + return "Enter a key_length of >16" + + + def encryption(plaintext, publicKey): + # Encrypts the message with the public key + # Note: make sure to encode the message before encrypting + encMessage = rsa.encrypt(plaintext.encode(), publicKey) + return encMessage + + def decryption(ciphertext, privateKey): + # Decrypts the encrypted message with the private key + # Note: make sure to decode the message after decryption + decMessage = rsa.decrypt(ciphertext, privateKey).decode() + return decMessage diff --git a/src/py-cli/main.py b/src/py-cli/main.py old mode 100644 new mode 100755 index f556c5f..7f2ec33 --- a/src/py-cli/main.py +++ b/src/py-cli/main.py @@ -1,5 +1,25 @@ """ A client to send an encrypted message to another client -@author hornetfighter515 +@author hornetfighter515, Lachezar Todorov """ +import encrypt_decrypt +import websock.alice +import websock.bob +import asyncio + +def main(): + message = "Hello World" + + key_length = 512 + + #Bob's keys + publicKey, privateKey = encrypt_decrypt.encrypt_decrypt.make_keys(key_length) + + asyncio.run(websock.alice.Alice.messageEncrypt("ws://localhost:8765", publicKey, message)) + + asyncio.run(websock.bob.Bob.messageDecrypt(privateKey)) + + + +