Giter Club home page Giter Club logo

electrum-ecies's Introduction

Electrum ECIES

This repo is depreciated, as the project is merged into bsv library.

You can view new usage with bsv library.

Electrum ECIES js implement, encrypt/decrypt message in BIE1 format, like Electrum/Electron Tools -> Encrypt/decrypt message.

You can encrypt message just with receiver's publickey, and receiver can decrypt message with his privatekey.

Usage

Encrypt a message, so it can be decrypted in Electrum/Electron Cash Wallet.

Or decrypt message Electrum/Electron Cash Wallet encrypted.

Install via NPM

npm install electrum-ecies

In browser

<script src='https://unpkg.com/bsv'></script>
<script src='https://unpkg.com/electrum-ecies'></script>

Examples

Encrypt Message with Receiver's PublicKey

const ECIES = require('electrum-ecies');

//Receiver's PublicKey: 02c01c35eef31acb60386f7fb8e0267f08faf5724d1b2fa1f3588a5fef3e726309

ECIES.encrypt('Hello','02c01c35eef31acb60386f7fb8e0267f08faf5724d1b2fa1f3588a5fef3e726309'); 

//'QklFMQMFmPdvjFe8Wfo+JWmTpo+33LXc+4G8ThfaucU72kieb6lWEv4layTb0x5tzpi6lA2it8rO/ELrXomJqC53uBOd+DZSzDhCSpK6SwR+Itt+Pw=='

//BIE1 use ephemeral keys, so ciphertext is different every time.

Decrypt BIE1 Message with Receiver's PrivateKey

const ECIES = require('electrum-ecies');

//Receiver's PrivateKey:a1b50c4d420b20059b01e7eea3b3d8a5e943728dfedf962628ca18d04bfa2cfc

//Decrypt Above Message

ECIES.decrypt('QklFMQMFmPdvjFe8Wfo+JWmTpo+33LXc+4G8ThfaucU72kieb6lWEv4layTb0x5tzpi6lA2it8rO/ELrXomJqC53uBOd+DZSzDhCSpK6SwR+Itt+Pw==','a1b50c4d420b20059b01e7eea3b3d8a5e943728dfedf962628ca18d04bfa2cfc')

//<Buffer 68 65 6c 6c 6f>

I Just Want the ECDH Key

const ECIES = require('electrum-ecies');

const PublicKey = require('bsv').PublicKey;

const PrivateKey = require('bsv').PrivateKey;

var ecdh_key = ECIES.ecdh_key(new PublicKey([Receiver Pubkey]),new PrivateKey([Sender/Ephemeral PrivKey]))

var iv=ecdh_key.subarray(0,16);

var key_aes=ecdh_key.subarray(16,32);

var key_hmac=ecdh_key.subarray(32,64);

Traditional 2 keys ECIES

const ECIES = require('electrum-ecies');
//Receiver Private Key:3876fd62a094f0700077c52f3e95cf776c1d3bc26937ed9a8c7da316b4486d2a
//Receiver Public Key:03e7f56e86cab54141d4cd1c49c79cdd31803ddb25aa1e2e37692c54f592e432c7
//Sender Private Key:da25c51abb3ef47d496caf4b857d9490949d926ba449de3b8e68417eecc71bf9
//Sender Public Key:0343ea04cfc5df7b486f4e37583fe43f553bd8d50c0a3ba8cd046c628de94828fd


//Encrypt with Receiver PublicKey and Sender PrivateKey
ECIES.encrypt('Hi ECIES','03e7f56e86cab54141d4cd1c49c79cdd31803ddb25aa1e2e37692c54f592e432c7','da25c51abb3ef47d496caf4b857d9490949d926ba449de3b8e68417eecc71bf9')
//'QklFMQND6gTPxd97SG9ON1g/5D9VO9jVDAo7qM0EbGKN6Ugo/RA+JiiLFOL59qviXS+MB87SL2x2pDfJr7vUttchThLokqLeeUKphuvGI+iLQ2Eulg=='

//Decrypt with Receiver PrivateKey
ECIES.decrypt('QklFMQND6gTPxd97SG9ON1g/5D9VO9jVDAo7qM0EbGKN6Ugo/RA+JiiLFOL59qviXS+MB87SL2x2pDfJr7vUttchThLokqLeeUKphuvGI+iLQ2Eulg==','3876fd62a094f0700077c52f3e95cf776c1d3bc26937ed9a8c7da316b4486d2a')
//<Buffer 48 69 20 45 43 49 45 53>

//Decrypt with Receiver PublicKey and Sender PrivateKey (if sender want to retrieve message in this case)
ECIES.decrypt('QklFMQND6gTPxd97SG9ON1g/5D9VO9jVDAo7qM0EbGKN6Ugo/RA+JiiLFOL59qviXS+MB87SL2x2pDfJr7vUttchThLokqLeeUKphuvGI+iLQ2Eulg==','da25c51abb3ef47d496caf4b857d9490949d926ba449de3b8e68417eecc71bf9','03e7f56e86cab54141d4cd1c49c79cdd31803ddb25aa1e2e37692c54f592e432c7')
//<Buffer 48 69 20 45 43 49 45 53>

//Note: By default, ECIES encrypt with random ephemeral private key which can't be retrieved later. We overrided ephemeral key with given private key here.

Donation

If you like it.

BSV address: 1BHcPbcjRZ9ZJvAtr9nd4EQ4HbsUC77WDf

I'm a supporter of BSV, but you are free to use it on BTC/BCH etc.

License

BSD-2-Clause

electrum-ecies's People

Contributors

kevinejohn avatar monkeylord avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

electrum-ecies's Issues

Not working in the browser

The ECIES encrypt/decrypt works perfectly on the server, but the decryption does not work in the browser. It seems there is something strange going on with regard to buffers.

This line
var mac = encrypted.subarray(encrypted.length - 32);
Does not create a valid buffer in mac, which is checked later on in the code when it is compared to the hmac
if(hmac.compare(mac) != 0) .....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.