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.
This commit is contained in:
parent
2e47014923
commit
33256b73a7
29
src/py-cli/encrypt_decrypt.py
Executable file
29
src/py-cli/encrypt_decrypt.py
Executable file
|
@ -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)
|
Loading…
Reference in New Issue
Block a user