我有几种方法可以进行一些加密,它总是\n在我比较的值中添加一个.
这是因为一些新的线条特征吗?
public static class Security
{
public static string CreateHash(this string unHashed)
{
SHA256CryptoServiceProvider x = new SHA256CryptoServiceProvider();
byte[] data = Encoding.UTF8.GetBytes(unHashed);
data = x.ComputeHash(data);
return Encoding.UTF8.GetString(data);
}
public static bool MatchHash(this string hashData, string hashUser)
{
if (hashUser == hashData)
{
return true;
}
else
{
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我将值输入到一个文本框中并运行CreateHash()以将值输入另一个文本框(全部用于测试).然后我从另一个文本框中运行我的比较检查.
protected void btn_dehash_Click(object sender, EventArgs e)
{
// get value entered and hash it
var matchPass = txt_password.Text.CreateHash();
// populate lbl with true or false
lbl_Name.Text = txt_hashed.Text.MatchHash(matchPass) ? "true" : "false";
}
Run Code Online (Sandbox Code Playgroud)
我得到的结果MD5是好的,因为这要短得多.我想使用更安全的方法,所以我使用了SHA256,这是我从比较中得到的结果.

有人知道为什么会这样吗?
不要将哈希变成字符串!将其保存为字节数组.
散列表示一个字节序列,可能有效也可能无效,如UTF-8.
如果你想要一个可读的哈希,那么做类似于:
byte[] hash = x.ComputeHash(bytes);
string hashString = string.Empty;
foreach (byte x in hash)
{
// Turn each byte into it's hex equivalent (00 to FF).
hashString += String.Format("{0:x2}", x);
}
return hashString;
Run Code Online (Sandbox Code Playgroud)
或者,正如评论所示,使用base 64编码.
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |