XML 中的哈希密码 - C#
我正在努力对密码进行哈希和加盐处理。我正在创建一个 XML 文件,其中存储一个人的姓名、卡号和密码。然后我需要将 xml 文件中的信息显示回控制台窗口。我能够加密卡号并将其显示到控制台,并将其解密回控制台窗口,但我正在努力对密码进行散列和加盐并将其显示回控制台窗口。
UserInfo addUser = new UserInfo
{
CardNumber = "1234-5678-9012-3456",
Password = "Pa$$w0rd",
CustomerName = "Bob Smith"
};
XmlSerializer serialize = new XmlSerializer(typeof(UserInfo));
string path = Combine(CurrentDirectory, "customers.xml");
using (FileStream stream = File.Create(path))
{
serialize.Serialize(stream, addUser);
}
private static string SaltAndHashPassword(string pasword, string salt)
{
var sha = SHA256.Create();
var saltedPassword = pasword + salt;
return Convert.ToBase64String(sha.ComputeHash(Encoding.Unicode.GetBytes(saltedPassword)));
}
I'm struggling with hashing and salting a password. I am creating an XML file which stores a person's name, card number and password. I then need to display the information from the xml file back to the console window. I was able to encrypt the card number and display it to the console and also decrypt it back to the console window, but I'm struggling with hashing and salting the password and displaying it back to the console window.
UserInfo addUser = new UserInfo
{
CardNumber = "1234-5678-9012-3456",
Password = "Pa$w0rd",
CustomerName = "Bob Smith"
};
XmlSerializer serialize = new XmlSerializer(typeof(UserInfo));
string path = Combine(CurrentDirectory, "customers.xml");
using (FileStream stream = File.Create(path))
{
serialize.Serialize(stream, addUser);
}
private static string SaltAndHashPassword(string pasword, string salt)
{
var sha = SHA256.Create();
var saltedPassword = pasword + salt;
return Convert.ToBase64String(sha.ComputeHash(Encoding.Unicode.GetBytes(saltedPassword)));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不想存储原始客户信息。您需要将卡号和密码的加盐和哈希版本存储在文件中。因此,您的 UserInfo 类需要类似于以下内容。如何进行加盐和散列取决于您(以及我假设的作业是什么)。关于将 XML 文件显示到控制台的部分实际上是一个单独的问题,还有很多其他答案。
You never want to store raw customer information. You need to store the salted and hashed version of the card number and password in the file. So your UserInfo class would need to be something like the following. How you do the salting and hashing is up to you (and what I'm assuming the assignment is). The part about displaying the XML file to the console is really a separate question with lots of other answers on SO.