与javascript中的Rfc2898DeriveBytes等效?

与javascript中的Rfc2898DeriveBytes等效?,javascript,node.js,cryptography,pbkdf2,cryptojs,Javascript,Node.js,Cryptography,Pbkdf2,Cryptojs,我见过C#代码,它可以使用如下代码对密码进行加密和解密: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Security.Cryptography; 使用System.IO; 运用系统反思; 名称空间SilverlightPhone数据库 { /// ///用于加密数据库的类 /// 公共静态类密码学 { /// ///使用提供的密码输入 /// ///输入要加密的字符串 ///要使用的密码 //

我见过C#代码,它可以使用如下代码对密码进行加密和解密:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Security.Cryptography;
使用System.IO;
运用系统反思;
名称空间SilverlightPhone数据库
{
/// 
///用于加密数据库的类
/// 
公共静态类密码学
{
/// 
///使用提供的密码输入
/// 
///输入要加密的字符串
///要使用的密码
///加密字符串
公共静态字符串加密(字符串输入、字符串密码)
{
字符串数据=输入;
字节[]utfdata=UTF8Encoding.UTF8.GetBytes(数据);
byte[]saltBytes=UTF8Encoding.UTF8.GetBytes(密码);
//我们的对称加密算法
aes管理aes=新aes管理();
//我们使用PBKDF2标准生成基于密码的密钥
Rfc2898DeriveBytes rfc=新的Rfc2898DeriveBytes(密码,saltBytes);
//设置参数
aes.BlockSize=aes.LegalBlockSize[0].MaxSize;
aes.KeySize=aes.LegalKeySizes[0].MaxSize;
aes.Key=rfc.GetBytes(aes.KeySize/8);
aes.IV=rfc.GetBytes(aes.BlockSize/8);
//加密
ICryptoTransform encryptranf=aes.CreateEncryptor();
//输出流,也可以是文件流
MemoryStream encryptStream=新的MemoryStream();
CryptoStream encryptor=新加密流(encryptStream、encryptTransf、CryptoStreamMode.Write);
加密机。写入(utfdata,0,utfdata.Length);
encryptor.Flush();
encryptor.Close();
byte[]encryptBytes=encryptStream.ToArray();
string encryptedString=Convert.ToBase64String(encryptBytes);
返回encryptedString;
}
/// 
///使用提供的密码解密字符串
/// 
///解密输入
///要使用的密码
///解密字符串
公共静态字符串解密(字符串base64输入,字符串密码)
{
byte[]encryptBytes=Convert.FromBase64String(base64Input);
byte[]saltBytes=Encoding.UTF8.GetBytes(密码);
//我们的对称加密算法
aes管理aes=新aes管理();
//我们使用PBKDF2标准生成基于密码的密钥
Rfc2898DeriveBytes rfc=新的Rfc2898DeriveBytes(密码,saltBytes);
//设置参数
aes.BlockSize=aes.LegalBlockSize[0].MaxSize;
aes.KeySize=aes.LegalKeySizes[0].MaxSize;
aes.Key=rfc.GetBytes(aes.KeySize/8);
aes.IV=rfc.GetBytes(aes.BlockSize/8);
//现在,解密
ICryptoTransform decryptTrans=aes.CreateDecryptor();
//输出流,也可以是文件流
MemoryStream decryptStream=新的MemoryStream();
CryptoStream Decryptoctor=新加密流(DecryptoStream、DecryptoTrans、CryptoStreamMode.Write);
解密程序.Write(encryptBytes,0,encryptBytes.Length);
decryptor.Flush();
decryptor.Close();
byte[]decryptBytes=decryptStream.ToArray();
string decryptedString=UTF8Encoding.UTF8.GetString(decryptBytes,0,decryptBytes.Length);
返回解密字符串;
}
}
}
我不是安全专家,在加密算法方面经验有限。我有一个用这种代码加密的加密密码,现在想在node.js程序(Javascript)中访问解密后的密码

似乎
crypto js
有一个
pbkdf2.js
模块,但它只知道如何加密密码

我见过,但它似乎只是一个加密机。没有解密


任何人都可以提供解密密码的代码,给定已知的salt和用于加密密码的迭代,使用普通Javascript,最好利用诸如
crypto js
之类的通用模块?

PBKDF是基于密码的密钥派生函数。PBKDF不是加密算法。可以将它们与单向安全散列算法进行比较,使用salt(使相同密码的输出唯一)和迭代计数(使它们变慢)。许多PBKDF,如您问题中的PBKDF2,实际上是使用SHA-1等散列算法实现的

PBKDF函数通常用于在密码上生成唯一标识符。该标识符将具有生成的密钥材料的所有属性,包括在没有暴力攻击的情况下无法检索输入材料的属性。换句话说,您无法解密密码-您只能尝试每个可能的密码,以查看PBKDF2的输出是否匹配

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Reflection;

namespace SilverlightPhoneDatabase
{
    /// <summary>
    /// Class used to encrypt the database
    /// </summary>
    public static class Cryptography
    {

        /// <summary>
        /// Incrypt the input using password provided
        /// </summary>
        /// <param name="input">Input string to encrypt</param>
        /// <param name="password">Password to use</param>
        /// <returns>Encrypted string</returns>
        public static string Encrypt(string input, string password)
        {

            string data = input;
            byte[] utfdata = UTF8Encoding.UTF8.GetBytes(data);
            byte[] saltBytes = UTF8Encoding.UTF8.GetBytes(password);



            // Our symmetric encryption algorithm
            AesManaged aes = new AesManaged();

            // We're using the PBKDF2 standard for password-based key generation
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, saltBytes);

            // Setting our parameters
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // Encryption
            ICryptoTransform encryptTransf = aes.CreateEncryptor();

            // Output stream, can be also a FileStream
            MemoryStream encryptStream = new MemoryStream();
            CryptoStream encryptor = new CryptoStream(encryptStream, encryptTransf, CryptoStreamMode.Write);

            encryptor.Write(utfdata, 0, utfdata.Length);
            encryptor.Flush();
            encryptor.Close();

            byte[] encryptBytes = encryptStream.ToArray();
            string encryptedString = Convert.ToBase64String(encryptBytes);

            return encryptedString;
        }

        /// <summary>
        /// Decrypt string using password provided
        /// </summary>
        /// <param name="base64Input">Input to decrypt</param>
        /// <param name="password">Password to use</param>
        /// <returns>Decrypted string</returns>
        public static string Decrypt(string base64Input, string password)
        {

            byte[] encryptBytes = Convert.FromBase64String(base64Input);
            byte[] saltBytes = Encoding.UTF8.GetBytes(password);

            // Our symmetric encryption algorithm
            AesManaged aes = new AesManaged();

            // We're using the PBKDF2 standard for password-based key generation
            Rfc2898DeriveBytes rfc = new Rfc2898DeriveBytes(password, saltBytes);

            // Setting our parameters
            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes(aes.KeySize / 8);
            aes.IV = rfc.GetBytes(aes.BlockSize / 8);

            // Now, decryption
            ICryptoTransform decryptTrans = aes.CreateDecryptor();

            // Output stream, can be also a FileStream
            MemoryStream decryptStream = new MemoryStream();
            CryptoStream decryptor = new CryptoStream(decryptStream, decryptTrans, CryptoStreamMode.Write);

            decryptor.Write(encryptBytes, 0, encryptBytes.Length);
            decryptor.Flush();
            decryptor.Close();

            byte[] decryptBytes = decryptStream.ToArray();
            string decryptedString = UTF8Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);
            return decryptedString;
        }
    }
}