Node.js密码加密与Asp.Net中相同

Node.js密码加密与Asp.Net中相同,asp.net,sql-server,node.js,encryption,Asp.net,Sql Server,Node.js,Encryption,我在asp.net中创建了一个wesite,并使用ms sql数据库保存记录。现在想在node.js应用程序中转换它。并且希望使用相同的sql数据库。在asp.net应用程序中,我为注册用户加密了密码。下面是代码 public static string CreateHash(string unHashed) { System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security

我在asp.net中创建了一个wesite,并使用ms sql数据库保存记录。现在想在node.js应用程序中转换它。并且希望使用相同的sql数据库。在asp.net应用程序中,我为注册用户加密了密码。下面是代码

   public static string CreateHash(string unHashed)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
        byte[] data = System.Text.Encoding.ASCII.GetBytes(unHashed);
        data = x.ComputeHash(data);
        return System.Text.Encoding.ASCII.GetString(data);
    }


    public static bool MatchHash(string HashData, string HashUser)
    {
        HashUser = CreateHash(HashUser);
        if (HashUser == HashData)
            return true;
        else
            return false;

    }
现在的问题是如何在node.js中使用相同的加密。所以,当节点应用程序准备就绪时,老用户也可以进行登录。只有当节点应用程序也使用我在asp.net中使用的相同加密时,才有可能


对于节点,我已经创建了所有环境,并使用mssql模块进行数据库通信。请帮我修一下。谢谢

首先,如果您认真考虑安全问题,MD5将不再使用

根据您的评论和代码,我担心初始ASP.net代码中存在“数据丢失”。 让我们再看看CreateHash函数,我添加了一些注释:

   public static string CreateHash(string unHashed)
    {
        System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();

        // Convert unHashed string to bytes using ASCII coding
        byte[] data = System.Text.Encoding.ASCII.GetBytes(unHashed);

        // Compute MD5 hash from bytes
        data = x.ComputeHash(data);

        // Decode MD5 resulting bytes as ASCII
        return System.Text.Encoding.ASCII.GetString(data);
    }
最后一行让我困惑,它将从MD5函数接收的字节解码为ASCII,但这是不正确的假设。您在注释中给出的结果编码字符串包含许多“?”

Next node.js代码将执行类似的操作,除了使用十六进制而不是ascii编码字符串:

var crypto = require('crypto')

function createHash(data)  {
    return crypto.createHash('md5').update(data, 'ascii').digest('hex')
}

要模拟“字节到ascii”,您可以尝试.digest('binary')而不是hex。如果它没有给出您期望的结果,那么您必须进行单独的从十六进制到ascii的“转换”步骤。(我的经验不足,无法为后面的问题提供优雅的解决方案)

谢谢您的回复,但似乎不起作用。我使用了字符串“testpassword”。净返回“?k*?#?N?9?l”和节点返回“e16b2ab8d12314bf4efbd6203906ea6c”。节点还应返回与.net函数相同的值。@user1951489我已更新了答案,因为您为我提供了有关comment中发生的事情的更多线索。节点返回“ak*,ñ#NÖ9”♠êl“if use.digest('binary')。它应该返回?k*??N?9?l?叹气。。。同样,它只是第一个ASP.net解释器/控制台/任何示例都无法显示大多数符号:ák*,ëţNÖ9♠êl并使用“?”代替它,那么它应该打印true而不是false var crypto=require('crypto')函数createHash(data){return crypto.createHash('md5').update(data,'ascii').digest('binary')}console.log(createHash('testpassword')=''k*?#N×9?l'))