如何使用nodejs通过密码短语加密私钥和公钥?

nnsrf1az  于 2023-01-04  发布在  Node.js
关注(0)|答案(1)|浏览(205)

我想得到一个生成公钥和私钥的函数,然后这些密钥可以用明文加密,在我的例子中,我设法用这些密钥加密了明文,但这与我想达到的目标相反。
这就是我正在做的,但是我想做的正好相反。用明文或密码短语加密密钥。

"use strict";

// Requires NodeJS >= 11.6.0

const crypto = require("crypto"); const util = require("util");

const generateKeyPair = util.promisify(crypto.generateKeyPair);

async function main() {
const exportOptions = {
    format: "pem",
    type: "pkcs1"
};

const { privateKey, publicKey } = await generateKeyPair("rsa", {
    modulusLength: 2048,
});

const plainText = "PRUEBA"; //Texto X a cifrar
const cipherText = crypto.publicEncrypt(publicKey, Buffer.from(plainText)); //Buffer is encrypted by the key

//console.log("Keys: ")
//console.log(publicKey.export(exportOptions)); // Public Key not encrypted
//console.log(privateKey.export(exportOptions)); // Private Key not encrypted

console.log("Plain text:", plainText); //Este es nuestro Pass Phrase
console.log("Cipher text:", cipherText.toString("hex")); 

const decrypted = crypto.privateDecrypt(privateKey, cipherText).toString(); 
console.log("Decrypted:", decrypted);
console.log("Success: ", decrypted === plainText);

}

main();

我就是这么试的。

"use strict";

// Requires NodeJS >= 11.6.0

const crypto = require("crypto"); const util = require("util");

const generateKeyPair = util.promisify(crypto.generateKeyPair);

async function main() {
const exportOptions = {
    format: "pem",
    type: "pkcs1"
};

const { privateKey, publicKey } = await generateKeyPair("rsa", {
    modulusLength: 2048,
});

console.log(publicKey.export(exportOptions)); // Public Key not encrypted
console.log(privateKey.export(exportOptions)); // Private Key not encrypted

const plainText = "PRUEBA"; //Pass phrase 
var clavePublica = publicKey.export(exportOptions);

console.log("Prueba",clavePublica);
const cipherText = crypto.publicEncrypt(plainText, clavePublica); //Encrypt "clavePublica" with plainText
//const cipherText = crypto.publicEncrypt(Buffer.from(plainText),Buffer.from(publicKey) );
//console.log("Keys: ")
//console.log("Plain text:", plainText); //Este es nuestro Pass Phrase
//console.log("Cipher text:", cipherText.toString("hex")); //Texto X cifrado

//const plainText2 = "PRUEBA2";
//const decrypted = crypto.privateDecrypt(privateKey, cipherText).toString(); //Texto X descifrado
//console.log("Decrypted:", decrypted);
//console.log("Success: ", decrypted === plainText);
}

main();

这就是结果:

-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAwCs5V1/qCqdLxgh/LBfhkLr4HF0Z7s9Bq2//fq4YEQ0lNgouYHfg
wEohDjTInJ/l7kOVkxoDknopJrc7Jac3in5y37o5yO7bThhg8/tpoZL/KT6FoDXt
0PH+o7qq2aSgYU/qJkfLl5DTViwHBDbE/A9byr9frV7+CWDA7i4nMe3692FA+xYv
nRbJ1OinEaDIts7YMJHfJJxHb0SNSrohof1Zqx5DOxs9T5JHHiC8XwSTfebilR+i
LVQPwevX9/ZLzU7FYbMSBiqYbaUp/VQ/4SBd2IE1naKTd/8mWgUPqn1DoOjC907T
h0zfkif+/RSR7lgtZ9t5IjlBoBOrOTI82QIDAQAB
-----END RSA PUBLIC KEY-----

-----BEGIN RSA PRIVATE KEY-----
MIIEogIBAAKCAQEAwCs5V1/qCqdLxgh/LBfhkLr4HF0Z7s9Bq2//fq4YEQ0lNgou
YHfgwEohDjTInJ/l7kOVkxoDknopJrc7Jac3in5y37o5yO7bThhg8/tpoZL/KT6F
oDXt0PH+o7qq2aSgYU/qJkfLl5DTViwHBDbE/A9byr9frV7+CWDA7i4nMe3692FA
+xYvnRbJ1OinEaDIts7YMJHfJJxHb0SNSrohof1Zqx5DOxs9T5JHHiC8XwSTfebi
lR+iLVQPwevX9/ZLzU7FYbMSBiqYbaUp/VQ/4SBd2IE1naKTd/8mWgUPqn1DoOjC
907Th0zfkif+/RSR7lgtZ9t5IjlBoBOrOTI82QIDAQABAoIBABr0NR0coUQ7RkqR
mIeDwKoUlNvNnJHoKirfZl52EX7FcZRh2zLBaG8wNptlK0T6EeiDPEX0ySQmKa16
LTv7zLOKnEGQlQL/qxe0yFc7IAwqBSDbhHyonncclP3myd4F1E+x7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hAGa6+MOfgSpyaU5eBIZl+tkD/F4w1yQ1LVU1yjGMfFo=
-----END RSA PRIVATE KEY-----

node:internal/crypto/cipher:79
    return method(data, format, type, passphrase, buffer, padding, oaepHash,
           ^

Error: error:1E08010C:DECODER routines::unsupported
    at Object.publicEncrypt (node:internal/crypto/cipher:79:12)
    at main (C:\Users\mmorena\Oracle Content - Accounts\Oracle Content\Escritorio\Comu BC\PubyPriv.js:28:31) {
  library: 'DECODER routines',
  reason: 'unsupported',
  code: 'ERR_OSSL_UNSUPPORTED'
}

Node.js v18.12.0
cyej8jka

cyej8jka1#

您只需要在选项对象中添加“passphrase”键。
下面是一些代码(假设您已经导入了密钥和节点加密):

const publicKey = ... // Public key
const privateKey = ... //Private key
const options = {
    type:"pkcs1",
    format:"pem",
    passphrase:"YOUR PASSPHRASE"
}
const exportedPublic = publicKey.export(options);
const exportedPrivate = privateKey.export(options);

相关问题