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

31 lines
881 B
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
# 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)
# the message we want to encrypt
message = "Hello World"
# 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)
# 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)