1

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.

enter image description here

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==

5
  • It is just a text file that says "test". So pretty small. Commented May 9, 2019 at 6:39
  • Shouldn't you be encrypting the data with public key and decrypting the data with private key? Right now you are using the key pair to do that. I don't know the Node js api but in most languages you do like publicKey.encrypt() and then decrypt using the private key. Commented May 9, 2019 at 7:30
  • Please check the tutorial I posted by Medium and you will see the Google Engineer, author does it this way. I posted a comment as well asking why he did not use the pair in his example. Commented May 9, 2019 at 19:42
  • No you do not need to specifically use the public key to perform encryption, that's implied. Encryption using the private key is called a signature. Commented May 9, 2019 at 21:11
  • Not sure why he created a public and private key pair with out using them. Seems like half a tutorial. Where is part 2? Commented May 9, 2019 at 21:30

2 Answers 2

1

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'));
Sign up to request clarification or add additional context in comments.

Comments

-1

You are trying to decrypt the clear text file .. Try decrypt on the encrypted file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.