5

我正在使用下面的代码将密码保存到注册表,如何将其转换回来?下面的代码不是我的,但它加密得很好。

谢谢

using System.Security.Cryptography;

public static string EncodePasswordToBase64(string password)
{  byte[] bytes   = Encoding.Unicode.GetBytes(password);
   byte[] dst     = new byte[bytes.Length];
   byte[] inArray = HashAlgorithm.Create("SHA1").ComputeHash(dst);
   return Convert.ToBase64String(inArray);
}
4

11 回答 11

32

SHA1 是哈希算法,不是加密算法。散列算法是一种单向函数,它将数据转换为该数据的散列,但无法从散列中取回原始数据。加密算法是将数据转换为加密数据的双向函数,然后可以将加密数据转换回原始数据。

于 2008-11-06T15:28:57.087 回答
12

要安全地存储密码以便回读,请使用ProtectedData类。

public static string ProtectPassword(string password)
{
    byte[] bytes = Encoding.Unicode.GetBytes(password);
    byte[] protectedPassword = ProtectedData.Protect(bytes, null, DataProtectionScope.CurrentUser);
    return Convert.ToBase64String(protectedPassword);
}

public static string UnprotectPassword(string protectedPassword)
{
    byte[] bytes = Convert.FromBase64String(protectedPassword);
    byte[] password = ProtectedData.Unprotect(bytes, null, DataProtectionScope.CurrentUser);
    return Encoding.Unicode.GetString(password);
}
于 2008-11-06T15:54:03.760 回答
8

将用户输入的任何内容作为密码来访问系统,以相同的方式对其进行加密,然后比较加密的值,这是正常的方法。我很确定 SHA1 是一种陷门加密,即无法回溯。

于 2008-11-06T15:28:18.197 回答
7

你没有。

SHA1 是哈希,而不是加密。这是一种单向操作;转换回来是不可能的。

(好吧,严格来说这不是真的;如果你有一个可能的 SHA1 值和纯文本值的表,一个彩虹表,那么你可能很幸运)

此外,您应该对哈希值加盐,因为您现在很容易受到彩虹表攻击。杰夫在他的博客上谈得更多

于 2008-11-06T15:29:20.050 回答
3

好的,所以我知道这不是在回答您的具体问题,但是您为什么要将其转换回来?

如果要进行比较以提供身份验证,标准方法是加密此文本 ALSO,并将存储的密码与提供的密码进行比较。

这更安全,因为这意味着原始密码永远不需要解密。

于 2008-11-06T15:29:39.627 回答
2

我认为使用哈希的要点之一是它们不能被计算回来。

正如其他人所说,根据用户密码计算哈希并与存储的哈希值进行比较。

于 2008-11-06T15:28:20.147 回答
2

为了使用 System.Security.Cryptography.ProtectedData 类,您必须在项目中添加对 System.Security 的引用。

(鼠标右键单击 References 文件夹,选择“Add Reference...”,在 .NET 选项卡上找到 System.Security)

于 2008-11-06T19:02:44.207 回答
1

嗯,只是好奇,但这不会为所有相同长度的密码返回相同的哈希值吗?

于 2008-11-06T15:37:35.150 回答
0

You want to use encryption not hashing. SHA is fine but use the encryption methods for it. The problem with encryption is always where to put the key for it. You didn't mention whether this was a workstation or server you were doing this on. On a server, I find it better to just use ACL's to restrict access to the reg key. Admins can usually access the encryption key anyway...you have to have some trust somewhere. On a workstation then you can go with encryption and store the key in code or use a certificate and restrict access to it at least in a corp environment...not for sale software).

You can use the ProtectedData class but be aware that it uses user profiles for its key usage and as such you have to make sure you are impersonating the user who has the profile with the key you need. This may or may not be trivial and may or may not cause headaches and security issues.

于 2008-11-06T16:44:44.390 回答
0

使用上面您自己的代码片段,您想要做的是在用户最初选择密码时调用该方法 - 但在密码字符串中的某处(通常在开头或结尾)添加称为盐的密码。然后,当用户稍后尝试进行身份验证时,他们输入他们的密码,您通过相同的方法运行该密码和散列,如果两个散列相等,则密码相等且有效的统计上极好的机会。

话虽这么说,SHA1 已知有弱点,您应该选择更强大的算法。如果你想留在SHA家族,SHA512还是不错的。

于 2008-11-06T16:01:25.820 回答
0

我注意到最近添加了 XMLEncryptedData 类。对于将数据加密到 XML 文件,XMLEncryptedData 方法是否比 DPAPI 方法更可取?

于 2008-12-30T02:29:15.520 回答