gpt4 book ai didi

node.js - 使用 Nodejs Crypto 模块的非对称加密

转载 作者:太空宇宙 更新时间:2023-11-03 22:06:05 26 4
gpt4 key购买 nike

我想使用nodejs10的crypto模块,进行非对称加密。

我找到答案here但是当我尝试运行下面的代码时出现此错误:

return method(toBuf(key), buffer, padding, passphrase);
^
Error: error:0608B096:digital envelope routines:EVP_PKEY_encrypt_init:operation not supported for this keytype.

代码:

var crypto = require("crypto");
var path = require("path");
var fs = require("fs");
const passphrase = "mySecret"

var encryptStringWithRsaPublicKey = function(toEncrypt, relativeOrAbsolutePathToPublicKey) {
var absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
var publicKey = fs.readFileSync(absolutePath, "utf8");
var buffer = new Buffer(toEncrypt);
var encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString("base64");
};

var decryptStringWithRsaPrivateKey = function(toDecrypt, relativeOrAbsolutePathtoPrivateKey) {
var absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
var privateKey = fs.readFileSync(absolutePath, "utf8");
var buffer = new Buffer(toDecrypt, "base64");
//var decrypted = crypto.privateDecrypt(privateKey, buffer);
const decrypted = crypto.privateDecrypt(
{
key: privateKey.toString(),
passphrase: passphrase,
},
buffer,
)
return decrypted.toString("utf8");
};

const { writeFileSync } = require('fs')
const { generateKeyPairSync } = require('crypto')

function generateKeys() {
const { privateKey, publicKey } = generateKeyPairSync('ec', {
namedCurve: 'secp256k1',
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: passphrase,
},
})

writeFileSync('private.pem', privateKey)
writeFileSync('public.pem', publicKey)
}

generateKeys();

let a = encryptStringWithRsaPublicKey("hello", "public.pem")
let b = decryptStringWithRsaPrivateKey(a, "private.pem");
console.log(b)

我没找到问题所在,看来是密码的问题。

最佳答案

更新私钥生成的一些参数将使此工作正常进行:

var crypto = require("crypto");
var path = require("path");
var fs = require("fs");
const passphrase = "mySecret"

var encryptStringWithRsaPublicKey = function(toEncrypt, relativeOrAbsolutePathToPublicKey) {
var absolutePath = path.resolve(relativeOrAbsolutePathToPublicKey);
var publicKey = fs.readFileSync(absolutePath, "utf8");
var buffer = Buffer.from(toEncrypt);
var encrypted = crypto.publicEncrypt(publicKey, buffer);
return encrypted.toString("base64");
};

var decryptStringWithRsaPrivateKey = function(toDecrypt, relativeOrAbsolutePathtoPrivateKey) {
var absolutePath = path.resolve(relativeOrAbsolutePathtoPrivateKey);
var privateKey = fs.readFileSync(absolutePath, "utf8");
var buffer = Buffer.from(toDecrypt, "base64");
const decrypted = crypto.privateDecrypt(
{
key: privateKey.toString(),
passphrase: passphrase,
},
buffer,
)
return decrypted.toString("utf8");
};

const { writeFileSync } = require('fs')
const { generateKeyPairSync } = require('crypto')

function generateKeys() {
const { publicKey, privateKey } = generateKeyPairSync('rsa',
{
modulusLength: 4096,
namedCurve: 'secp256k1',
publicKeyEncoding: {
type: 'spki',
format: 'pem'
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: passphrase
}
});

writeFileSync('private.pem', privateKey)
writeFileSync('public.pem', publicKey)
}

generateKeys();

let a = encryptStringWithRsaPublicKey("hello", "public.pem")
let b = decryptStringWithRsaPrivateKey(a, "private.pem");
console.log(b)

关于node.js - 使用 Nodejs Crypto 模块的非对称加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087514/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com