How Nostr Chats Really Work

Ever wondered how private chats work on a public, decentralized network? This deep‑dive explains Nostr Kind‑4 (NIP‑04) encrypted direct messages – from the ECDH shared secret to AES‑256‑CBC encryption – and reveals what’s truly private (content) vs. what’s still public (metadata). Learn how your messages stay safe without a central server ever touching them.

InvalidCastX

July 10, 2026 · 6 min read

If you’ve ever used a Nostr client and tapped “New Message,” you might wonder: how does a private chat work on a network where every event is public? The answer is clever encryption that happens entirely on your device. This guide will walk you through the magic of Nostr Kind‑4 DMs (NIP‑04), from key exchange to decryption, with clear visuals along the way.


1. The Nostr Universe in a Nutshell#

Before we dive into messages, let’s quickly recap what Nostr is.

Nostr is a decentralised protocol built on three simple building blocks:

Because every event is signed and public by default, you might ask: how do we get private conversations? The answer is end‑to‑end encryption bolted onto that public infrastructure.


2. What Is a Kind‑4 Event?#

Every Nostr event has a kind number that tells clients what type of event it is. Kind‑4 is the special code for Encrypted Direct Messages. Here’s the raw structure of a Kind‑4 DM:

{"id": "abc123...","pubkey": "3e6be6b0...","created_at": 1712345678,"kind": 4,"tags": [["p", "7c8a6f9b..."]],"content": "AesEncryptedBase64String...","sig": "signature..."}

The beauty is that any relay can store these events, but without the correct keys, the content looks like gibberish. This is how private messages travel over a public network.


3. The Encryption Magic: How It Stays Private#

Now the cool part: how that content becomes unreadable to everyone except you and your chat partner.

3.1. Deriving the Shared Secret

Nostr DMs use Elliptic Curve Diffie‑Hellman (ECDH) over the secp256k1 curve (the same one used by Bitcoin). Here’s the step‑by‑step:

  1. Sender takes their own private key and the recipient’s public key.
  2. Using ECDH, they compute a shared secret – a value that is identical to what the recipient would compute using their private key and the sender’s public key.
  3. This shared secret is then hashed with SHA‑256 to create a symmetric encryption key (the exact key material used by AES).
  4. The sender picks a random 16‑byte initialisation vector (IV).

Only the two chat participants can ever compute this same shared secret. Relays, observers, even the developer of the app – nobody else can do the maths.

3.2. Encrypting the Message

The sender:

The result is a string like: "A1B2C3D4...eQ==" – which is what goes into the content field of the Kind‑4 event.

When the recipient receives this event, they:

All of this happens locally on your device. Your nsec never leaves your browser/app.


4. A Real Chat Journey, Step by Step#

Let’s follow a message from Alice to Bob.

  1. Alice opens her Nostr chat app and types “Hey Bob, coffee?”
  2. The app takes her private key (which she unlocked with her password) and Bob’s public key.
  3. It computes the shared secret, generates a random IV, and encrypts the message.
  4. It creates a signed Kind‑4 event containing the encrypted blob and a p tag pointing to Bob.
  5. The event is published to several relays that Alice and Bob both use (or to all of Alice’s configured relays).
  6. Bob’s client is subscribed to those relays. When it sees a new event with kind=4 and a p tag matching Bob’s public key, it downloads the event.
  7. Bob’s app decrypts the content using Bob’s private key and Alice’s public key, and Bob sees “Hey Bob, coffee?”

The entire process is fast (usually a fraction of a second) and completely transparent to the user.


5. What Kind‑4 DMs Do (and Don’t) Protect#

✅ What’s protected

❌ What’s NOT hidden

This is often called “metadata leakage.” It’s a trade‑off: you get fully decentralised, censorship‑resistant messaging, but at the cost of visible social graphs.


6. The Future: Better Encryption in Nostr#

Nostr developers recognised the limitations of NIP‑04 and have created newer standards:

NostrChat currently uses Kind‑4 (NIP‑04) because it’s the most widely supported, but support for NIP‑17 and NIP‑44 is coming soon!


7. Wrapping Up#

Now you know exactly what happens when you press “Send” in a Nostr chat. It’s a beautiful combination of public infrastructure and private cryptography – no central server ever sees your messages, and you hold the keys to your own conversations. As Nostr evolves, chat will become even more secure, but the fundamental principle remains: you control your data, always.

Ready to try it yourself? Jump into NostrChat and send your first encrypted DM! 🚀


Here's an working prototype of a separate webapp dedicated on sending messages (also includes GIF and sending images)

https://nostrchat.vercel.app

If you have more questions about Nostr privacy, check out our in‑app “Privacy & Security” section – no telemetry, no tracking, just honest explanations.

#aes256#bch#bchnostr#bitcoin#cryptography#cybersecurity#decentralized#ecdh#encryptedchat#endtoendencryption#kind4#messaging#nip04#nip17#nip44#nostr#nostrprotocol#privacy#secp256k1#selfsovereign#web3
00

Written by

I write articles that your algorithm has successfully hidden from you

invalidcastx@bchnostr.com

Comments0

No comments yet. Start the conversation.