I'm developing an API which requires an API key to use. These keys are assigned to users. To figure out which user the key belongs to, I have to store some information in that key. But I don't want it to be seen by "others". So I want to encrypt that value.

  • I was first thinking about making an encryption key for each user, but then I figured if the database has been breached, they have the key for each user.
  • So instead, I thought I should make a server-side encryption key. But I realised, if they breached the database, they probably don't care much about that encryption key anymore.

And then I started asking myself the question: What can I do to protect the data stored in a database, even if it has been breached?

For example, does it make sense to encrypt the values inside the database? I would assume not due to the time needed to decrypt everything again which would make for a bad user experience. So what options do I have?

  • Thanks @mentallurg, that's actually a great idea and easy to implement even for a "newb" like me. Thanks. May 14, 2020 at 22:16

1 Answer 1


Firstly please take a look at the suggested answer How to encypt sensitive data in database of a web app? (as per the helpful comment by @mentallurg

From a defence in depth approach, it absolutely makes sense to encrypt values on the database, as well it is worth taking measures to limit and contain breaches, such as ensuring your application is resilient to SQL injection, its DB connection is done with an unprivileged account (ideally multiple depending on the role and task) that can access only a subset of data on the database. Then the database server should be segregated from the remaining network. The database service should be running under unprivileged user permissions and have system command execution disabled.

As to how you should manage encryption and keys, there's several good solutions (as per the recommended question/answer), but be very very careful, as there's many times more bad/ineffective/insecure solutions.

  • I'm not an expert in databases I'm afraight. Do you maybe have a source or smth which highlights in more detail, what set(s) of permissions are appropriate in the scenario you described above? MySQL, in case you need to know. May 14, 2020 at 22:20
  • To it absolutely makes sense to encrypt values on the database - I don't agree with that. How will you implement search by values, e.g. how will you find products with price between P1 and P2? Or how will you calculate sums of some field values? How will you check if one value is greater that the other? How will you index values? FHE can be used only in a very limited cases. That's why encrypting the values is in my opinion a bad approach.
    – mentallurg
    May 15, 2020 at 1:14
  • @mentallurg I mean, you only encrypt user data or similar stuff. Product prices, names or other meta is public anyway, so why would you encrypt these infos? May 15, 2020 at 12:23
  • @mentallurg please not I didn't say it makes sense to encrypt all data on the database. naturally data you want to search through cannot be encrypted, although not all fields are meant to be searched through/indexed. Only the application architects can judge what must, can and can't be encrypted.
    – Pedro
    May 15, 2020 at 16:45

Not the answer you're looking for? Browse other questions tagged .