Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Node.js crypto生成多对公钥/私钥_Node.js_Cryptography - Fatal编程技术网

使用Node.js crypto生成多对公钥/私钥

使用Node.js crypto生成多对公钥/私钥,node.js,cryptography,Node.js,Cryptography,如何在Node.js中使用crypto生成多对密钥 在node.js中,有以下代码用于生成对。但是如果我将名称更改为{key,public_key}然后尝试console.log它们,它将打印未定义的。但是,我需要两对,不能用相同的名称运行它两次,或者它告诉我它们是以前定义的。有没有其他方法可以使用加密生成另一对 const { publicKey, privateKey } = generateKeyPairSync('rsa', { modulusLength: 4096, publ

如何在Node.js中使用crypto生成多对密钥

在node.js中,有以下代码用于生成对。但是如果我将名称更改为
{key,public_key}
然后尝试
console.log
它们,它将打印
未定义的
。但是,我需要两对,不能用相同的名称运行它两次,或者它告诉我它们是以前定义的。有没有其他方法可以使用加密生成另一对

const { publicKey, privateKey } = generateKeyPairSync('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret'
  }
});
关键字:未定义 发布密钥:未定义

或:


您不能在一行中使用两个接收器函数,因为我们必须等待这两个函数都出现,然后再打印。因此,最好使用回调函数来获取答案并加入外部变量

let public1, public2;
let private1, private2;

const { generateKeyPair } = require('crypto');
generateKeyPair('rsa', {
    modulusLength: 4096,
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem'
    },
    privateKeyEncoding: {
        type: 'pkcs8',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: 'top secret'
    }
}, (err, publicKey, privateKey) => {
    public1 = publicKey;
    private1 = privateKey;
    generateKeyPair('rsa', {
        modulusLength: 4096,
        publicKeyEncoding: {
            type: 'spki',
            format: 'pem'
        },
        privateKeyEncoding: {
            type: 'pkcs8',
            format: 'pem',
            cipher: 'aes-256-cbc',
            passphrase: 'top secret'
        }
    }, (err, publicKey1, privateKey1) => {
        public2 = publicKey1;
        private2 = privateKey1;
    });
});
console.log("public1 : ", public1)
console.log("private1 : ", private1)
console.log("public2 : ", public2)
console.log("private2 : ", private2)

在销毁分配过程中,您可以按如下方式将
publicKey、privateKey
属性分配给:

const { generateKeyPairSync } = require('crypto');

const keyOptions = [{
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret 1'
  }
}, {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret 2'
  }
}]

const [
  { publicKey: publicKey1, privateKey: privateKey1 },
  { publicKey: publicKey2, privateKey: privateKey2 }
] = keyOptions.map(options => generateKeyPairSync('rsa', options))

console.log(
  publicKey1,
  privateKey1,
  publicKey2,
  privateKey2
)
const { generateKeyPairSync } = require('crypto');

const keyOptions = [{
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret 1'
  }
}, {
  modulusLength: 2048,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem'
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret 2'
  }
}]

const [
  { publicKey: publicKey1, privateKey: privateKey1 },
  { publicKey: publicKey2, privateKey: privateKey2 }
] = keyOptions.map(options => generateKeyPairSync('rsa', options))

console.log(
  publicKey1,
  privateKey1,
  publicKey2,
  privateKey2
)