# Hi, i am trying to decypher web push notification in function node, with no luck

**URL:** <https://discourse.nodered.org/t/hi-i-am-trying-to-decypher-web-push-notification-in-function-node-with-no-luck/82804>\
**Category:** General\
**Created:** [12 November 2023 19:02 UTC](https://discourse.nodered.org/t/hi-i-am-trying-to-decypher-web-push-notification-in-function-node-with-no-luck/82804 "2023-11-12T19:02:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![igisho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/igisho/32/84749_2.png) [@igisho](https://discourse.nodered.org/u/igisho)\
**Post date:** [12 November 2023 19:02 UTC](https://discourse.nodered.org/t/hi-i-am-trying-to-decypher-web-push-notification-in-function-node-with-no-luck/82804/1 "2023-11-12T19:02:09Z")

</div>

Recieving push notification in http in node. and than using this code to decypher with no luck for now...

```auto
const headers = msg.req.headers;
const keys = JSON.parse(msg.payload);

// Extracting headers
const salt = Buffer.from(headers['encryption'].split('=')[1], 'base64');
const clientPublicKey = Buffer.from(headers['crypto-key'].split('=')[1].split(';')[0], 'base64');

// ECDH key agreement
const ecdh = crypto.createECDH('prime256v1');
ecdh.setPrivateKey(Buffer.from(keys.privateKey, 'base64'), 'base64');
const sharedSecret = ecdh.computeSecret(clientPublicKey);

// HKDF
const info = Buffer.concat([Buffer.from('WebPush: info\x00', 'utf8'), clientPublicKey, ecdh.getPublicKey()]);
const rawDerivedKey = crypto.hkdfSync('sha256', sharedSecret, salt, info, 32);
const derivedKey = Buffer.from(rawDerivedKey);

// Decrypting
try {
    const encryptedData = msg.req.body;
    const nonce = encryptedData.slice(0, 12);
    const ciphertext = encryptedData.slice(12, encryptedData.length - 16);
    const authTag = encryptedData.slice(encryptedData.length - 16);

    const decipher = crypto.createDecipheriv('aes-256-gcm', derivedKey, nonce);
    decipher.setAuthTag(authTag);
    const decryptedData = Buffer.concat([decipher.update(ciphertext), decipher.final()]);
    return { payload: decryptedData.toString('utf8') };
} catch (error) {
    console.error(`Error decrypting message: ${error.message}`);
    return { error: error.message };
}

```

---

<div class="post-metadata">

**Author:** ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)\
**Post date:** [13 November 2023 14:55 UTC](https://discourse.nodered.org/t/hi-i-am-trying-to-decypher-web-push-notification-in-function-node-with-no-luck/82804/2 "2023-11-13T14:55:17Z")

</div>

It would probably help to provide:

- Sample input to the function
- Expected output
- Some indication of what you think isn't working, e.g. do you think you get the right delivered key?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 January 2024 14:56 UTC](https://discourse.nodered.org/t/hi-i-am-trying-to-decypher-web-push-notification-in-function-node-with-no-luck/82804/3 "2024-01-12T14:56:11Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
