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

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

@ -7,25 +7,32 @@ https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/
""" """
import rsa import rsa
# Generates public and private keys class encrypt_decrypt():
# Method takes in key length as a parameter
# Note: the key length should be >16 # the message we want to encrypt
publicKey, privateKey = rsa.newkeys(512) message = "Hello World"
# the message we want to encrypt def make_keys(key_length):
message = "Hello World" publicKey, privateKey = ""
# Generates public and private keys
# Encrypts the message with the public key # Method takes in key length as a parameter
# Note: make sure to encode the message before encrypting # Note: the key length should be >16
encMessage = rsa.encrypt(message.encode(), if(key_length < 16):
publicKey) publicKey, privateKey = rsa.newkeys(key_length)
return publicKey, privateKey
print("original string: ", message) else:
print("encrypted string: ", encMessage) return "Enter a key_length of >16"
# Decrypts the encrypted message with the private key
# Note: make sure to decode the message after decryption def encryption(plaintext, publicKey):
decMessage = rsa.decrypt(encMessage, privateKey).decode() # Encrypts the message with the public key
# Note: make sure to encode the message before encrypting
print("decrypted string: ", decMessage) 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

View File

@ -1,5 +1,24 @@
""" """
A client to send an encrypted message to another client 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())