我正在编写一个使用第三方 api 的 .NET 应用程序。存储 api 凭据的最佳方式是什么?我正在考虑使用 RSACryptoServiceProvider 对它们进行加密,但我的问题是:如果我使用 RSA 加密我的凭据,我将不得不在应用程序中的某处拥有私钥来解密凭据,这是否意味着加密是基本无关?因为任何人都可以继续使用加密的凭据并使用我必须提供的密钥来解密凭据。
处理私钥并避免使用我的应用程序的任何人的最佳方法是什么?
如果我将密钥存储为班级某处的字符串,是否可以读取密钥?
我正在编写一个使用第三方 api 的 .NET 应用程序。存储 api 凭据的最佳方式是什么?我正在考虑使用 RSACryptoServiceProvider 对它们进行加密,但我的问题是:如果我使用 RSA 加密我的凭据,我将不得不在应用程序中的某处拥有私钥来解密凭据,这是否意味着加密是基本无关?因为任何人都可以继续使用加密的凭据并使用我必须提供的密钥来解密凭据。
处理私钥并避免使用我的应用程序的任何人的最佳方法是什么?
如果我将密钥存储为班级某处的字符串,是否可以读取密钥?
是的,如果您将密钥存储为一个简单的字符串,则可以读取它。但是您可以使用 SecureString 类并最大限度地减少易失性访问。
此外,永远不要将 SecureString 的内容放入字符串中:如果这样做,则字符串在堆中未加密,并且在垃圾回收后重用内存之前不会将其字符清零。
通过 C# 来自 CLR 的示例:
public static class Program
{
public static void Main()
{
using (SecureString ss = new SecureString())
{
Console.Write("Please enter password: ");
while (true)
{
ConsoleKeyInfo cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.Enter) break;
// Append password characters into the SecureString
ss.AppendChar(cki.KeyChar);
Console.Write("*");
}
Console.WriteLine();
// Password entered, display it for demonstration purposes
DisplaySecureString(ss);
}
// After 'using', the SecureString is Disposed; no sensitive data in memory
}
// This method is unsafe because it accesses unmanaged memory
private unsafe static void DisplaySecureString(SecureString ss)
{
Char* pc = null;
try
{
// Decrypt the SecureString into an unmanaged memory buffer
pc = (Char*)Marshal.SecureStringToCoTaskMemUnicode(ss);
// Access the unmanaged memory buffer that
// contains the decrypted SecureString
for (Int32 index = 0; pc[index] != 0; index++)
Console.Write(pc[index]);
}
finally
{
// Make sure we zero and free the unmanaged memory buffer that contains
// the decrypted SecureString characters
if (pc != null)
Marshal.ZeroFreeCoTaskMemUnicode((IntPtr)pc);
}
}
}
对于已经有权访问您的计算机以及您的 Windows 用户名和密码的坚定攻击者,您将无法隐藏它。
话虽如此,ProtectedData 类可能是使没有适当凭据的用户无法访问数据的最简单方法。