bin-y / standard-ecies

Standard ECIES implemention for NodeJS based on crypto.ECDH with no other dependencies.
Creative Commons Zero v1.0 Universal
8 stars 3 forks source link

TODO: More docs, examples and tests #1

Open bin-y opened 8 years ago

qisbic commented 7 years ago

I have done a lot of search for ECIES and your code seems to be the only one that works for me as a Windows user. I am using this with Chrome extension.

I would like to know how to load an external generated public key generated by BouncyCastle. Or load in the hex for X and Y values to encypt.

privateKey = "-----BEGIN EC PRIVATE KEY-----\r\nMIIBCwIBAQQgXUriUohju+MZIN/6GmejKI+9pw5fs464BzK8IbRPX+GggeMwgeAC\r\nAQEwLAYHKoZIzj0BAQIhAP////////////////////////////////////7///wv\r\nMEQEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABCAAAAAAAAAAAAAA\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAABwRBBHm+Zn753LusVaBilc6HCwcCm/zbLc4o\r\n2VnygVsW+BeYSDradyajxGVdpPv8DhEIqP0XtEimhVQZnEfQj/sQ1LgCIQD/////\r\n///////////////+uq7c5q9IoDu/0l6M0DZBQQIBAQ==\r\n-----END EC PRIVATE KEY-----\r\n" string

"-----BEGIN PUBLIC KEY-----\r\nMIIBMzCB7AYHKoZIzj0CATCB4AIBATAsBgcqhkjOPQEBAiEA////////////////\r\n/////////////////////v///C8wRAQgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nAAAAAAAAAAAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHBEEEeb5m\r\nfvncu6xVoGKVzocLBwKb/NstzijZWfKBWxb4F5hIOtp3JqPEZV2k+/wOEQio/Re0\r\nSKaFVBmcR9CP+xDUuAIhAP////////////////////66rtzmr0igO7/SXozQNkFB\r\nAgEBA0IABGMgfhYdsm7dTzR6Nr1+lUlLoCDzqlJq/QBV41mcON2yCJQ9SnNS3YUa\r\nxA8SwkXnnuatFpSmd8UfINaq+cLktO8=\r\n-----END PUBLIC KEY-----\r\n"

bin-y commented 7 years ago

@qisbic Glad to see my first issue reporter on github 😄, I think ecdh.setPrivateKey is what you want. Just replace ecdh.generateKeys() in my example code to ecdh.setPrivateKey, it should work BTW, since you published your private key in here, you should never use this key in production environment, it's already unsafe.

qisbic commented 7 years ago

Unfortunate.. this is not what I want.. the client is receiving a "public" key which is used to encrypt.

Unfortunately, according to the documentation the ecdh.setPublicKey has been Deprecated.

I have struggled for a few days trying to find javascript codes that will let me set a public key to encrypt for ECIES. Almost all codes used are ECDH/ECDSA in which the client generate the keys. Your codes come the closest to what I need to do.

Thanks for any suggestions you would have.

bin-y commented 7 years ago

@qisbic

server side:

ecdh.setPrivateKey()
publicKey = ecdh.getPublicKey();// send this to client

client side:

ecies.encrypt(publicKey);

if crypto.ECDH can import your private key, encryption with public key on client side won't be a problem

But after doing this ,client still generate a temporary key on each encryption, that's the first step to do according to the standard (see first step of Encryption)

bin-y commented 7 years ago

@qisbic maybe I didn't fully understand your requirement before. After reconsider the requirement of generate multiple public key from one private key, since nodejs didn't implement that in ECDH, import keys from other crypto libraries is one of the solution. So I did some search on your problem, here is some information that looks helpful:

your key is ASN.1 encoded, and from this link you can see detailed data structure of your public key.

And from this mail archive it looks like the key you using is invalid encoded, but it is ok, from the correct encoding we can see that the value you wanted to use with crypto.ECDH is the BIT_STRING part.

There is an ASN.1 library on npm you can use it to decode your public key and get the last part use it as the public key on ecdh. I'm not sure but it should be same way of your library encoding public ec point and the way node did, because how to encode the point is standardized, you can check it by reading the source, here is how the node output the public key.

Hope this post will help you.