# Error while using IPFS keypair for public key cryptography

**URL:** <https://discuss.ipfs.tech/t/error-while-using-ipfs-keypair-for-public-key-cryptography/10520>\
**Category:** Help\
**Tags:** js-ipfs, encryption\
**Created:** [March 17, 2021, 4:08pm UTC](https://discuss.ipfs.tech/t/error-while-using-ipfs-keypair-for-public-key-cryptography/10520 "2021-03-17T16:08:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krithik](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@krithik](https://discuss.ipfs.tech/u/krithik)\
**Post date:** [March 17, 2021, 4:08pm UTC](https://discuss.ipfs.tech/t/error-while-using-ipfs-keypair-for-public-key-cryptography/10520/1 "2021-03-17T16:08:07Z")

</div>

Hello IPFS community,

I’m trying to share a small encrypted message between two nodes (say node1 wants to share a message with node2) using public key cryptography (i.e. node1 encrypts the data with node2’s public key and node2 decrypts it with their private key). For this, I’m trying to use the IPFS node’s own key pair. I’m following the procedure that’s very similar to the one mentioned in this [gist](https://gist.github.com/npfoss/663689d74a2318c13cb8754cef295e6a).

To get the public key at node2, I’m using

```
let pub_key = (await node.id()).publicKey

```

This key is then written to orbit-db, which is read by node1. The encryption process at node1’s side  
usng the public key is:

```
// assume public key obtained from the orbit-db is stored in pub_key

pub_key = await p2pcrypto.keys.unmarshalPublicKey(Buffer.from(pub_key, 'base64'))

let publicKey = {
    n: Buffer.from(pub_key._key.n, 'base64'),
    e: Buffer.from(pub_key._key.e, 'base64'),
}

let usefulPubKey = new NodeRSA()

usefulPubKey.importKey(publicKey, 'components-public')

let cipher = usefulPubKey.encrypt(data)

return cipher;

```

This encrypted data is then read by node1, and is decrypted as follows:

```
let private_key = (await node.config.get("Identity"))["PrivKey"]
private_key = await p2pcrypto.keys.unmarshalPrivateKey(Buffer.from(private_key, 'base64'))

let usefulPrivKey = new NodeRSA();

usefulPrivKey.importKey({
    n: Buffer.from(private_key._key.n, 'base64'),
    e: Buffer.from(private_key._key.e, 'base64'),
    d: Buffer.from(private_key._key.d, 'base64'),
    p: Buffer.from(private_key._key.p, 'base64'),
    q: Buffer.from(private_key._key.q, 'base64'),
    dmp1: Buffer.from(private_key._key.dp, 'base64'),
    dmq1: Buffer.from(private_key._key.dq, 'base64'),
    coeff: Buffer.from(private_key._key.qi, 'base64'),
}, 'components');

let messageBytes = usefulPrivKey.decrypt(data)
let plain = messageBytes.toString()

return plain

```

The issue I’m facing is that sometime this works fine – the message is successfully decrypted back into the plaintext. But, other times I get this error:

![image](https://us1.discourse-cdn.com/flex020/uploads/ipfs1/original/2X/e/e50e80faeba5e63f35590a6cd30bb449cd1a96fe.png)

Could anyone help me understand the issue here?

---

<div class="post-metadata">

**Author:** ![wclayf](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ipfs.tech/wclayf/32/4936_2.png) [@wclayf](https://discuss.ipfs.tech/u/wclayf)\
**Post date:** [March 18, 2021, 2:04am UTC](https://discuss.ipfs.tech/t/error-while-using-ipfs-keypair-for-public-key-cryptography/10520/2 "2021-03-18T02:04:32Z")

</div>

In case it helps, I’m using ‘crypto.subtle’ API, and doing the kind of stuff you’re talking about here:

> <https://github.com/Clay-Ferguson/quantizr/blob/master/src/main/resources/public/ts/Encryption.ts>

The crypto API is in all web browsers, and just because of that and known browser compatibility that would be the API I’d use, but I’m not sure it’s available in NPM (non-browser uses)

---

<div class="post-metadata">

**Author:** ![krithik](https://avatars.discourse-cdn.com/v4/letter/k/eada6e/32.png) [@krithik](https://discuss.ipfs.tech/u/krithik)\
**Post date:** [March 18, 2021, 12:17pm UTC](https://discuss.ipfs.tech/t/error-while-using-ipfs-keypair-for-public-key-cryptography/10520/3 "2021-03-18T12:17:33Z")

</div>

Thanks, I’ll take a look
