dist-iot-net/src/encrypt_decrypt.py
hornet a85b08f050
Feature/py iot (#4)
* Changing files but having issues with space tabs.

* Removed config. Fixed indents.

* Removed config.

* Fixed test config. Fixed syntax in cli.

* Editing cli and hub to finally commit transactions.

* Added better logging logic.
Will clean up in the morning.

* Hub is more functional.

* Edited IoT to gen keys.

* Public keys are now generated, sent, and stored.
However, they are not transmitted when a new client joins.

* Added crypto testing

* Communication is now functional.
2021-11-28 19:00:46 +00:00

35 lines
1.0 KiB
Python
Executable File

"""
Encrypting and decrypting a message
https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/
@author hornetfighter515
@author Todorov-Lachezar
"""
import rsa
def make_keys(key_length):
publicKey, privateKey = None,None
# 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