Is there some sort of secure local storage on Windows?

20,252

Solution 1

is there a way, to encrypt information on windows, and have windows securely manage the passwords?

CryptProtectData: http://msdn.microsoft.com/en-us/library/windows/desktop/aa380261(v=vs.85).aspx

Using from .NET: http://msdn.microsoft.com/en-us/library/aa302402.aspx

Historically, Protected Storage (available in XP, read-only in vista+): http://msdn.microsoft.com/en-us/library/bb432403%28VS.85%29.aspx

Solution 2

You should consider using DPAPI for this purpose. It will encrypt your data with a special (internal) symmetric key which is on per-user basis. You don't even need to ask for passwords in this case, because different users on the system will have different keys assigned to them.

The downside of it might be that you can't recover the data if the user is deleted/Windows reinstalled (I believe that this is the case, not quite sure though). In that case encrypt the data with a "self-generated" key derived from the password and store the password in registry/file encrypted using DPAPI.

Solution 3

You can use the native encryption facility. Set the encrypt attribute on your folder or file (from the property page, click on the "advanced" button). Then you can set the users that can access the file (by default this only includes the file creator). The big advantage of this solution is that it is totally transparent from the application and the users points of view.

To do it programmatically: using the Win32 API, call EncryptFile() on the directory where you want to store your sensitive per-user data. From now on all newly created files within this dir will be encrypted and only readable by their creator (that would be the current user of your app). Alternatively you can use the FILE_ATTRIBUTE_ENCRYPTED flag on individual files at creation time. You can check encryption info from the explorer on the file's property page, and see that app-created files are correctly encrypted and restricted to their respective users. There is no password to store or use, everything is transparent.

If you want to hide data from all users then you can create a special app-specific user and impersonate it from your app. This, along with ACLs, is the blessed technique on Windows for system services.

Solution 4

You might want to look at Isolated Storage, which is a way of storing settings and other data on a per-application data automatically. See an example and MSDN.

This is an alternative to storing normal settings in the registry, a better one in a lot of cases... I'm not sure how the data is stored to file however so you'd need to check, you wouldn't want it to be accessible, even encrypted, to other users. From memory only the app. that created the storage can open it - but that needs checking.

Edit:

From memory when I last used this, a good approach is to write a "Setting" class which handles all the settings etc. in your app. This class then has the equivalent of Serialize and DeSerialize methods which allow it to write all its data to an IsolatedStorage file, or load them back again.

The extra advantage of implementing it in this way is you can use attributes to mark up bits of the source and can then use a Property Grid to quickly give you user-edit control of settings (the Property Grid manipulates class properties at runtime using reflection).

Share:
20,252
Paulius
Author by

Paulius

I'm a python developer, currently working on google-app-engine based project.

Updated on July 09, 2022

Comments

  • Paulius
    Paulius almost 2 years

    I was thinking of making a small tool. It is not important what the tool will do. The important thing, is that the tool will need to store some sensitive information on the user's HDD. EDIT: The information that will be stored is USER'S information - I'm not trying to protect my own content, that I distribute with the app.

    I understand that I need to encrypt this information. But then, where do I safely store the encryption password? It's some sort of an infinite recursion...

    So, is there a way, to encrypt information on windows, and have windows securely manage the passwords? When I say windows I mean Windows XP SP2 or later.

    I should also note, that users on the same system must not have access to other users information (even when they are both running my application).

    I'm looking for both - .NET 2.0 (C#) and native (C/C++) solutions to this problem.

  • Paulius
    Paulius over 15 years
    Can I set it from code? Like from .NET C# or native C++ application?
  • Paulius
    Paulius over 15 years
    Well, the information must be entered by the user first, and then my app would need to securely store it. So, if I can create such file from inside my app - it could work.
  • Paulius
    Paulius over 15 years
    That's the point - I don't want to even ask the user for password. The point of my app, is that if the user already logged in into Windows - then he is who he says he is. So this wouldn't work for me.
  • Pavel Hájek
    Pavel Hájek over 15 years
    The compression flag is a standard file attribute, so it can be set programmatically with e.g. CreateFile (use the flag FILE_ATTRIBUTE_ENCRYPTED). So you can create a dir with this flag set, then add your files there and the system takes care of the rest.
  • liggett78
    liggett78 over 15 years
    If the user is already logged in then why do you bother using a password in the first place?
  • liggett78
    liggett78 over 15 years
    Link to the docs for .NET 2.0 and later: msdn.microsoft.com/en-us/library/…
  • Paulius
    Paulius over 15 years
    Because the information that will be stored in an external file (or whatever) must not be accessible from another user (if someone else logs into his own windows account).
  • Paulius
    Paulius over 15 years
    What's S.O.L.? Anyway, I'm not trying to achieve the same thing as DRM. I'm not trying to encrypt MY files, that I would later distribute with the app - I need a way to encrypt USER information, that HE/SHE will enter into my APP (the info needs to be stored for later use from inside my app).
  • Paulius
    Paulius over 15 years
    Well, while the data is sensitive, I can safely assume, that the user can re-enter the data in case of a windows reinstall. After all, that's how I get the data in the first place - user enters it, and then my app just auto-magically remembers it.
  • Paulius
    Paulius over 15 years
    AND the information must be safe from other applications, that the same user might execute.
  • sina
    sina over 15 years
    Be aware that the flag is only honoured on NTFS partitions. If the data is saved/copied to a FAT partition (e.g. USB stick) it will be in its raw form.
  • sina
    sina over 15 years
    For .NET 2.0+ use the System.Security.Cryptography.ProtectedData class instead of @bobince's 2nd link.
  • Quibblesome
    Quibblesome over 15 years
    Wait... if they don't have a password that your app uses then.... what prevents another user using the same program on the same machine and reading their data that way?
  • Quibblesome
    Quibblesome over 15 years
    I guess it depends on how "secret" the data should be. Family / friends or just outsiders.
  • Paulius
    Paulius over 15 years
    No it doesn't. Just read the other answers - there IS an API in WINDOWS, that allows to store data without password (well, internally, password is used, but my app doesn't need to remember it - it's all managed internally by Windows).
  • Quibblesome
    Quibblesome over 15 years
    Well then I humbly apologise for both a) not having a complete answer and b) trying to help.
  • Casebash
    Casebash almost 13 years
    From MSDN: "Do not use isolated storage to store high-value secrets, such as unencrypted keys or passwords, because isolated storage is not protected from highly trusted code, from unmanaged code, or from trusted users of the computer."
  • MrJman006
    MrJman006 about 2 years
    Also note that I don't believe this will work for the basic home/personal edition of Windows. It only applies to some of the higher level Windows licenses.