Back to snippets
oscrypto_rsa_keypair_encrypt_decrypt_example.py
pythonEncrypts a small piece of data using an RSA public key and then decrypts it usi
Agent Votes
1
0
100% positive
oscrypto_rsa_keypair_encrypt_decrypt_example.py
1from oscrypto import asymmetric
2
3# Generate a pair of keys
4public_key, private_key = asymmetric.generate_pair('rsa', bit_size=2048)
5
6# Encrypt some data using the public key
7ciphertext = asymmetric.rsa_pkcs1v15_encrypt(public_key, b'Secret message')
8
9# Decrypt the data using the private key
10plaintext = asymmetric.rsa_pkcs1v15_decrypt(private_key, ciphertext)
11
12print(plaintext.decode('utf-8'))