繁体   English   中英

如何在已经在 .net C# 中实现的 nodejs 中转换 SHA1 哈希 function

[英]How can convert SHA1 Hashing function in nodejs that already implemented in .net C#

我是nodejs的初学者。 我已经通过sha1实现了加解密,并在asp.net项目中使用。 现在我们在节点和 angular 中启动了新项目。 这里我需要相同的登录机制,包括使用 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;

    }

SHA1 是hash function 无法从 hash 获取原始数据( 碰撞除外)。

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

暂无
暂无

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

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