0

I am encrypting some text with the following encrypt function on Heroku:

const crypto = require('crypto');

// function to encrypt data ....
function encrypt(KEY, text){
  const cipher = crypto.createCipher('aes192', KEY);
  var encrypted = cipher.update(text,'utf8', 'hex');
  encrypted += cipher.final('hex');
  return encrypted;
}

// function to decryt data..............
function decrypt(KEY, text){ 
    const decipher = crypto.createDecipher('aes192', KEY) 
    var decrypted = decipher.update(text,'hex','utf8') 
    decrypted += decipher.final('utf8'); 
    return decrypted; 
 }

It then saves the text I encrypted to MongoDb server. I read the encrypted value and try to decrypt it on the local machine, but get a digital envelope routines:EVP_DecryptFinal_ex:bad decrypt. I have spent so much time trying to figure out what is the issue.

Both in Heroku and locally I am using the same key. If I try this code locally (i.e. encrypt and decrypt locally), then everything works as expected.

Do you have an idea of what might be going wrong?

I have noticed that heroku server is in United States, while I am in UK. Does the timezone play any role in here?

Naz
  • 2,012
  • 1
  • 21
  • 45
  • there are lot of factors that are mentioned in this ticket https://stackoverflow.com/questions/34304570/how-to-resolve-the-evp-decryptfinal-ex-bad-decrypt-during-file-decryption – karthick Jun 03 '19 at 18:07
  • Possible duplicate of [How to resolve the "EVP\_DecryptFInal\_ex: bad decrypt" during file decryption](https://stackoverflow.com/questions/34304570/how-to-resolve-the-evp-decryptfinal-ex-bad-decrypt-during-file-decryption) – karthick Jun 03 '19 at 18:07
  • thanks for this. my local openssl is 1.1.1b. checking the heroku version – Naz Jun 03 '19 at 18:12
  • yup. it is older. thank you! – Naz Jun 03 '19 at 18:16

0 Answers0