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.

This commit is contained in:
Lachezar 2021-10-17 04:16:54 -04:00
parent f98c630ea2
commit 2b1f25e131
4 changed files with 95 additions and 23 deletions

31
src/py-cli/encrypt_decrypt.py Executable file → Normal file
View File

@ -8,24 +8,31 @@ 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)
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(message.encode(),
publicKey)
print("original string: ", message)
print("encrypted string: ", encMessage)
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(encMessage, privateKey).decode()
print("decrypted string: ", decMessage)
decMessage = rsa.decrypt(ciphertext, privateKey).decode()
return decMessage

View File

@ -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))

View File

@ -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"))

24
src/py-cli/websock/bob.py Normal file
View File

@ -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())