gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:50:42 24 4
gpt4 key购买 nike

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

/// <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() 类,如下所示:

/// <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 时我必须在同一台机器上(或使用同一用户)解密,但这对我来说不是问题。

感谢任何帮助!

最佳答案

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

您不能“填满” DP API - 您只能“填满”自己(因为 blob 有点大;但它们确实在内部包含了所有需要的东西稍后验证加密数据)。

Data Protection API 的缺点是您必须以用户身份登录;并且您不能在用户之间共享设置(除非您使用了计算机范围)。

关于c# - 为什么不使用 DPAPI 加密所有设置而不是只加密主密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com