EOSIO / eosjs-ecc

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

Encrypt data using public key, decrypt using EOS private key #19

Closed filipniziol closed 6 years ago

filipniziol commented 6 years ago

Is it possible to add an assymetric encryption options to the library so it is possible to encrypt the data using one's public key and then decrypt the data using one's private key using the same algorithm as the one used for signing the transactions?

There is only symmetric encryption available when both keys are needed

jcalfee commented 6 years ago

You could encrypt the message for yourself using encryptedBuffer = Aes.encrypt(myPrivate, myPublic, 'message') .. That is designed to encrypt messages for other users though (instead of myPublic it would typically read otherPublic).

jcalfee commented 6 years ago

Correct, that is the only case so far.. I'm not sure what you mean by using the same algorithm as signing..

This may be the logic your looking for: https://download.libsodium.org/doc/secret-key_cryptography/authenticated_encryption.html

This is the javascript compiled version and is large: https://github.com/jedisct1/libsodium.js

In any event, libsodium is probably a good guide on exactly how to do it.

filipniziol commented 6 years ago

I think I was not precise: "You could encrypt the message for yourself using encryptedBuffer = Aes.encrypt(myPrivate, myPublic, 'message')" -> This is the example of symmetric encryption.

I am interested in assymetrcic encryption EOS is using for example to generate the key pairs used to sign the transactions. What algorithm is being used? And I understand: we sign the transaction with private key so that anyone with public key can validate it. So what I would like to do is: encrypt the data with someone's public key, so that the owner of the private key can decrypt it. This is assymetric. What you suggested is using some third party library - I wonder how it is related with the algorithm EOS is using in for example eosjs-ecc? If EOS is using one algorithm for signing the transactions I would like to use the same algorithm for encrypting the data - or at least understand how it can be done. Doing it with third party library - will EOS key pairs be consistend with that library? I mean there are always some requirements for key length and construction

jcalfee commented 6 years ago

encrypt the data with someone's public key, so that the owner of the private key can decrypt it

This is usually how this is done:

let {ecc} = Eos.modules

message = 'the data'

someonesPrivateKey=ecc.PrivateKey.fromSeed('someone')
someonesPublicKey=someonesPrivateKey.toPublic()

myPrivate=ecc.PrivateKey.fromSeed('my')
myPublic=myPrivate.toPublic()

encryptedMessage = ecc.Aes.encrypt(myPrivate, someonesPublicKey, message)
assert.equal(message, ecc.Aes.decrypt(someonesPrivateKey, myPublic,
  encryptedMessage.nonce, encryptedMessage.message, encryptedMessage.checksum))

I do not see a struct to hold an encrypted field with the nonce, data, public key, and checksum..

chris-allnutt commented 6 years ago

@filipniziol eosjs is not concerned with encryption of content, while it falls under the area of cryptography our usage is for signing of transactions that can be verified. You could encrypt the message send it then decrypt it but that would be outside of eos-js's use case.

filipniziol commented 6 years ago

@chris-allnutt So the answer is that the question is out of scope. Still I wonder how I can encrypt the message using the algorithm you use for signing the transactions (encrypt with pub key, and decrypt with private key) I close the issue, but if you find some time I have an open issue on stack exchange: https://eosio.stackexchange.com/questions/506/encrypt-data-using-public-key-decrypt-using-eos-private-key-eosjs-ecc I will be grateful for some comment or help