Created
June 8, 2023 06:24
-
-
Save TheAlchemistKE/0e4ab66cae291030ccc685f4e65a1958 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cryptography.hazmat.primitives import serialization | |
from cryptography.hazmat.primitives.asymmetric import rsa, padding | |
from cryptography.hazmat.primitives import hashes | |
# Generate RSA key pair | |
private_key = rsa.generate_private_key( | |
public_exponent=65537, | |
key_size=2048 | |
) | |
public_key = private_key.public_key() | |
# Encrypt | |
message = b"Hello, world!" | |
ciphertext = public_key.encrypt( | |
message, | |
padding.OAEP( | |
mgf=padding.MGF1(algorithm=hashes.SHA256()), | |
algorithm=hashes.SHA256(), | |
label=None | |
) | |
) | |
# Decrypt | |
decrypted_message = private_key.decrypt( | |
ciphertext, | |
padding.OAEP( | |
mgf=padding.MGF1(algorithm=hashes.SHA256()), | |
algorithm=hashes.SHA256(), | |
label=None | |
) | |
) | |
print("Original message:", message.decode()) | |
print("Cipher Text:", ciphertext) | |
print("Decrypted message:", decrypted_message.decode()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment