Skip to content

Instantly share code, notes, and snippets.

@TheAlchemistKE
Created June 8, 2023 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheAlchemistKE/0e4ab66cae291030ccc685f4e65a1958 to your computer and use it in GitHub Desktop.
Save TheAlchemistKE/0e4ab66cae291030ccc685f4e65a1958 to your computer and use it in GitHub Desktop.
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