<p>After some research I found that the ECIES algorithm is what I was looking for. First, I tried the library <code>bitcore-ecies</code> but seems to be deprecated, old and buggy. So I pick <code>ecies-lite</code>. This is the result:</p>
<pre class="lang-js prettyprint-override"><code>import pkg from 'bitcore-lib'
import * as ecies from 'ecies-lite'

const { PrivateKey, Networks } = pkg

function generateKeys () {
  const network = Networks.livenet
  const privateKey = new PrivateKey(Networks[network])
  return {
    privateKey: privateKey,
    P2PKHAddress: privateKey.toAddress(),
    publicKey: privateKey.toPublicKey(),
    wif: privateKey.toWIF()
  }
}

function main() {
  const result = generateKeys()
  const body = ecies.encrypt(
    result.publicKey.toBuffer(),
    Buffer.from('This message is for demo purpose')
  )
  console.log(body)
  const message = ecies.decrypt(result.privateKey.toBuffer(), body)
  console.log(message.toString())
}

main()

</code></pre>

CC BY-SA 4.0
2022-10-21T21:24:11.010Z
2022-10-21T21:24:11.010Z
Cartucho
<p>I'm wondering if it's possible to use the Bitcoin public and private keys (usually used for signing) for text encryption. This is an example in nodeJs:</p>
<pre class="lang-js prettyprint-override"><code>import pkg from 'bitcore-lib'

const { PrivateKey, Networks } = pkg
const privateKey = new PrivateKey(Networks['livenet'])
const myObject = {
    privateKey: privateKey,
    P2PKHAddress: privateKey.toAddress(),
    publicKey: privateKey.toPublicKey(),
    wif: privateKey.toWIF()
}
</code></pre>
<p>It is possible to use <code>publicKey</code> to encrypt a message and decrypt with  <code>privateKey</code>?</p>
<pre class="lang-js prettyprint-override"><code>const encryptedText = encrypt('Hello', publicKey)
const decryptedText = decrypt(encryptedText, privateKey)

assert decryptedText === 'Hello'

</code></pre>

<cryptography><bitcore><nodejs>
How to encrypt messages with Pub/Priv keys
Cartucho
# How to encrypt messages with Pub/Priv keys

(Asked by: username_0 on 2022-10-21)

I'm wondering if it's possible to use the Bitcoin public and private keys (usually used for signing) for text encryption. This is an example in nodeJs:

```js
import pkg from 'bitcore-lib'

const { PrivateKey, Networks } = pkg
const privateKey = new PrivateKey(Networks['livenet'])
const myObject = {
    privateKey: privateKey,
    P2PKHAddress: privateKey.toAddress(),
    publicKey: privateKey.toPublicKey(),
    wif: privateKey.toWIF()
}

```

It is possible to use `publicKey` to encrypt a message and decrypt with `privateKey`?

```js
const encryptedText = encrypt('Hello', publicKey)
const decryptedText = decrypt(encryptedText, privateKey)

assert decryptedText === 'Hello'

```

**Tags:** cryptography, bitcore, nodejs


---

## 1 answers

### Answer by username_0 on 2022-10-21. Score: 1

After some research I found that the ECIES algorithm is what I was looking for. First, I tried the library `bitcore-ecies` but seems to be deprecated, old and buggy. So I pick `ecies-lite`. This is the result:

```js
import pkg from 'bitcore-lib'
import * as ecies from 'ecies-lite'

const { PrivateKey, Networks } = pkg

function generateKeys () {
  const network = Networks.livenet
  const privateKey = new PrivateKey(Networks[network])
  return {
    privateKey: privateKey,
    P2PKHAddress: privateKey.toAddress(),
    publicKey: privateKey.toPublicKey(),
    wif: privateKey.toWIF()
  }
}

function main() {
  const result = generateKeys()
  const body = ecies.encrypt(
    result.publicKey.toBuffer(),
    Buffer.from('This message is for demo purpose')
  )
  console.log(body)
  const message = ecies.decrypt(result.privateKey.toBuffer(), body)
  console.log(message.toString())
}

main()

```


---

