简体   繁体   中英

How do I decrypt a file using node-rsa with public and private keys?

I was following this tutorial on medium . However the tutorial does not explain how to decrypt the encrypted file. Nor does it explain how the public / private keys are used.

Simply running decrypt does not work with the error below.

在此处输入图片说明

Relevant documentation appears to be on npmjs.com .

Actual Code

// node modules
const fs = require('fs');

// npm modules
const parseArgs = require('minimist');
const NodeRSA = require('node-rsa');

// get command line arguments
const argv = parseArgs(process.argv.slice(2));
// console.dir(argv);

// read a file
const file = fs.readFileSync('' + argv.file, 'utf8');

// generate keys
const key = new NodeRSA().generateKeyPair();
const publicKey = key.exportKey('pkcs8-public-pem');
const privateKey = key.exportKey('pkcs1-pem');

// write public key
fs.openSync('keys/public.pem', 'w');
fs.writeFileSync('keys/public.pem', publicKey, 'utf8');

// write private key
fs.openSync('keys/private.pem', 'w');
fs.writeFileSync('keys/private.pem', privateKey, 'utf8');

// write encrypted file
const encrypted = key.encrypt(file, 'base64');
fs.openSync('encrypted.txt', 'w');
fs.writeFileSync('encrypted.txt', encrypted, 'utf8');

// read encrypted file
const fileEncrypted = fs.readFileSync('encrypted.txt', 'utf8');
const decrypted = key.decrypt(fileEncrypted, 'base64'); // error here
// has to do with key generation?

default.txt

test

encrypted.txt

qLs0dUez+LzhlNBGnvEzLRdYF0HUHoRignMxT2MZO7Qs8tMvkmWbiA1oxJbT7ZC5bPS+dHvFgbiWbdje/3/Y17JT8JxflryJU6394UPfsTDLmtZZroemTtTzJxVnGZlw0IyQtfn79eysQaEoKMQ9hKjDySxO1gLwJJZ1DoxW7CNu0BqfcGUMcREQ+ozrhKpMRK0piWUWqHYwX0EIxQT8/rh5ER+tCdh4lR7N5+FPA4VOde3z/36DvQ9KOMChS7m91aH0QXUqhMaHtjslvcoj4i1Rwzd0qn1imHPc8LncZz6hv2deRqU65rS+M6UeC9LWJjblVf2er25x8B1yszaV+A==

You are not decrypting the data correctly, the encoding argument of NodeRSA.prototype.decrypt(buffer, encoding) is the desired result encoding, not the input encoding.

Although the decrypt method is said to take a buffer via the JSDoc:

/**
 * Decrypting data method with private key
 *
 * @param buffer {Buffer} - buffer for decrypting
 * @param encoding - encoding for result string, can also take 'json' or 'buffer' for the automatic conversion of this type
 * @returns {Buffer|object|string}
 */

You can trace the code to see that if buffer is a string it is assumed to be in base64 encoding:

buffer = _.isString(buffer) ? Buffer.from(buffer, 'base64') : buffer;

We can simplify your problem down by ignoring the file system interactions to see how a round trip works:

const key = new NodeRSA().generateKeyPair();

// Encrypt the utf8 encoded input string and output a base64 encoded string
const encrypted = key.encrypt('test', 'base64');
console.log(encrypted);

// Decrypt the base64 encoded input string and output a utf8 encoded string
const decrypted = key.decrypt(encrypted, 'utf8');
console.log(decrypted);

Alternatively you could explicitly convert the input and output to and from buffers respectively, this may help you understand what is happening under the hood:

const key = new NodeRSA().generateKeyPair();

const input = Buffer.from('test', 'utf8');

const encrypted = key.encrypt(input);
console.log(encrypted.toString('base64'));

const decrypted = key.decrypt(encrypted);
console.log(decrypted.toString('utf8'));

您正在尝试解密明文文件.. 尝试对加密文件进行解密。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM