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:
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.Why am I not able to decrypt the file? What Am I missing?