为什么不使用 DPAPI 加密所有设置而不是只加密主密码?

Why not use DPAPI for encrypting all settings instead of only encrypting main password?

在我的应用程序中,我需要加密各种设置和密码。 到目前为止,我一直在使用 RijndaelManaged class 等进行此操作,如下所示:

/// <summary>
/// Encrypts the string defined by parameter "data" and returns the encrypted data as string
/// </summary>
/// <param name="data">Data to be encrypted</param>
/// <returns>The encrypted data</returns>
public static string Encrypt(string data)
        {
            if (data == "")
                return "";

            byte[] bytes = Encoding.ASCII.GetBytes(initVector);
            byte[] rgbSalt = Encoding.ASCII.GetBytes(saltValue);
            byte[] buffer = Encoding.UTF8.GetBytes(data);
            byte[] rgbKey = new PasswordDeriveBytes(passPhrase, rgbSalt, hashAlgorithm, passwordIterations).GetBytes(keySize / 8);
            RijndaelManaged managed = new RijndaelManaged();
            managed.Mode = CipherMode.CBC;
            ICryptoTransform transform = managed.CreateEncryptor(rgbKey, bytes);
            MemoryStream memStream = new MemoryStream();
            CryptoStream cryStream = new CryptoStream(memStream, transform, CryptoStreamMode.Write);
            cryStream.Write(buffer, 0, buffer.Length);
            cryStream.FlushFinalBlock();
            byte[] inArray = memStream.ToArray();
            memStream.Close();
            cryStream.Close();
            return Convert.ToBase64String(inArray);
        }

通常的问题是我需要将密码(和盐值)存储在某处。 为了以连续的方式存储密码,我遇到了 DPAPI Protect() 和 Unprotect() classes,如下所示:

/// <summary>
/// Use Windows' "Data Protection API" to encrypt the string defined by parameter "clearText".
/// To decrypt, use the method "Unprotect"
/// http://www.thomaslevesque.com/2013/05/21/an-easy-and-secure-way-to-store-a-password-using-data-protection-api/
/// </summary>
/// <param name="clearText"></param>
/// <param name="optionalEntropy"></param>
/// <param name="scope"></param>
/// <returns></returns>
        public static string Protect(string clearText, string optionalEntropy = null, DataProtectionScope scope = DataProtectionScope.CurrentUser)
        {
            if (clearText == null)
                throw new ArgumentNullException("The parameter \"clearText\" was empty");
            byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
            byte[] entropyBytes = string.IsNullOrEmpty(optionalEntropy) ? null : Encoding.UTF8.GetBytes(optionalEntropy);
            byte[] encryptedBytes = ProtectedData.Protect(clearBytes, entropyBytes, scope);
            return Convert.ToBase64String(encryptedBytes);
        }

我的问题如下: 使用 DPAPI,我现在可以以安全的方式存储我的加密方法的密码,但为什么我不应该直接使用 DPAPI 来加密我的所有设置? 这会用大量数据填充 DPAPI 吗?

我的想法不是执行以下操作:

string setting1 = ”mySettingValue1”;
StoreSettingSomewhere(Encrypt(setting1));

我可以做到以下几点:

string setting1 = ”mySettingValue1”;
StoreSettingSomewhere(Protect(setting1, bla bla bla));

我知道在使用 DPAPI 时我必须在同一台机器上(或使用同一用户)解密,但这对我来说不是问题。

感谢任何帮助!

数据保护 API 返回一个不透明的 blob,它是您想要加密的内容的加密(加盐和散列)结果。

你不能 "fill up" DP API - 你只能 "fill up" 你自己 (因为 blob 有点大;但它们确实在内部包含了以后验证加密数据所需的一切)。

Data Protection API的down-side是您必须以用户身份登录;并且您不能在用户之间共享设置(除非您使用了 Machine-wide 范围)。