From 33256b73a7b230654a6cb7bc349c58826118a7d1 Mon Sep 17 00:00:00 2001 From: Lachezar Todorov Date: Sat, 16 Oct 2021 12:55:00 -0400 Subject: [PATCH] Created a new file that encrypts and decrypts a message using the rsa library. Note: make sure to install the rsa library before using the following code. --- src/py-cli/encrypt_decrypt.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100755 src/py-cli/encrypt_decrypt.py diff --git a/src/py-cli/encrypt_decrypt.py b/src/py-cli/encrypt_decrypt.py new file mode 100755 index 0000000..e02d4e0 --- /dev/null +++ b/src/py-cli/encrypt_decrypt.py @@ -0,0 +1,29 @@ +""" +Encrypting and decrypting a message + +@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) \ No newline at end of file