如何将c#System.Security.Cryptography迁移到javascript nodejs?

时间:2016-06-28 04:15:03

标签: javascript c# node.js

我的公司有此功能来加密或解密任何字符串。但现在他们想要使用nodejs进行迁移。如何将此System.Security.Cryptography代码或函数迁移到nodejs?

#region Secure
    private static readonly byte[] initVectorBytes = Encoding.ASCII.GetBytes("tu89geji340t89u2");
    private const int keysize = 256;
    private const int saltLength = 10;
    private static String Encrypt(String toEncrypt, String salt)
    {
        byte[] plainTextBytes = Encoding.UTF8.GetBytes(toEncrypt);
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(salt, null))
        {
            byte[] keyBytes = password.GetBytes(keysize / 8);
            using (RijndaelManaged symmetricKey = new RijndaelManaged())
            {
                symmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
                {
                    using (MemoryStream memoryStream = new MemoryStream())
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            byte[] cipherTextBytes = memoryStream.ToArray();
                            return Convert.ToBase64String(cipherTextBytes);
                        }
                    }
                }
            }
        }
    }

    private static String Decrypt(String toDecrypt, String salt)
    {
        byte[] cipherTextBytes = Convert.FromBase64String(toDecrypt);
        using (PasswordDeriveBytes password = new PasswordDeriveBytes(salt, null))
        {
            byte[] keyBytes = password.GetBytes(keysize / 8);
            using (RijndaelManaged symmetricKey = new RijndaelManaged())
            {
                symmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
                {
                    using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                    {
                        using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                        {
                            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                        }
                    }
                }
            }
        }
    }

    private static String MakeSalt(int size)
    {
        char[] chars = new char[62];
        string a;
        a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        chars = a.ToCharArray();
        byte[] data = new byte[1];
        RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
        crypto.GetNonZeroBytes(data);
        data = new byte[size];
        crypto.GetNonZeroBytes(data);
        StringBuilder result = new StringBuilder(size);
        foreach (byte b in data)
        { result.Append(chars[b % (chars.Length - 1)]); }
        return result.ToString();

    }
    #endregion

我很抱歉,我不知道该怎么做,所以这是我尝试过的唯一一次:

var vector = 'tu89geji340t89u2';
var initVectorBytes = [];
for (var i = 0; i < vector.length; ++i) {
    initVectorBytes.push(vector.charCodeAt(i));
}
var keysize = 256;
var saltLength = 10;

function Encrypt(toEncrypt, salt){
    var plainTextBytes = [];
    for (var i = 0; i < toEncrypt.length; ++i) {
        plainTextBytes.push(toEncrypt.charCodeAt(i));
    }
}


function Decrypt(toDecrypt, salt){

}

function MakeSalt(int size){
    var a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
}

2 个答案:

答案 0 :(得分:0)

使用此模块。它实现了rijndael算法

https://www.npmjs.com/package/node-rijndael

答案 1 :(得分:0)

使用加密模块。这是一个非常受欢迎的模块,平均每天下载量不超过10K。

var crypto = require('crypto'),
    ENCRYPT_ALGO = 'aes-256-ctr',
    ENCRYPT_PASSWORD = 'somesecret';

function encrypt(text) {
  var cipher = crypto.createCipher(ENCRYPT_ALGO, ENCRYPT_PASSWORD);
  var crypted = cipher.update(text, 'utf8', 'hex')
  crypted += cipher.final('hex');
  return crypted;
};

function decrypt(text) {
  var dec = '';
  var decipher = crypto.createDecipher(ENCRYPT_ALGO, ENCRYPT_PASSWORD)
  try {
      dec = decipher.update(text, 'hex', 'utf8');
      dec += decipher.final('utf8');
  } catch (e) {
      log.error('decrypt failed for text:' + text);
  } finally {
      return dec;
  }
}