0

I have some text encrypted in C#/.NET which I want to decrypt in Node.js. I have the decryption code that works perfectly fine in C#, but I am not able to find code that decrypts the same encrypted text in Node.js. Below mentioned is the code in .NET for decryption

public static string Decrypt(string encodedText, string key)
{
    TripleDESCryptoServiceProvider desCryptoProvider = new TripleDESCryptoServiceProvider();
    byte[] byteBuff;
    try
    {
        byteBuff = Convert.FromBase64String(encodedText);
        desCryptoProvider.Key = UTF8Encoding.UTF8.GetBytes(key);
        desCryptoProvider.Mode = CipherMode.ECB;
        desCryptoProvider.Padding = PaddingMode.PKCS7;
        string plaintext = Encoding.UTF8.GetString(desCryptoProvider.CreateDecryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
        return plaintext;
    }
    catch (Exception except)
    {
        Console.WriteLine(except + "\n\n" + except.StackTrace);
        return null;
    }
}

The Node.js built-in library crypto did not help me a great deal as it errored out with the encrypted text. When I used 'Crypto-JS' package from npm, it did not error out, however, it did not give any output either. Below is the Node.js equivalent

function decrypt(input, key) {
   var key = CryptoJS.enc.Utf8.parse(key);
   var iv = CryptoJS.enc.Base64.parse('QUJDREVGR0g=');
   var options = {
       mode: CryptoJS.mode.ECB,
       padding: CryptoJS.pad.Pkcs7
   };
   var bytes = CryptoJS.TripleDES.decrypt(input, key, options);
   console.log('key:' + key);
   var decryptedText = bytes.toString(CryptoJS.enc.Utf8);
   console.log('DecryptedText:' + decryptedText);
   return decryptedText;
}

How do I find out the Node.js equivalent of the above code?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anee
  • 463
  • 9
  • 26

0 Answers0