C# 如何在已经在.NETC中实现的NodeJ中转换SHA1哈希函数#

C# 如何在已经在.NETC中实现的NodeJ中转换SHA1哈希函数#,c#,node.js,encryption,sha1,C#,Node.js,Encryption,Sha1,我是nodejs的初学者。我已经通过sha1实现了加密和解密,并在asp.net项目中使用。现在,我们开始了节点和角度的新项目。这里我需要相同的登录机制,包括使用sha1进行加密和解密 以下是我的可行代码: 因变量必须为我所需 加密密码或任何文本的加密方法 public static string EncryptText(string text) { byte[] initVectorBytes = Encoding.ASCII.GetBytes(ini

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

以下是我的可行代码:

因变量必须为我所需

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

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 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;
        }
SHA1是。无法从散列中获取原始数据(除非)


你的问题不是散列,而是加密/解密算法。尝试使用。

哈希函数无法加密/解密。您尝试的是使用中的哈希函数从密码中派生密钥。它使用了PBKDF1的扩展。您的目标和代码令人困惑。您正在使用PasswordDeriveBytes派生要在RijndaelManaged中使用的密钥。这是一种对称算法,在AES竞赛中获胜。注意,AES=瑞恩代尔。你使用的是一个非常古老的图书馆。对于存储密码,我们不是加密它们,而是用盐将它们散列。更好地使用密码哈希标准,如PBKF2、Bcrypt、Scrypt,更好地使用新的获胜者Argon2。实际上,OP混淆了术语。在OP的代码中,SHA1用于PasswordDeriveBytes,然后用于RijndaelManaged()。那是一个非常古老的图书馆,不完全是AES。另外,密码不是加密的,它们至少是用盐散列的。但我需要完全相同的结果,因为它是存储在数据库中的加密结果,我必须与此匹配@kelalakaWe用一个好的密码哈希算法。在散列时,我们添加盐以减轻Rainbow表的影响。当用户尝试登录时,您可以使用密码哈希算法和用户的salt获取密码,并比较结果。对密码进行加密是非常困难的。或搜索“加密密码”
    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;

    }