如何解密Nostr消息?

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

How do I decrypt a Nostr message?

问题

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

  1. const crypto = require("crypto");
  2. const secp = require("noble-secp256k1");
  3. const ourPrivateKey = ""; // 我们的私钥
  4. const ourPubKey = ""; // 我们的公钥
  5. const theirPublicKey = ""; // 对方的公钥
  6. const text = "Hello World"; // 要发送的文本消息
  7. let sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
  8. let sharedX = sharedPoint.substr(2, 64);
  9. let iv = crypto.randomFillSync(new Uint8Array(16));
  10. var cipher = crypto.createCipheriv(
  11. "aes-256-cbc",
  12. Buffer.from(sharedX, "hex"),
  13. iv
  14. );
  15. let encryptedMessage = cipher.update(text, "utf8", "base64");
  16. encryptedMessage += cipher.final("base64");
  17. let ivBase64 = Buffer.from(iv.buffer).toString("base64");
  18. let event = {
  19. pubkey: ourPubKey,
  20. created_at: Math.floor(Date.now() / 1000),
  21. kind: 4,
  22. tags: [["p", theirPublicKey]],
  23. content: encryptedMessage + "?iv=" + ivBase64,
  24. };
  25. 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:

  1. const crypto = require("crypto");
  2. const secp = require("noble-secp256k1");
  3. const ourPrivateKey = "";
  4. const ourPubKey = "";
  5. const theirPublicKey = "";
  6. const text = "Hello World";
  7. let sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
  8. let sharedX = sharedPoint.substr(2, 64);
  9. let iv = crypto.randomFillSync(new Uint8Array(16));
  10. var cipher = crypto.createCipheriv(
  11. "aes-256-cbc",
  12. Buffer.from(sharedX, "hex"),
  13. iv
  14. );
  15. let encryptedMessage = cipher.update(text, "utf8", "base64");
  16. encryptedMessage += cipher.final("base64");
  17. let ivBase64 = Buffer.from(iv.buffer).toString("base64");
  18. let event = {
  19. pubkey: ourPubKey,
  20. created_at: Math.floor(Date.now() / 1000),
  21. kind: 4,
  22. tags: [["p", theirPublicKey]],
  23. content: encryptedMessage + "?iv=" + ivBase64,
  24. };
  25. console.log(event.content);

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

答案1

得分: 1

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

  1. import {nip04} from 'nostr-tools'
  2. nip04.decrypt(key1, key2, message)
英文:

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

  1. import {nip04} from 'nostr-tools'
  2. nip04.decrypt(key1,key2,message)

答案2

得分: 0

Here is the translation of the code portion you provided:

  1. let encryptedMessageParts = event.content.split("?iv=");
  2. let senderMessage_enctrypted = encryptedMessageParts[0];
  3. let iv_ = Buffer.from(encryptedMessageParts[1], "base64");
  4. sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
  5. sharedX = sharedPoint.substr(2, 64);
  6. var decipher = crypto.createDecipheriv(
  7. "aes-256-cbc",
  8. Buffer.from(sharedX, "hex"),
  9. iv_
  10. );
  11. let senderMessage_decrypted = decipher.update(
  12. senderMessage_enctrypted,
  13. "base64",
  14. "utf8"
  15. );
  16. senderMessage_decrypted += decipher.final("utf8");
  17. 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.

英文:
  1. let encryptedMessageParts = event.content.split("?iv=");
  2. let senderMessage_enctrypted = encryptedMessageParts[0];
  3. let iv_ = Buffer.from(encryptedMessageParts[1], "base64");
  4. sharedPoint = secp.getSharedSecret(ourPrivateKey, "02" + theirPublicKey);
  5. sharedX = sharedPoint.substr(2, 64);
  6. var decipher = crypto.createDecipheriv(
  7. "aes-256-cbc",
  8. Buffer.from(sharedX, "hex"),
  9. iv_
  10. );
  11. let senderMessage_decrypted = decipher.update(
  12. senderMessage_enctrypted,
  13. "base64",
  14. "utf8"
  15. );
  16. senderMessage_decrypted += decipher.final("utf8");
  17. 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:

确定