diff --git a/src/py-cli/encrypt_decrypt.py b/src/py-cli/encrypt_decrypt.py old mode 100755 new mode 100644 index 9565085..b24c438 --- a/src/py-cli/encrypt_decrypt.py +++ b/src/py-cli/encrypt_decrypt.py @@ -7,25 +7,32 @@ https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/ """ import rsa - -# Generates public and private keys -# Method takes in key length as a parameter -# Note: the key length should be >16 -publicKey, privateKey = rsa.newkeys(512) - -# the message we want to encrypt -message = "Hello World" - -# Encrypts the message with the public key -# Note: make sure to encode the message before encrypting -encMessage = rsa.encrypt(message.encode(), - publicKey) - -print("original string: ", message) -print("encrypted string: ", encMessage) - -# Decrypts the encrypted message with the private key -# Note: make sure to decode the message after decryption -decMessage = rsa.decrypt(encMessage, privateKey).decode() - -print("decrypted string: ", decMessage) \ No newline at end of file + +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 index f556c5f..f54e232 100644 --- a/src/py-cli/main.py +++ b/src/py-cli/main.py @@ -1,5 +1,24 @@ """ 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 + + 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)) + + + + diff --git a/src/py-cli/websock/alice.py b/src/py-cli/websock/alice.py new file mode 100644 index 0000000..49c6a42 --- /dev/null +++ b/src/py-cli/websock/alice.py @@ -0,0 +1,22 @@ +""" +!/usr/bin/env python + +https://pypi.org/project/websockets/ + +@author Lachezar Todorov +""" + +import asyncio +import encrypt_decrypt + +from websockets import connect + +class Alice(): + async def messageEncrypt(uri, publicKey, message): + encryptedMessage = encrypt_decrypt.encrypt_decrypt.encryption(message, publicKey) + async with connect(uri) as websocket: + await websocket.send(encryptedMessage) + message = await websocket.recv() + print(message) + + #asyncio.run(hello("ws://localhost:8765")) \ No newline at end of file diff --git a/src/py-cli/websock/bob.py b/src/py-cli/websock/bob.py new file mode 100644 index 0000000..81a2563 --- /dev/null +++ b/src/py-cli/websock/bob.py @@ -0,0 +1,24 @@ +""" +!/usr/bin/env python + +https://pypi.org/project/websockets/ + +@author Lachezar Todorov +""" + +import asyncio +import encrypt_decrypt +from websockets import serve + +class Bob(): + async def decrypt(websocket, path, privateKey): + async for message in websocket: + message = await websocket.recv() + decryptedMessage = encrypt_decrypt.encrypt_decrypt.decryption(message, privateKey) + await websocket.send(decryptedMessage) + + async def main(privateKey): + async with serve(Bob.decrypt, "localhost", 8765, privateKey): + await asyncio.Future() # run forever + + #asyncio.run(main()) \ No newline at end of file