0

on Node 6.3.1.

i'm trying to get node to decipher a file that was originally encrypted via openssl

here is how the file "blah.txt" was encrypted. the last part is just to confirm that we are able to decrypt using the private key.

# ---------------
# Prep
#
# create private key
openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048

# generate public key from our private key
openssl rsa -pubout -in private_key.pem -out public_key.pem

# create random key
openssl rand -base64 128 -out key.bin

# ---------------
# Encryption
#
# encrypt data using random key
openssl enc -aes-256-cbc -salt -in blah.txt -out blah.txt.enc -pass file:./key.bin

# encrypt random key using public key
openssl pkeyutl -encrypt -in key.bin -pubin -inkey public_key.pem -out key.bin.enc

# ---------------
# DECRYPTION
#
# decrypt random key using private key
openssl pkeyutl -decrypt -inkey private_key.pem -in key.bin.enc -out key.bin.decrypted

# decrypt data using decrypted random key
openssl enc -d -aes-256-cbc -in blah.txt.enc -out decrypted.txt -pass file:./key.bin.decrypted

Then I have a simple form that submits the files to a node server. there is a helper function that is intended to use private key file to decrypt the encrypted random key which in turn is used to decrypt the file.

I can confirm that the random key file is decrypted as expected by the private key. My problem is that I'm unabel to successfully decrypt the encrypted file using the key. I either get an error (see question 1 below) or I get gibberish in the file.

helper function code in node:

var decryptFileWithRsaPrivateKey = function(relativeOrAbsolutePathToEncryptedFile, relativeOrAbsolutePathToEncryptedKey, relativeOrAbsolutePathToPrivateKey) {
    var absolutePathToPrivateKey= path.resolve(relativeOrAbsolutePathToPrivateKey);
    var privateKey = fs.readFileSync(absolutePathToPrivateKey, "utf8");
    var absolutePathToEncryptedKey = path.resolve(relativeOrAbsolutePathToEncryptedKey);

    var encryptedKeyBuffer = fs.readFileSync(absolutePathToEncryptedKey);
    var options = {
        key: privateKey,
        //padding: crypto.constants.RSA_NO_PADDING
        padding: crypto.constants.RSA_PKCS1_PADDING
        //padding: crypto.constants.RSA_PKCS1_OAEP_PADDING
    }
    var decryptedKey = crypto.privateDecrypt(options, encryptedKeyBuffer).toString('binary');
    //decryptedKey = 'abcdef'
    //var decryptedKey = fs.readFileSync(absolutePathToEncryptedKey).toString('binary');
    logger.info('decryptedKey', util.inspect(decryptedKey));  

    var absolutePathToEncryptedFile = path.resolve(relativeOrAbsolutePathToEncryptedFile);
    var encryptedFileBuffer = fs.readFileSync(absolutePathToEncryptedFile);

    // one of openssl list-cipher-algorithms
    var algorithm = 'AES-256-CBC';
    var decipher = crypto.createDecipher(algorithm, decryptedKey);
    decipher.setAutoPadding(false)

    logger.info('type of', util.inspect(encryptedFileBuffer))

    var decryptedBuffer = decipher.update(encryptedFileBuffer);
    decryptedBuffer += decipher.final();

    logger.info('horay: ', util.inspect(decryptedBuffer));
    return decryptedBuffer;
};

Questions:

  1. Do I want to use decipher.setAutoPadding(false) ? If I don't set this I constantly get the error digital envelope routines:EVP_DecryptFinal_ex:bad decrypt similar to what is described here: https://github.com/nodejs/node/issues/2794
    If I set this, I don't get the errors but the file is gibberish. I can set the decryptedKey to anything and it will still decipher and return gibberish.

  2. Why am I not able to decrypt the file? What Am I missing?

w--
  • 6,427
  • 12
  • 54
  • 92
  • 2
    Possibly related: [What's wrong with nodejs crypto decipher?](http://stackoverflow.com/q/12219499/608639). You might also be interested in the discussion of `EVP_BytesToKey` at [Node.js v6.8.0 Crypto Documentation](https://nodejs.org/api/crypto.html). – jww Oct 14 '16 at 22:03
  • thanks the link to that SO question. I suspect that is exactly what is happening. will attempt to verify later. The second link you mention is a discussion but just points to the Node docs. Was that intentional or a typo? – w-- Oct 15 '16 at 02:09

0 Answers0