1

I barely have any knowledge on c#. I have a scenario where I have encryption and decryption in c# as follows. But, I want to implement same decryption in NodeJS. Can anyone help me in figuring this out.

Here's the salt and secret keys

private static byte[] _salt = Encoding.ASCII.GetBytes("SampleSaltHere");
private string secret = "SecretHere";

Encryption Part

public string Encrypt(string plainText)
{
    string outStr = null;
    Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(secret, _salt);
    RijndaelManaged  aesAlg = new RijndaelManaged();

    aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
    ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

    using (MemoryStream msEncrypt = new MemoryStream())
    {
        msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
        msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
        {
            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
            {
                swEncrypt.Write(plainText);
            }
        }
        outStr = Convert.ToBase64String(msEncrypt.ToArray());
    }
    return outStr;
}

Decryption Part which I want to implement in NodeJS.

public string Decrypt(string cipherText)
{
    string plaintext = null;
    Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(secret, _salt);
    byte[] bytes = Convert.FromBase64String(cipherText);

    using (MemoryStream msDecrypt = new MemoryStream(bytes))
    {
        RijndaelManaged aesAlg = new RijndaelManaged();
        aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
        aesAlg.IV = ReadByteArray(msDecrypt);

        ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
        {
            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
            plaintext = srDecrypt.ReadToEnd();
        }
    }
    return plaintext;
}

private static byte[] ReadByteArray(Stream s)
{
    byte[] rawLength = new byte[sizeof(int)];
    byte[] buffer = new byte[BitConverter.ToInt32(rawLength, 0)];

    return buffer;
}

Any help is highly appreciated. Thank you.

prashanth
  • 19
  • 4
  • not pretty clear from the code exact encryption algorithm you are using, but you can try the code as described in Crypto cipheriv, https://nodejs.org/api/crypto.html#crypto_class_cipher – Neo Mar 14 '19 at 20:27
  • 1
    Got it, you are using [rjindael](https://searchsecurity.techtarget.com/definition/Rijndael) algo, try these npm projects: [js-rjindael](https://www.npmjs.com/package/js-rijndael) – Neo Mar 14 '19 at 20:31
  • Thank you @Neo. Will try using js-rjindael and will update you. – prashanth Mar 14 '19 at 20:41
  • @Neo I tried using js-rjindael but not luck. No sure what to use in place of key and IV. tried with salt and secret key. But did not work. Any more suggestions? – prashanth Mar 14 '19 at 20:55
  • You might would have to read in detail how rjindael work, but you can also try with this npm module [mcrypt](https://www.npmjs.com/package/mcrypt), it do specify way to [generateIV](https://www.npmjs.com/package/mcrypt#generateiv--buffer) or check if a particular model/algo [hasIV](https://www.npmjs.com/package/mcrypt#hasiv--boolean) – Neo Mar 14 '19 at 21:12
  • instead of redoing it...can you just have a service that nodejs calls...microservices is always an option...you don't have to reprogram everything? – Ctznkane525 Mar 14 '19 at 22:37
  • @Ctznkane525 the only code I have in c# is the above mentioned decryption. Rest all I have in NodeJS. Do you still think it's a good idea to add a microservice for this on alone? – prashanth Mar 18 '19 at 15:06
  • @prashanth - no – Ctznkane525 Mar 18 '19 at 17:57
  • rather than converting c# code to NodeJS , I'm using child_process in nodejs to run c# file. Do you guys think will there be any issues with it? @Ctznkane525 any comments? – prashanth Mar 19 '19 at 14:17
  • nodejs is intended to be very very lightweight...so running a process defeats one of its key benefits – Ctznkane525 Mar 19 '19 at 14:38

0 Answers0