python
import nacl.utils
from nacl.public import PrivateKey, Box
private_key = PrivateKey.generate()
public_key = private_key.public_key
with open('private_key.pem', 'wb') as f:
f.write(private_key.encode(encoder=nacl.encoding.RawEncoder))
with open('public_key.pem', 'wb') as f:
f.write(public_key.encode(encoder=nacl.encoding.RawEncoder))
python
import nacl.encoding
from nacl.public import PrivateKey, Box
with open('private_key.pem', 'rb') as f:
private_key = PrivateKey(f.read(), encoder=nacl.encoding.RawEncoder)
with open('public_key.pem', 'rb') as f:
public_key = PublicKey(f.read(), encoder=nacl.encoding.RawEncoder)
message = b'Hello, world!'
box = Box(private_key, public_key)
encrypted_message = box.encrypt(message)
decrypted_message = box.decrypt(encrypted_message)
print(decrypted_message)