繁体   English   中英

从XML文件中提取/解密哈希/加盐密码

[英]Extracting/Decrypting hashed/salted password out of XML file

我在该节目中阅读的大多数主题都将密码与哈希/加盐密码进行了比较。 那不是我的用例。 我需要从xml文件中读取经过哈希加密的密码,然后使用该密码登录Sql数据库。 我将需要从Windows服务执行此操作。 我首先不确定如何读取XML文件中的条目,然后如何对其进行“解密”?

GenerateSaltForPassword和ComputePasswordHash函数直接来自以下SO帖子

private int GenerateSaltForPassword()
        {
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            byte[] saltBytes = new byte[4];
            rng.GetNonZeroBytes(saltBytes);
            return (((int) saltBytes[0]) << 24) + (((int) saltBytes[1]) << 16) + (((int) saltBytes[2]) << 8) +
                   ((int) saltBytes[3]);
        }

        private byte[] ComputePasswordHash(string password, int salt)
        {
            byte[] saltBytes = new byte[4];
            saltBytes[0] = (byte) (salt >> 24);
            saltBytes[1] = (byte) (salt >> 16);
            saltBytes[2] = (byte) (salt >> 8);
            saltBytes[3] = (byte) (salt);

            byte[] passwordBytes = UTF8Encoding.UTF8.GetBytes(password);

            Byte[] preHashed = new Byte[saltBytes.Length + passwordBytes.Length];
            System.Buffer.BlockCopy(passwordBytes, 0, preHashed, 0, passwordBytes.Length);
            System.Buffer.BlockCopy(saltBytes, 0, preHashed, passwordBytes.Length, saltBytes.Length);

            SHA1 sha1 = SHA1.Create();
            return sha1.ComputeHash(preHashed);
        }

XML文件是通过以下方式生成的:

 private void btnSave_Click(object sender, System.EventArgs e)
        {
            int salt = GenerateSaltForPassword();

            string fileName = System.IO.Path.Combine(Application.StartupPath, "alphaService.xml");
            XDocument doc = new XDocument();
            XElement xml = new XElement("Info",
                new XElement("DatabaseServerName", txtServerName.Text),
                new XElement("DatabaseUserName", txtDatabaseUserName.Text),
                new XElement("DatabasePassword", ComputePasswordHash(txtDatabasePassword.Text, salt)),
                new XElement("ServiceAccount", txtAccount.Text),
                new XElement("ServicePassword", ComputePasswordHash(txtServicePassword.Text, salt)));

            doc.Add(xml);
            doc.Save(fileName);
        }

正如安德烈(Andrei)正确指出的那样,散列密码的全部目的是使密码无法检索。 这是一种非常标准的技术,可以帮助保护密码文件(被黑客窃取)。 使用此文件的方式是:当用户尝试进行身份验证时,从DB(或您的XML)中读取salt,以与设置密码时相同的方式将其添加到输入的密码中(在您的代码中,密码附加在密码字节之后),然后将所得哈希与存储的哈希进行比较。 如果相同,则用户输入正确的密码,否则输入错误的密码。 因此可以完成密码验证,但是密码本身无法恢复。

现在,如果不使用盐或弱盐,则有可能使用彩虹表来反向散列,但这更多是一种黑客技巧,并不实用。

简而言之,您无法从哈希中恢复密码,因为这就是为什么首先对其进行哈希处理的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM