C#读回加密密码

5 c# hash

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

谢谢

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);
}
Run Code Online (Sandbox Code Playgroud)

yfe*_*lum 32

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


Bra*_*ger 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);
}
Run Code Online (Sandbox Code Playgroud)


Laz*_*rus 8

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


blo*_*art 7

你没有.

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

(好吧,这不是严格意义上的;如果你有一个可能的SHA1值和纯文本值的表,那么彩虹表就可以运气了)

你也应该用盐水腌制,因为你现在很容易受到彩虹表攻击.杰夫在他的博客上谈了这个