From 2b1f25e131bd501d0f23a0e7e4af4d5e474062a4 Mon Sep 17 00:00:00 2001 From: Lachezar <31971186+Todorov-Lachezar@users.noreply.github.com> Date: Sun, 17 Oct 2021 04:16:54 -0400 Subject: [PATCH] Took the encrypt_decrypt class and changed it to a class with functions. Added in alice.py and bob.py. The idea is that from main.py, Alice is called and encrypts then sends a message, while Bob receives the message and decrypts it. At some point the after Bob decrypts it the plaintext should be printed to show that Bob did it successfully, however nothing gets printed at the moment. --- src/py-cli/encrypt_decrypt.py | 51 ++++++++++++++++++++--------------- src/py-cli/main.py | 21 ++++++++++++++- src/py-cli/websock/alice.py | 22 +++++++++++++++ src/py-cli/websock/bob.py | 24 +++++++++++++++++ 4 files changed, 95 insertions(+), 23 deletions(-) mode change 100755 => 100644 src/py-cli/encrypt_decrypt.py create mode 100644 src/py-cli/websock/alice.py create mode 100644 src/py-cli/websock/bob.py 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