How can I do asymmetric encryption by using Stellar's private key of sender and Stellar's public key of receiver and vice-versa in case of case decryption? I had gone through many methods but I am not getting what I want.
Giving full credit to Johan here and mostly just want to have an answer down on this question.
In general, you need to take your Stellar keypair in its raw form (not its StrKey representation), convert it to a
curve25519 key (which is definitely doable with
libsodium and its various bindings in multiple languages, and take a look at the sealed box encryption provided in
libsodium and friends.
In Python, you can use PyNaCl to utilize libsodium in Python. Summarizing the exact three steps he mentioned:
- Take your Stellar keypair and convert it from its StrKey encoding (traditionally
GASOMELONGSETOFCHARACTERSfor a public key) to the raw bytes. It's possible to do this using the Python SDK (take a look at the KeyPair class, and getting the raw bytes).
- Because it's an
ed25519key, you can't encrypt with it. You need to create a key based off of it (in particular, the easiest is to make a
curve25519key). You can do with
nacl.signing.SigningKey.to_curve25519_private_keyonce you've instantiated the
nacl.signing.SigningKeyproperly from your bytes (See the SigningKey class. You can see this public method in the docs.
- From there, encrypt using SealedBox in PyNaCl as you see fit.