英文:
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);
接收者如何解密接收到的消息呢?解密过程如下:
-
接收者从事件对象中提取
content
字段,其中包含加密的消息和初始化向量(iv)。 -
接收者需要知道自己的私钥以及发送者的公钥(在
tags
中的theirPublicKey
字段)。这两者是解密所必需的信息。 -
使用接收者的私钥和发送者的公钥来生成共享的密钥。
-
从
content
字段中提取加密的消息和初始化向量(iv)。 -
使用共享的密钥和初始化向量(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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论