dist-iot-net/src/py-cli/encrypt_decrypt.py

39 lines
1.2 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 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