如何解密Nostr消息?

huangapple go评论86阅读模式
英文:

How do I decrypt a Nostr message?

问题

以下是如何向Nostr Pubkey发送私人消息的代码示例:

const crypto = require("crypto");
const secp = require("noble-secp256k1");

const ourPrivateKey = ""; // 我们的私钥
const ourPubKey = ""; // 我们的公钥
const theirPublicKey = ""; // 对方的公钥
const text = "Hello World"; // 要发送的文本消息

let sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
let sharedX = sharedPoint.substr(2, 64);

let iv = crypto.randomFillSync(new Uint8Array(16));
var cipher = crypto.createCipheriv(
  "aes-256-cbc",
  Buffer.from(sharedX, "hex"),
  iv
);
let encryptedMessage = cipher.update(text, "utf8", "base64");
encryptedMessage += cipher.final("base64");
let ivBase64 = Buffer.from(iv.buffer).toString("base64");

let event = {
  pubkey: ourPubKey,
  created_at: Math.floor(Date.now() / 1000),
  kind: 4,
  tags: [["p", theirPublicKey]],
  content: encryptedMessage + "?iv=" + ivBase64,
};

console.log(event.content);

接收者如何解密接收到的消息呢?解密过程如下:

  1. 接收者从事件对象中提取content字段,其中包含加密的消息和初始化向量(iv)。

  2. 接收者需要知道自己的私钥以及发送者的公钥(在tags中的theirPublicKey字段)。这两者是解密所必需的信息。

  3. 使用接收者的私钥和发送者的公钥来生成共享的密钥。

  4. content字段中提取加密的消息和初始化向量(iv)。

  5. 使用共享的密钥和初始化向量(iv)来解密消息。通常,使用相同的加密算法和模式(在此示例中为aes-256-cbc)以及相同的编码(在此示例中为base64)来执行解密操作。

解密后,接收者将能够获得原始的文本消息。请注意,为了实现解密,接收者需要知道发送者的公钥以及拥有自己的私钥,这些信息在加密和解密过程中都是必需的。

英文:

Here's how to send a private message to a Nostr Pubkey:

const crypto = require("crypto");
const secp = require("noble-secp256k1");

const ourPrivateKey = "";
const ourPubKey = "";
const theirPublicKey = "";
const text = "Hello World";

let sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
let sharedX = sharedPoint.substr(2, 64);

let iv = crypto.randomFillSync(new Uint8Array(16));
var cipher = crypto.createCipheriv(
  "aes-256-cbc",
  Buffer.from(sharedX, "hex"),
  iv
);
let encryptedMessage = cipher.update(text, "utf8", "base64");
encryptedMessage += cipher.final("base64");
let ivBase64 = Buffer.from(iv.buffer).toString("base64");

let event = {
  pubkey: ourPubKey,
  created_at: Math.floor(Date.now() / 1000),
  kind: 4,
  tags: [["p", theirPublicKey]],
  content: encryptedMessage + "?iv=" + ivBase64,
};

console.log(event.content);

How would the receiver be able decrypt this once they receive it?

答案1

得分: 1

如果您不介意使用一个库,您可以使用 nostr-tools

import {nip04} from 'nostr-tools'

nip04.decrypt(key1, key2, message)
英文:

If you don't mind using a library, you can use nostr-tools

import {nip04} from 'nostr-tools'

nip04.decrypt(key1,key2,message)

答案2

得分: 0

Here is the translation of the code portion you provided:

let encryptedMessageParts = event.content.split("?iv=");
let senderMessage_enctrypted = encryptedMessageParts[0];
let iv_ = Buffer.from(encryptedMessageParts[1], "base64");
sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
sharedX = sharedPoint.substr(2, 64);
var decipher = crypto.createDecipheriv(
  "aes-256-cbc",
  Buffer.from(sharedX, "hex"),
  iv_
);
let senderMessage_decrypted = decipher.update(
  senderMessage_enctrypted,
  "base64",
  "utf8"
);
senderMessage_decrypted += decipher.final("utf8");
console.log(senderMessage_decrypted);

Please note that this is a translation of the code, and I won't respond to translation requests. If you have any questions or need further assistance, feel free to ask.

英文:
let encryptedMessageParts = event.content.split("?iv=");
let senderMessage_enctrypted = encryptedMessageParts[0];
let iv_ = Buffer.from(encryptedMessageParts[1], "base64");
sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
sharedX = sharedPoint.substr(2, 64);
var decipher = crypto.createDecipheriv(
  "aes-256-cbc",
  Buffer.from(sharedX, "hex"),
  iv_
);
let senderMessage_decrypted = decipher.update(
  senderMessage_enctrypted,
  "base64",
  "utf8"
);
senderMessage_decrypted += decipher.final("utf8");
console.log(senderMessage_decrypted);

You can read more about how encrypted DM's work here: https://github.com/nostr-protocol/nips/blob/master/04.md

huangapple
  • 本文由 发表于 2023年1月4日 15:00:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75001912.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定