【问题标题】:How can convert SHA1 Hashing function in nodejs that already implemented in .net C#如何在.net C#中实现的nodejs中转换SHA1哈希函数
【发布时间】:2020-02-16 02:09:36
【问题描述】:

我是 nodejs 的初学者。我已经通过sha1实现了加解密,并在asp.net项目中使用。现在我们在节点和角度开始了新项目。这里我需要相同的登录机制,包括使用 sha1 的加密和解密。

这是我的可行代码:

我必须需要因变量

        static string passPhrase = "Paaaa5p***";
        static string saltValue = "s@1t***lue";
        static string hashAlgorithm = "SHA1";
        static int passwordIterations = 2;
        static string initVector = "@1B2c3D4e5F6****";
        static int keySize = 256;

加密密码或任何文本的加密方法。

public static string EncryptText(string text)
        {

            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);

            byte[] plainTextBytes = Encoding.UTF8.GetBytes(text);

            PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                            passPhrase,
                                                            saltValueBytes,
                                                            hashAlgorithm,
                                                            passwordIterations);

            byte[] keyBytes = password.GetBytes(keySize / 8);


            RijndaelManaged symmetricKey = new RijndaelManaged();


            symmetricKey.Mode = CipherMode.CBC;


            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                                                             keyBytes,
                                                             initVectorBytes);


            MemoryStream memoryStream = new MemoryStream();


            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);

            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);


            cryptoStream.FlushFinalBlock();


            byte[] cipherTextBytes = memoryStream.ToArray();


            memoryStream.Close();
            cryptoStream.Close();


            string decryptText = Convert.ToBase64String(cipherTextBytes);


            return decryptText;
        }

加密密码或任何文本的解密方法。

    public static string DecryptText(string encryptText)
    {

        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
        byte[] cipherTextBytes = Convert.FromBase64String(encryptText);

        PasswordDeriveBytes password = new PasswordDeriveBytes(
                                                        passPhrase,
                                                        saltValueBytes,
                                                        hashAlgorithm,
                                                        passwordIterations);

        byte[] keyBytes = password.GetBytes(keySize / 8);

        RijndaelManaged symmetricKey = new RijndaelManaged();

        symmetricKey.Mode = CipherMode.CBC;


        ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                                                         keyBytes,
                                                         initVectorBytes);

        MemoryStream memoryStream = new MemoryStream(cipherTextBytes);


        CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                      decryptor,
                                                      CryptoStreamMode.Read);


        byte[] plainTextBytes = new byte[cipherTextBytes.Length];


        int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                   0,
                                                   plainTextBytes.Length);


        memoryStream.Close();
        cryptoStream.Close();


        string text = Encoding.UTF8.GetString(plainTextBytes,
                                                   0,
                                                   decryptedByteCount);


        return text;

    }

【问题讨论】:

  • 哈希函数不能加密/解密。您尝试的是使用PasswordDeriveBytes 中的哈希函数从密码中获取密钥。它使用 PBKDF1 的扩展。
  • 您的目标和代码令人困惑。您正在使用 PasswordDeriveBytes 派生要在 RijndaelManaged 中使用的密钥。它是一种对称算法,是 AES 竞赛的获胜者。请注意 AES !=Rijndael。你使用的是一个非常古老的图书馆。为了存储密码,我们没有加密它们,我们用盐对它们进行哈希处理。使用 PBKF2、Bcrypt、Scrypt 等密码哈希标准越好,新的赢家 Argon2 越好。

标签: c# node.js encryption sha1


【解决方案1】:

SHA1 是hash function。无法从哈希中获取原始数据(collisions 除外)。

您的问题不是哈希,而是加密/解密算法。尝试使用js-crypto-pbkdf from NPM

【讨论】:

  • 实际上,OP 混淆了这些术语。在 OP 的代码中,SHA1 用于 PasswordDeriveBytes,然后是 RijndaelManaged()。那是一个非常古老的库,并不完全是 AES。此外,密码没有加密,它们至少用盐进行了哈希处理。
  • 但我需要完全相同的结果,因为它是存储在数据库中的加密结果,我必须与之匹配。 @kelalaka
  • 我们使用良好的password hashing 算法对密码进行哈希处理。在散列时,我们添加盐以减轻彩虹表的影响。当用户尝试登录时,您会使用密码散列算法与用户的 salt 获取密码并比较结果。加密密码是unsafe。或搜索“加密密码”
猜你喜欢
  • 2013-05-11
  • 2010-09-06
  • 2011-09-15
  • 2017-05-02
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-07-12
相关资源
最近更新 更多