密码可以存储在纯文本中,只要它在内存中,而不是存储在数据库,文件等中吗?

时间:2014-02-25 14:54:06

标签: c# .net security

请参阅下面的代码片段(一个包含明文密码等敏感数据,另一个用明文加密)。我理解如果在文件或数据库等中保留这些,需要采取预防措施,如设置ACL等,以便攻击者无法轻易找到它们。

但是,如果密码不需要保留,该怎么办:

  1. 方法2是否比方法1好,因为密码只在内存中?或者,是否没有必要?或者,是否有可能某人可以通过内存读取密码 - 始终建议在内存中加密或持久加密?

  2. 如果对象被序列化并通过应用程序域传递怎么办? (请注意,我了解如果密码是通过HTTP(网络)发送的,它需要加密,但如果它只是跨应用程序域我可以发送普通密码吗?

  3. 此致

    纯文本密码代码段

      [Serializable]
        class PlainTextPassword
        {
            //Password stored in plain text
            private string _plainTextPassword = null;
            public PlainTextPassword(string password)
            {
                this._plainTextPassword = password;
            }        
            public string Password
            {
                get
                {
                    return this._plainTextPassword;
                }
            }
        }
    

    加密密码代码段

     [Serializable]
        class EncryptedPassword
        {
            //Encrypted password
            private string _encryptedPassword = null;
            public EncryptedPassword(string password)
            {
                byte[] encryptedPassword = ProtectedData.Protect(System.Text.Encoding.Unicode.GetBytes(password), null, DataProtectionScope.CurrentUser);
                this._encryptedPassword = System.Text.Encoding.Unicode.GetString(encryptedPassword);
            }
            public string Password
            {
                get
                {
                    return this._encryptedPassword;
                }
            }        
        }
    

1 个答案:

答案 0 :(得分:3)

方法2)在内存快照方面不比1)更安全。您无法控制GC何时释放原始密码(因为您几乎无法控制GC) - 因此,如果您要针对内存快照进行安全性拍摄,请查看

SecureString

参考: http://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx

对于可以拍摄内存快照的工具,请查看一些分析器(如Dynatrace)。