I am using MongoDB client-side field level encryption to encrypt and decrypt data. However, I noticed that every time I execute the program, the primitive binary key changes. This makes it difficult for me to retrieve previously encrypted data because I cannot decrypt it using the new key.
Is there a way to maintain a consistent encryption key for client-side field level encryption in MongoDB? If so, how can I do it?
https://go.dev/play/p/6W8e0OiPV2L
I'm trying to implement client-side field level encryption in my MongoDB Community project to encrypt certain fields in my documents before storing them in the database, and then decrypt them when I retrieve them from the database.
I've been following the MongoDB documentation and was able to successfully encrypt and decrypt the data during the first execution of my program. However, I noticed that the key keeps changing every time I run the program again, and I want to keep the key stationary.
These lines should be removed:
And the following lines are needed only when the key does not exist yet:
Maybe do a
clientEnc.GetKeyByAltNamecall first to check whether the key exists.By saying "the primitive binary key changes", I think you meant the Data Encryption Key (DEK) changes. That's caused by the dropping of the collection that stores the DEK.
See Keys and Key Vaults: