Search code examples
javascriptc#node.jsencryptionaes

Reproduce AES decryption method from C# in JavaScript


I am trying to reproduce the following C# decryption method in JavaScript.

This method is used to decrypt short strings: names, addresses, email addresses, etc.

It feels tantalisingly close, because the strings I have been able to "successfully" decrypt seem partially decrypted.

For instance, some of the emails look like this: x"R�Îd¹1gtWÈ2)web@example.com

CSharp

public static readonly byte[] INIT_VECTOR = { 0x00, 0x00, ... };

public static string Decrypt(string cipherText) {

  string EncryptionKey = "Some Encryption Key";

  byte[] cipherBytes = Convert.FromBase64String(cipherText);

  using (Aes encryptor = Aes.Create())
  {
​
    Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(EncryptionKey, INIT_VECTOR);

    encryptor.Key = pdb.GetBytes(32);
    encryptor.IV = pdb.GetBytes(16);

    using (MemoryStream ms = new MemoryStream())
    {
      using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
      {
        cs.Write(cipherBytes, 0, cipherBytes.Length);
        cs.Close();
      }
      cipherText = Encoding.Unicode.GetString(ms.ToArray());
    }
  }

  return cipherText;
}

JavaScript

import atob from 'atob';
import forge from 'node-forge';

const InitVector = [0x00, ...];
const EncryptionKey = 'Some Encryption Key';

const iv = Buffer.from(InitVector).toString();

const convertBase64StringToUint8Array = input => {
  const data = atob(input);
  const array = Uint8Array.from(data, b => b.charCodeAt(0));

  return array;
};

const decrypt = cipher => {
  const cipherArray = convertBase64StringToUint8Array(cipher);

  const key = forge.pkcs5.pbkdf2(EncryptionKey, iv, 1000, 32);

  const decipher = forge.cipher.createDecipher('AES-CBC', key);

  decipher.start({ iv });

  decipher.update(forge.util.createBuffer(cipherArray, 'raw'));

  const result = decipher.finish();

  if (result) {
    return decipher.output.data;
  } else {
    return false;
  }
};

Solution

  • Thanks to kelalaka I managed to figure this out!

    This was the code I ended up with.

    import atob from 'atob';
    import forge from 'node-forge';
    
    const InitVector = [0x00, ...];
    const EncryptionKey = 'Some Encryption Key';
    
    const initKey = Buffer.from(InitVector).toString(); // Changed this to `initKey`
    
    const convertBase64StringToUint8Array = input => {
      const data = atob(input);
      const array = Uint8Array.from(data, b => b.charCodeAt(0));
    
      return array;
    };
    
    const decrypt = cipher => {
      const cipherArray = convertBase64StringToUint8Array(cipher);
    
      const key = forge.pkcs5.pbkdf2(EncryptionKey, iv, 1000, 32);
    
      /**
       * Added the following
       * Note the key size = 48
       *  This was due to the fact that the C# dictated that
       *  the IV was 16 bytes, starting at the end of the key.
       */
      const keyAndIV = forge.pkcs5.pbkdf2(encryptionKey, initKey, 1000, 32 + 16);
    
      /**
       * Therefore, we cut the iv from the new string
       */
      const iv = keyAndIV.slice(32, 32 + 16); // 16 bytes
    
      const decipher = forge.cipher.createDecipher(
        'AES-CBC',
        forge.util.createBuffer(key)
      );
    
      decipher.start({ iv });
    
      decipher.update(forge.util.createBuffer(cipherArray, 'raw'));
    
      const result = decipher.finish();
    
      if (result) {
        return decipher.output.data;
      } else {
        return false;
      }
    };