如何在注册表中保存密码

我有一个带有远程接口的桌面应用程序。 通过用户名和密码保护对远程接口的访问。

什么是安全保存这些密码的最佳方法,最好是在注册表中?

您需要保存散列密码(无论是在注册表中还是其他位置)。 然后,当用户输入他们的密码时,您将检查他们输入的散列版本以及存储的散列版本。 如果匹配则密码匹配,您可以让用户进入。

这样,您就不会以任何人(包括您自己)的身份以明文forms存储密码,以获取并以其他人身份获取访问权限。

至于使用哪种哈希算法 – 我不知道。 有很多可供选择,所以我不愿意推荐一个盲人。 我建议你找几个并评估它们。 CSharpFriends有一篇文章看起来可能是一个很好的起点。

如果确实需要存储未散列的密码,请查看使用ProtectedData类 。 这使用了Data Protection API(DPAPI) ,这是在Windows上保护数据的最佳方式。

这是一个包装ProtectedData的小类,并在String上提供两种扩展方法来加密和解密数据:

public static class DataProtectionApiWrapper { ///  /// Specifies the data protection scope of the DPAPI. ///  private const DataProtectionScope Scope = DataProtectionScope.CurrentUser; public static string Encrypt(this string text) { if (text == null) { throw new ArgumentNullException("text"); } //encrypt data var data = Encoding.Unicode.GetBytes(text); byte[] encrypted = ProtectedData.Protect(data, null, Scope); //return as base64 string return Convert.ToBase64String(encrypted); } public static string Decrypt(this string cipher) { if (cipher == null) { throw new ArgumentNullException("cipher"); } //parse base64 string byte[] data = Convert.FromBase64String(cipher); //decrypt data byte[] decrypted = ProtectedData.Unprotect(data, null, Scope); return Encoding.Unicode.GetString(decrypted); } }