maroodb
maroodb

Reputation: 1118

Asymmetric Encryption using Nodejs Crypto module

I want to use the crypto module of nodejs10, to make an asymmetric encryption.

I find the answer here But when I try to run the code below I got this error:

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

The code:

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)

I didn't find what is the problem, it seems like the problem with the passphrase.

Upvotes: 3

Views: 12127

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30715

Updating a few parameters on the private key generation will make this work:

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)

Upvotes: 4

Related Questions