如何安全地保存用户名/密码(本地)? [英] How to securely save username/password (local)?

查看:1330
本文介绍了如何安全地保存用户名/密码(本地)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在做一个Windows应用程序,您需要登录到第一位。

该帐户的详细信息包括用户名和密码,并且需要将它们保存在本地。

这只是一个安全的事,让其他人使用同一台计算机不能​​看到每个人的个人资料。

什么是保存这些数据的最佳/最安全的方法是什么?


我不想使用一个数据库,所以我尝试了一些东西与资源文件。

但是,由于我有点这个新的,我不能完全肯定我在做什么,并在那里我要寻找一个解决方案。

推荐答案

如果你只是要验证/验证输入的用户名和密码,使用Rfc2898DerivedBytes类(也称为密码基于密钥推导函数2或PBKDF2)。这比,因为没有从RFC2898DerivedBytes的结果返回给密码去没有实际的方式使用加密像三重DES或AES更安全。您只能从一个密码,结果去了。见<一href=\"http://stackoverflow.com/questions/12529574/is-it-ok-to-use-sha1-hash-of-password-as-a-salt-when-deriving-encryption-key-and\">Is它确定,派生从密码字符串加密密钥和IV时使用的密码的SHA1哈希作为盐?的用于.NET或<为例,讨论href=\"http://stackoverflow.com/questions/12338514/string-encrypt-decrypt-with-password-c-sharp-metro-style\">String加密/密码为C#Metro风格获得的WinRT /地铁解密。

If you are just going to verify/validate the entered user name and password, use the Rfc2898DerivedBytes class (also known as Password Based Key Derivation Function 2 or PBKDF2). This is more secure than using encryption like Triple DES or AES because there is no practical way to go from the result of RFC2898DerivedBytes back to the password. You can only go from a password to the result. See Is it ok to use SHA1 hash of password as a salt when deriving encryption key and IV from password string? for an example and discussion for .Net or String encrypt / decrypt with password c# Metro Style for WinRT/Metro.

如果你是存储重复使用的密码,比如其提供给第三方使用的Windows数据保护API(DPAPI)。这使用操作系统生成和保护键和三重DES 的加密算法来加密和解密信息。这意味着你的应用程序不必担心使用加密时,生成和保护加密密钥,主要关心的问题。

If you are storing the password for reuse, such as supplying it to a third party, use the Windows Data Protection API (DPAPI). This uses operating system generated and protected keys and the Triple DES encryption algorithm to encrypt and decrypt information. This means your application does not have to worry about generating and protecting the encryption keys, a major concern when using cryptography.

在C#中,使用System.Security.Cryptography.ProtectedData类。例如,要加密的数据块,用<一href=\"http://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata.protect.aspx\"><$c$c>ProtectedData.Protect():

In C#, use the System.Security.Cryptography.ProtectedData class. For example, to encrypt a piece of data, use ProtectedData.Protect():

// Data to protect. Convert a string to a byte[] using Encoding.UTF8.GetBytes().
byte[] plaintext; 

// Generate additional entropy (will be used as the Initialization vector)
byte[] entropy = new byte[20];
using(RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
{
    rng.GetBytes(entropy);
}

byte[] ciphertext = ProtectedData.Protect(plaintext, entropy,
    DataProtectionScope.CurrentUser);

存放熵和密文牢固,如在与这样只有当前用户可以阅读它设置权限的文件或注册表项。为了访问原始数据,使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.security.cryptography.protecteddata.unprotect.aspx\"><$c$c>ProtectedData.Unprotect():

byte[] plaintext= ProtectedData.Unprotect(ciphertext, entropy,
    DataProtectionScope.CurrentUser);

请注意,有额外的安全注意事项。例如,避免将密码等作为字符串秘密。字符串是不可改变的,是他们不能在内存中通知,以便有人在看应用程序的内存或者内存转储可能会看到密码。使用 SecureString的或一个byte [],而不是记住处置或零它们作为一旦密码不再需要。

Note that there are additional security considerations. For example, avoid storing secrets like passwords as a string. Strings are immutable, being they cannot be notified in memory so someone looking at the application's memory or a memory dump may see the password. Use SecureString or a byte[] instead and remember to dispose or zero them as soon as the password is no longer needed.

这篇关于如何安全地保存用户名/密码(本地)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆