A web application I'm developing needs to store some sensitive data in a database. I need to be able to retrieve and use this data later on so hashing isn't enough.

The web application might, at some point in the future, be load-balanced, so I'm trying to get things done correctly now instead of battling this later.

I've created a two-step process for this using standard technologies:

  • The sensitive data is encrypted using AES in the SQL database.
  • To make sure the AES key/IV values are as secure as possible, they are encrypted by the IIS web application using DPAPI (user scope) and stored in a file in App_Data.

This is because I don't think I can make use of the DPAPI alone if load-balancing will be in use - am I correct?

I'm wondering if I'm not overcomplicating things with the second step, but I don't like the idea of keeping the AES key in plain-text in some web.config file in the IIS application.

Is this approach OK and relatively secure?


The ideal for a situation like this is to use an HSM, but baring that as an option, using the software equivalent in DPAPI is probably your next best bet. I also don't see an issue with storing each server's copy of the key encrypted with the DPAPI key for that server/service user.

You certainly do not want to simply store the key, unprotected, in the file system. Almost any compromise at all of your server would leak it without further protection.

One possible enhancement, though it would require a bit of programming to retrieve, is to store the key itself in DPAPI. It's only a small level of extra protection, since the key is still stored (in encrypted form) on the system, but it makes it marginally harder to find and may make it slightly easier to access from within your program. Without looking in to it in depth, I think that is probably what I would do.

  • Sorry, I'm not sure I understand the last paragraph. I wrote that the AES key and IV values are already encrypted using DPAPI on the host machine(s). What exactly is it you're suggesting?
    – Shaamaan
    Jan 9 '14 at 13:21
  • @Shaamaan - there are two ways you could encrypt the key. You could encrypt it with a master key stored in DPAPI and then store it on the file system or you could give the key to DPAPI and have it store it for you. I would slightly prefer the latter unless it adds significant extra complexity, simply because it may provide slightly better protection of the key. It shouldn't matter if the encrypted version of the key is leaked, but still a little safer to try to keep it out of the hands of an attacker all together, all other things being equal. Jan 9 '14 at 13:23
  • OK, thanks. I'll look into that. I'm using the ProtectedData class in C# as a wrapper around DPAPI and that class doesn't have that kind of functionality (it's used to encrypt / decrypt data - what I do with that data is up to me). If there's a nifty way to have DPAPI store the data on it's own, then I'm all for it. :)
    – Shaamaan
    Jan 9 '14 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.