[英]How can convert SHA1 Hashing function in nodejs that already implemented in .net C#
I am beginner in nodejs.我是nodejs的初学者。 I have already implemented encryption and decryption through sha1 and using in asp.net projects.我已经通过sha1实现了加解密,并在asp.net项目中使用。 Now we started new project in node and angular.现在我们在节点和 angular 中启动了新项目。 Here i need same login mechanism including encryption and decryption using sha1.这里我需要相同的登录机制,包括使用 sha1 的加密和解密。
Here is my workable code:这是我的可行代码:
Dependent variable must need for me我必须需要因变量
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;
Encryption method to encrypt password or any text.加密密码或任何文本的加密方法。
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;
}
Decryption method to encrypt password or any text.加密密码或任何文本的解密方法。
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 is hash function . SHA1 是hash function 。 It's no way to get original data from hash (except collisions ).无法从 hash 获取原始数据( 碰撞除外)。
Your problem is not a hash, it's encrypt/decrypt algorithm.您的问题不是 hash,而是加密/解密算法。 Try to use js-crypto-pbkdf from NPM .尝试使用NPM 中的 js-crypto-pbkdf 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.