如何在NodeJ中使用单键加密和解密?

发布于 2025-02-08 00:42:42 字数 933 浏览 0 评论 0原文

我想用一个键加密数据并用多个键解密。我为此使用了加密库。在下面提到的代码使用单键加密,但也用相同的键加密,但我需要用多个键解密。请给我任何建议。我正在使用Typescript的节点JS工作。 代码如下 - >

  const crypto = require("crypto");
  const algorithm = "aes-256-ctr";
  const secretKey = "vOVH6sdmpNWjRRIqCc7rdxs01lwHzfr3";

  encrypt = (text: string) => {

  let iv = crypto.randomBytes(16);
  let cipher = crypto.createCipheriv(algorithm, secretKey, iv);
  let encrypted: any = Buffer.concat([cipher.update(text), 
  cipher.final()]);
  let hash: any = {
  iv: iv.toString("hex"),
  content: encrypted.toString("hex"),
   };
    return hash;
    };

    decrypt = (hash: any) => {
    if (hash.iv) {
    const decipher =     
    crypto.createDecipheriv(algorithm,
    secretKey,Buffer.from(hash?.iv, 
    "hex"));

    const decrpyted = Buffer.concat([
    decipher.update(Buffer.from(hash.content, 
    "hex")),decipher.final(),]);
     return decrpyted.toString();
     }
     };

I want to encrypt data with a single key and decrypt it with multiple keys. I used the crypto library for this. Code used for it mention below that encrypt with single key but decrypt also with the same key but I required to decrypt with multiple keys. Pls, Give me any suggestions to achieve it. I am working in node js with typescript.
code is as below-->

  const crypto = require("crypto");
  const algorithm = "aes-256-ctr";
  const secretKey = "vOVH6sdmpNWjRRIqCc7rdxs01lwHzfr3";

  encrypt = (text: string) => {

  let iv = crypto.randomBytes(16);
  let cipher = crypto.createCipheriv(algorithm, secretKey, iv);
  let encrypted: any = Buffer.concat([cipher.update(text), 
  cipher.final()]);
  let hash: any = {
  iv: iv.toString("hex"),
  content: encrypted.toString("hex"),
   };
    return hash;
    };

    decrypt = (hash: any) => {
    if (hash.iv) {
    const decipher =     
    crypto.createDecipheriv(algorithm,
    secretKey,Buffer.from(hash?.iv, 
    "hex"));

    const decrpyted = Buffer.concat([
    decipher.update(Buffer.from(hash.content, 
    "hex")),decipher.final(),]);
     return decrpyted.toString();
     }
     };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2025-02-15 00:42:42

一个有趣的问题。从我的评论开始,这是一个可能的例子。

您的密钥将是SHA256(“ 13378Adkkqty”)或类似代码。如果需要,哈希字符串可以更长。请注意,数字和字母都按顺序排列,所有字母都是首都。

然后,您通过将其洗牌并添加小写字母和特殊字符来掩盖字符串:“ d3gj/k3ytop71!mams8kbqs”。每个收件人都会获得不同的伪装字符串。当您的代码收到伪装的字符串时,它会剥离特殊字符和小写字母。然后将数字和大写字母分为顺序,从而为哈希提供正确的字符串。琴弦提供了真正的钥匙。

基本上,您对每个人的关键都不同,并确保伪装是可移动的。

An interesting question. Following on from my comment, here is a possible example.

Your key would be SHA256("13378ADKKQTY") or some such. The hashed string can be longer if you want. Note that the digits are in order, as are the letters, and all letters are capitals.

You then disguise the string to be hashed by shuffling it and adding in lowercase letters and special characters: "D3gj/K3YTop71!mAms8KbQs". Each recipient gets a different disguised string. When your code receives the disguised string it strips out the special characters and lowercase letters. Then it sorts the digits and uppercase letters into order, which gives the correct string to hash. Hashing that string gives the real key.

Basically you disguise the key differently for each person, and make sure that the disguises are removable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文