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.
-
3There aren't that many options. tweetnacl-box, like, I showed you, does work. Maybe you can expand on what you're trying to accomplish?– Johan SténSep 27, 2018 at 11:45
-
Yeah I tried using that but it is not accepting stellar's keypair.– priSep 27, 2018 at 11:58
-
1Of course it doesn't. ed25519 keys are signing keys. They need to be converted to curve25519 keys first. And before that, you need to convert the keys from StrKey encoding to raw binary.– Johan SténSep 27, 2018 at 12:06
-
Yeah I know that but still I am facing problem because the library you suggested was written in js and I am a python developer– priSep 29, 2018 at 5:28
-
There are python libraries for this also, that's what I used. Years ago though, so I don't remember what they are.– Johan SténOct 1, 2018 at 5:08
1 Answer
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
GASOMELONGSETOFCHARACTERS
for 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
ed25519
key, you can't encrypt with it. You need to create a key based off of it (in particular, the easiest is to make acurve25519
key). You can do withnacl.signing.SigningKey.to_curve25519_private_key
once you've instantiated thenacl.signing.SigningKey
properly 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.