EOSIO / eosjs-ecc

Elliptic curve cryptography functions: Private Key, Public Key, Signature, AES, Encryption, Decryption
288 stars 119 forks source link

I need example of encoding and decoding string via public and private key. #29

Closed HomeRobot closed 6 years ago

HomeRobot commented 6 years ago

I mean the scenario when Alice sign/encrypt message via public key of Bob and than Bob decode the message use own private key.

chris-allnutt commented 6 years ago

@HomeRobot while that could be added to this library our primary usage of this is for signing not encryption.

HomeRobot commented 6 years ago

Ok, thanks. May be you can tell how to convert EOS keys to "classic" bitcoin keys?

jcalfee commented 6 years ago

You need to work with Eos.modules.ecc.PrivateKey and the raw data. Here is an example that gets you to the raw 32 private key bytes:

> Eos.modules.ecc.PrivateKey.fromSeed('test').toBuffer()
<Buffer 9f 86 d0 81 88 4c 7d 65 9a 2f ea a0 c5 5a d0 15 a3 bf 4f 1b 2b 0b 82 2c d1 5d 6c 15 b0 f0 0a 08>
> Eos.modules.ecc.PrivateKey.fromSeed('test').toBuffer().length
32

Here is the Object API: https://github.com/eosio/eosjs-ecc#usage-object-api

jcalfee commented 6 years ago

You'll have to do this in your own code like Chris pointed out..

Often Bob and Alice will calculate a shared secret and use that to encrypt.. Something like this is used below but you'll have to adapt it.

This example uses signing instead of encryption and client server instead of Alice and Bob. But this one way to do it. You could use the shared secret (one-time key) for encrypting and decrypting. Every single message needs a new shared secret. Only use it one time to get the entropy needed to protect the encrypting private key.. It is important that every message use a new "one-time" private as the name implies.

https://github.com/EOSIO/eosjs-keygen/blob/v1.3.2/src/keystore.test.js#L388-L422