我正在使用下面的代码将密码保存到注册表中,如何将其转换回来?下面的代码不是我的,但它加密很好.
谢谢
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)