29 lines
802 B
Python
Executable File
29 lines
802 B
Python
Executable File
"""
|
|
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) |