What is the best practice when saving passwords using the C # Settings feature?

I use the built-in Visual C # Settings feature to save some parameters of my program. I also want to save one password, but then it becomes public ... is it possible to encrypt the password before saving it using this settings method, and then decrypt it back?

+5
source share
3 answers

For simple encryption requirements, I used DPAPI through the ProtectedData class . To make the resulting encrypted value stored in a text file or registry, I encode the received byte array.

Here is the class I wrote to wrap this:

namespace SomeNamespace
{
   using System;
   using System.Security.Cryptography;
   using System.Text;

   /// <summary>
   /// used for encryption and decryption
   /// </summary>
   public static class DataProtector
   {
      private const string EntropyValue = "secret";

      /// <summary>
      /// Encrypts a string using the DPAPI.
      /// </summary>
      /// <param name="stringToEncrypt">The string to encrypt.</param>
      /// <returns>The encrypted data.</returns>
      public static string EncryptData(string stringToEncrypt)
      {
         byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(stringToEncrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine);
         return Convert.ToBase64String(encryptedData);
      }

      /// <summary>
      /// Decrypts a string using the DPAPI.
      /// </summary>
      /// <param name="stringToDecrypt">The string to decrypt.</param>
      /// <returns>The decrypted data.</returns>
     public static string DecryptData(string stringToDecrypt)
      {
         byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(stringToDecrypt), Encoding.Unicode.GetBytes(EntropyValue), DataProtectionScope.LocalMachine);
         return Encoding.Unicode.GetString(decryptedData);
      }
   }
}
+4
source

An easy way to do this is to encrypt the password with yourself. You can never decrypt it, but you can compare the password entered by the user.

+4
source

, - , - .

, .

, , "/", , , (), . , .

, , .

+2