【问题标题】:RSA Encryption, Key storageRSA加密,密钥存储
【发布时间】:2012-08-23 13:21:28
【问题描述】:

我正在编写一个使用第三方 api 的 .NET 应用程序。存储 api 凭据的最佳方式是什么?我正在考虑使用 RSACryptoServiceProvider 对它们进行加密,但我的问题是:如果我使用 RSA 加密我的凭据,我将不得不在应用程序中的某处拥有私钥来解密凭据,这是否意味着加密是基本无关?因为任何人都可以继续使用加密的凭据并使用我必须提供的密钥来解密凭据。

处理私钥并防止任何使用我的应用程序的人的最佳方法是什么?

如果我将密钥作为字符串存储在班级某处,是否可以读取密钥?

【问题讨论】:

    标签: c# .net security cryptography rsa


    【解决方案1】:

    对于已经可以访问您的计算机以及您的 Windows 用户名和密码的坚定攻击者,您将无法隐藏它。

    话虽如此,ProtectedData class 可能是使没有适当凭据的用户无法访问数据的最简单方法。

    【讨论】:

    • 我就是这么想的。我想我只会使用加密的凭据构建应用程序,但只将解密密钥提供给受信任的机器。
    【解决方案2】:

    是的,如果将密钥存储为简单字符串,则可以读取它。但是您可以使用 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);
            }
        }
    }
    

    【讨论】:

    • 我真的不明白,我不需要在代码中的某处将密钥作为明文来生成安全字符串吗?无论如何,如果它就在身边,那么拥有它有什么意义呢?
    猜你喜欢
    • 2017-03-07
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2011-11-07
    • 2011-03-20
    相关资源
    最近更新 更多