Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用node.js解密使用c加密的数据#_C#_Node.js_Encryption_Cryptography_Rijndael - Fatal编程技术网

C# 使用node.js解密使用c加密的数据#

C# 使用node.js解密使用c加密的数据#,c#,node.js,encryption,cryptography,rijndael,C#,Node.js,Encryption,Cryptography,Rijndael,我现在面临一个需要你们帮助的问题 我用c#做一些加密。然后需要使用node.js来声明它。但我只是发现,基于我的c#加密算法,我无法正确地进行加密。如果你们有什么解决办法,请帮帮我 这是我的c#加密代码: public static string Encrypt(string text, String password, string salt, string hashAlgorithm, int passwordIterations, string initialVector, int key

我现在面临一个需要你们帮助的问题

我用c#做一些加密。然后需要使用node.js来声明它。但我只是发现,基于我的c#加密算法,我无法正确地进行加密。如果你们有什么解决办法,请帮帮我

这是我的c#加密代码:

public static string Encrypt(string text, String password, string salt, string hashAlgorithm, int passwordIterations, string initialVector, int keySize)
{
    if (string.IsNullOrEmpty(text))
        return "";

    var initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
    var saltValueBytes = Encoding.ASCII.GetBytes(salt);
    var plainTextBytes = Encoding.UTF8.GetBytes(text);
    var derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
    var keyBytes = derivedPassword.GetBytes(keySize / 8);
    var symmetricKey = new RijndaelManaged();
    symmetricKey.Mode = CipherMode.CBC;
    byte[] cipherTextBytes = null;
    using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes))
    {
        using (var memStream = new MemoryStream())
        {
            using (var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
            {
                cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                cryptoStream.FlushFinalBlock();
                cipherTextBytes = memStream.ToArray();
                memStream.Close();
                cryptoStream.Close();
            }
        }
    }
    symmetricKey.Clear();
    return Convert.ToBase64String(cipherTextBytes);
}
public static string Decrypt(string text, String password, string salt, string hashAlgorithm, int passwordIterations, string initialVector, int keySize)
{
    if (string.IsNullOrEmpty(text))
        return "";

    var initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
    var saltValueBytes = Encoding.ASCII.GetBytes(salt);
    var cipherTextBytes = Convert.FromBase64String(text);
    var derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
    var keyBytes = derivedPassword.GetBytes(keySize / 8);
    var symmetricKey = new RijndaelManaged();
    symmetricKey.Mode = CipherMode.CBC;
    var plainTextBytes = new byte[cipherTextBytes.Length];
    var byteCount = 0;
    using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
    {
        using (var memStream = new MemoryStream(cipherTextBytes))
        {
            using (var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
            {

                byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                memStream.Close();
                cryptoStream.Close();
            }
        }
    }
    symmetricKey.Clear();
    return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);    
}

如果有人能为nodejs提供相同的功能,那将非常有帮助。无论如何,感谢您阅读本文。

首先,我们将使用Node.js执行
PasswordDeriveBytes
函数。这是:

接下来,使用创建解密器:

// find algorithm from the available ciphers; see crypto.getCiphers()
var decipher = crypto.createDecipheriv(/* algorithm */, key, initialVector);

然后使用
decipher.update
decipher.final
向其提供数据。他们会将解密数据的一部分返回给您。

有着完全相同的场景,我能够使用上面@icktoofay提供的代码获得成功的“C#encrypt=>Node decrypt”解决方案,但是Rfc2898DeriveBytes替换了PasswordDeriveBytes

我的代码大致如下:

C#

实际上是我在互联网上找到的几个例子的组合


因为在某些特定情况下(编码字符串开头的“+”符号),我在缓冲区中实现Base64时遇到了问题,所以我使用了Base64 js from,这似乎很好。

查看语法突出显示。这就是为什么你们应该遵循惯例,不要使用大写的变量名。必须清理一下@JonathonReinhart,太痛苦了:)对不起,伙计们,我下次一定会清理干净的。谢谢你清理我的代码。你试过使用吗?是的,我试过。我试过:
crypto.pbkdf2(密码,salt,迭代,键值/8,函数(err,key){var decipher=crypto.createDecipheriv('aes-128-cbc',key,initialVector);var dec=decipher.update(text,'base64',utf8')+decipher.final('utf8');})不工作,我发现有一个“hashAlgorithm”我在c#中使用过,但在Node中没有。有什么意见吗?我已经试过了:
crypto.pbkdf2(密码,salt,迭代,keySize/8,函数(err,key){var decipher=crypto.createDecipheriv('aes-128-cbc',key,initialVector);var dec=decipher.update(text,'base64',utf8')+decipher.final('utf8');})不工作,我发现有一个“hashAlgorithm”我在c#中使用过,但在Node中没有。有意见吗?@Zhe:对于PBKDF2,Node.js只支持HMAC-SHA1。
// find algorithm from the available ciphers; see crypto.getCiphers()
var decipher = crypto.createDecipheriv(/* algorithm */, key, initialVector);
private byte[] saltBytes = ASCIIEncoding.ASCII.GetBytes(salt);

public string Encrypt<T>(string value, string password) where T: SymmetricAlgorithm, new() {
  byte[] valueBytes = UTF8Encoding.UTF8.GetBytes(value);

  byte[] encrypted = null;
  using (T cipher = new T()) {
    var db = new Rfc2898DeriveBytes(password, saltBytes);
    db.IterationCount = iterationsConst;
    var key = db.GetBytes(keySizeConst / 8);

    cipher.Mode = CipherMode.CBC;

    using (ICryptoTransform encryptor = cipher.CreateEncryptor(key, vectorBytes)) {
      using (MemoryStream ms = new MemoryStream()) {
        using (CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write)) {
          cs.Write(valueBytes, 0, valueBytes.Length);
          cs.FlushFinalBlock();
          encrypted = ms.ToArray();
        }
      }
    }
    cipher.Clear();
  }
  return Convert.ToBase64String(encrypted);
}
var crypto = require('crypto');
var base64 = require('base64-js');
var algorithm = 'AES-256-CBC';

[...]

var saltBuffer = new Buffer(salt);
var passwordBuffer = new Buffer(password);

[...]

var encodedBuffer = new Buffer(base64.toByteArray(encryptedStringBase64Encoded));

crypto.pbkdf2(passwordBuffer, saltBuffer, iterations, keySize / 8, function(err, key) {
  var decipher = crypto.createDecipheriv(algorithm, key, iv);
  var dec = Buffer.concat([decipher.update(encodedBuffer), decipher.final()]);
  return dec;
});