dist-iot-net/src/client/encrypt_decrypt.py
hornet 5d0a26ca7e
Moved a bunch of files (#3)
Removed dumb stuff. Added nicer logging.
2021-11-16 01:02:14 +00:00

36 lines
1.1 KiB
Python
Executable File

"""
Encrypting and decrypting a message
https://www.geeksforgeeks.org/how-to-encrypt-and-decrypt-strings-in-python/
@author Todorov-Lachezar
"""
import rsa
class EncryptDecrypt():
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