C# 从 SHA256 解密
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10271645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Decrypt from SHA256
提问by netmajor
I have that code to encrypt string to sha256 and next to base64:
我有将字符串加密为 sha256 和 base64 旁边的代码:
public static string Sha256encrypt(string phrase)
{
UTF8Encoding encoder = new UTF8Encoding();
SHA256Managed sha256hasher = new SHA256Managed();
byte[] hashedDataBytes = sha256hasher.ComputeHash(encoder.GetBytes(phrase));
return Convert.ToBase64String(hashedDataBytes);
}
How can I decrypt my password in other side?
如何在另一端解密我的密码?
采纳答案by DaveShaw
You cannot decrypt the result of a One Way Hash. What you should do instead is compare a hash of the entered password versus the stored hash in the database.
您无法解密One Way Hash的结果。您应该做的是比较输入密码的哈希值与数据库中存储的哈希值。
Example:
例子:
var password = "1234";
var hashedPassword = Sha256encrypt(password);
var allowLogin = hashedPassword == storedPassword; //storedPassword from Database, etc.
This is only the very basics though, when using hashing algorithms you should consider using a Salttoo.
这只是最基本的,当使用散列算法时,你也应该考虑使用Salt。
回答by netmajor
It is impossible per se. SHA is a hash function, which implies it is one-way and used just for validation and similar things. Since the result of SHA-256 is of fixed length (256 bits) that also means that most of the information is lost when computing it.
这本身是不可能的。SHA 是一个散列函数,这意味着它是单向的,仅用于验证和类似的事情。由于 SHA-256 的结果是固定长度(256 位),这也意味着大部分信息在计算时会丢失。
You can brute-force it though, meaning that you could try and compute hash of a large number of different inputs and see if the hash matches.
不过,您可以对其进行暴力破解,这意味着您可以尝试计算大量不同输入的哈希值,然后查看哈希值是否匹配。
Sometime in the future a cryptographic weakness may be found for SHA thus making it breakable but practically it is not a reversible function.
将来的某个时候,可能会发现 SHA 的加密弱点,从而使其易碎,但实际上它不是可逆函数。
See details about hash functions on Wikipedia.
请参阅Wikipedia上有关哈希函数的详细信息。

