XML 中的哈希密码 - C#

发布于 2025-01-17 03:13:35 字数 879 浏览 1 评论 0原文

我正在努力对密码进行哈希和加盐处理。我正在创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天冷不及心凉 2025-01-24 03:13:35

您永远不想存储原始客户信息。您需要将卡号和密码的加盐和哈希版本存储在文件中。因此,您的 UserInfo 类需要类似于以下内容。如何进行加盐和散列取决于您(以及我假设的作业是什么)。关于将 XML 文件显示到控制台的部分实际上是一个单独的问题,还有很多其他答案。

public class UserInfo
{
    public string CustomerName {get; set;}

    public string HashedPassword {get; set;}
    public string DecryptPassword() => DecryptValue(HashedPassword);

    public string HashedCardNumber {get; set;}
    public string DecriptCardNumber() => DecryptValue(HashedCardNumber);

    /// xml serializer needs a public empty constructor
    public UserInfo() {} 

    /// this is the constructor you'll used when you add a new customer
    public UserInfo(string customerName, string password, string creditCard)
    {
        CustomerName = customerName;
        HashedPassword = HashAndSaltValue(password, "someSalt");
        HashedCreditCard = HashAndSaltValue(creditCard, "someSalt");
    }

    private static Decrypt(string value, string salt)
    {
        // unhash and unsalt value
    }

    private static string SaltAndHashPassword(string password, string salt)
    {
        var sha = SHA256.Create();
        var saltedPassword = password + salt;
        return Convert.ToBase64String(
                   sha.ComputeHash(
                     Encoding.Unicode.GetBytes(saltedPassword)));
    }
}

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.

public class UserInfo
{
    public string CustomerName {get; set;}

    public string HashedPassword {get; set;}
    public string DecryptPassword() => DecryptValue(HashedPassword);

    public string HashedCardNumber {get; set;}
    public string DecriptCardNumber() => DecryptValue(HashedCardNumber);

    /// xml serializer needs a public empty constructor
    public UserInfo() {} 

    /// this is the constructor you'll used when you add a new customer
    public UserInfo(string customerName, string password, string creditCard)
    {
        CustomerName = customerName;
        HashedPassword = HashAndSaltValue(password, "someSalt");
        HashedCreditCard = HashAndSaltValue(creditCard, "someSalt");
    }

    private static Decrypt(string value, string salt)
    {
        // unhash and unsalt value
    }

    private static string SaltAndHashPassword(string password, string salt)
    {
        var sha = SHA256.Create();
        var saltedPassword = password + salt;
        return Convert.ToBase64String(
                   sha.ComputeHash(
                     Encoding.Unicode.GetBytes(saltedPassword)));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文