How to store passwords in Winforms application?

前端 未结 4 981
梦如初夏
梦如初夏 2020-11-30 22:44

I have some code like this in a winforms app I was writing to query a user\'s mail box Storage Quota.

DirectoryEntry mbstore = new DirectoryEntry(
      @\"L         


        
相关标签:
4条回答
  • 2020-11-30 22:58

    As mentioned, the Data Protection API is a good way to do this. Note that if you're using .NET 2.0 or greater, you don't need to use P/Invoke to invoke the DPAPI. The framework wraps the calls with the System.Security.Cryptography.ProtectedData class.

    0 讨论(0)
  • 2020-11-30 22:59

    I found this book by keith Brown The .NET Developer's Guide to Windows Security. It has some good samples covering all kinds of security scenarios. Free Online version is also available.

    0 讨论(0)
  • 2020-11-30 23:20

    If you store it as a secure string and save the secure string to a file (possibly using Isolated Storage, the only time you will have a plain text password is when you decrypt it to create your mbstore. Unfortunately, the constructor does not take a SecureString or a Credential object.

    0 讨论(0)
  • 2020-11-30 23:21

    The sanctified method is to use CryptoAPI and the Data Protection APIs.

    To encrypt, use something like this (C++):

    DATA_BLOB blobIn, blobOut;
    blobIn.pbData=(BYTE*)data;
    blobIn.cbData=wcslen(data)*sizeof(WCHAR);
    
    CryptProtectData(&blobIn, description, NULL, NULL, NULL, CRYPTPROTECT_LOCAL_MACHINE | CRYPTPROTECT_UI_FORBIDDEN, &blobOut);
    _encrypted=blobOut.pbData;
    _length=blobOut.cbData;
    

    Decryption is the opposite:

    DATA_BLOB blobIn, blobOut;
    blobIn.pbData=const_cast<BYTE*>(data);
    blobIn.cbData=length;
    
    CryptUnprotectData(&blobIn, NULL, NULL, NULL, NULL, CRYPTPROTECT_UI_FORBIDDEN, &blobOut);
    
    std::wstring _decrypted;
    _decrypted.assign((LPCWSTR)blobOut.pbData,(LPCWSTR)blobOut.pbData+blobOut.cbData/sizeof(WCHAR));
    

    If you don't specify CRYPTPROTECT_LOCAL_MACHINE then the encrypted password can be securely stored in the registry or config file and only you can decrypt it. If you specify LOCAL_MACHINE, then anyone with access to the machine can get it.

    0 讨论(0)
提交回复
热议问题