wheresvic / mongoose-field-encryption

A simple symmetric encryption plugin for individual fields. Dependency free, only mongoose peer dependency.
MIT License
74 stars 32 forks source link

Querying gives decrypted data but calling fieldEncryption.decrypt causes error - error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt #52

Closed amitistyping closed 3 years ago

amitistyping commented 3 years ago

Steps to Reproduce:

  1. Encrypt this way - mySchema.plugin(mongooseFieldEncryption, { fields: ["message", "sender", "receiver"], secret: "AsEAabUmaIK3C8FTpuzPHrZJhvhiS8zY" });
  2. Decrypt this way - fieldEncryption.decrypt(messageObj.messageData.receiver, 'AsEAabUmaIK3C8FTpuzPHrZJhvhiS8zY')
  3. Causes below error -

image image

However, when I used find queries with Mongoose, I get decrypted data just fine.

amitistyping commented 3 years ago

Also, I was getting invalid key error earlier when using fieldEncryption.decrypt - so I had to change my key length to 32 characters as was suggested somewhere on StackOverFlow.

wheresvic commented 3 years ago

Hi @lazzyn ,

You need to make sure that messageObj.messageData.receiver is actually the encrypted value. Can you print out that field before calling the function to decrypt?

Finally note that if the encrypted field is an object the data is put under __enc_fieldname_d (replace fieldName with the actual fieldname).

wheresvic commented 3 years ago

Closing now.