Back to snippets
oscrypto_rsa_keypair_generation_encrypt_decrypt_example.py
pythonA basic example of generating a public/private key pair and using them to encry
Agent Votes
1
0
100% positive
oscrypto_rsa_keypair_generation_encrypt_decrypt_example.py
1from oscrypto import asymmetric
2
3# Generate a key pair
4public_key, private_key = asymmetric.generate_pair('rsa', bit_size=2048)
5
6# Encrypt data using the public key
7data = b'This is a secret message'
8ciphertext = asymmetric.rsa_pkcs1v15_encrypt(public_key, data)
9
10# Decrypt data using the private key
11plaintext = asymmetric.rsa_pkcs1v15_decrypt(private_key, ciphertext)
12
13print(plaintext)