使用node.js解密使用c# [英] Using node.js to decrypt data encrypted using c#

查看:189
本文介绍了使用node.js解密使用c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面对一个需要你帮助的问题。

I'm facing a issue now which need you guys help.

我使用c#做一些加密。然后需要使用node.js来解密它。但我刚刚发现,我不能做正确基于我的c#加密算法。如果你们有任何解决方案,请帮助我。

I use c# to do some encryption. Then need to use node.js to decry-pt it. But I just found that I can't do it correctly base on my c# encryption algorithm. If you guys have any solution, please help me.

这是我的c#加密代码:

Here is my c# encryption code:

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提供一个相同的函数, 。无论如何,感谢您阅读这篇文章。

If anyone can give me a same function for nodejs, that's will be really help. Anyway, thanks for read this post.

推荐答案

首先,我们将 PasswordDeriveBytes 函数。这是 crypto.pbkdf2

First, we'll do the PasswordDeriveBytes function with Node.js. This is crypto.pbkdf2:

// assumes HMAC-SHA1
crypto.pbkdf2(password, salt, iterations, keySize / 8, function(err, key) {
    if(err) /* handle error */
    // ...
});

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

Next, use crypto.createDecipheriv to create a decryptor:

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

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

Then use decipher.update and decipher.final to feed it the data. They will return portions of decrypted data to you.

这篇关于使用node.js解密使用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆