title: Converting C# Cryptography.Rijndael encryption to Javascript (pref. crypto-js)
body: <p>I have two system which are sharing a user database, so authentication needs to be the same.</p>
  
  <p>The passwords are currently encrypted using C#'s <code>Cryptography.Rijndael</code>(N.B. <strong>not</strong> RijndaelManaged). Using a custom key and iv (initialisation vector). (CBC mode and Pkcs7 padding)</p>
  
  <p>The C# encryption is as follows:</p>
  
  <pre><code>Rijndael alg = Rijndael.Create();
  alg.Key = key;
  alg.IV = IV;
  CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
  cs.Write(clearData, 0, clearData.Length);
  cs.Close();
  byte[] encryptedData = ms.ToArray();
  </code></pre>
  
  <p><code>key</code> is 256 bits (32 bytes) and <code>iv</code> (initialisation vector) is 128 bits (16 bytes). The block size is 128 bits (16 bytes).</p>
  
  <p>key and iv are a byte arrays from a base64 strings via: </p>
  
  <pre><code>byte[] key = Convert.FromBase64String(base64Key);
  byte[] iv = Convert.FromBase64String(base64IV);
  </code></pre>
  
  <p>N.B. I have no control on the C# code (legacy system).</p>
  
  <p>On the javascript based system I have to encrypt the passwords exactly the same way. I've tried using node <code>crypto-js</code> to no avail.
  my code looks something like this:</p>
  
  <pre><code>var encrypted = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse(password), keyCodeWords, {
      iv: ivCodeWords,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
  });
  </code></pre>
  
  <p>keyCodeWords and ivCodeWords are CryptoJS code words from the same base64 key and iv as follows:</p>
  
  <pre><code>var keyCodeWords = CryptoJS.enc.Base64.parse(base64Key);
  var ivCodeWords = CryptoJS.enc.Base64.parse(base64IV);
  </code></pre>
  
  <p>The encryption does work (in the sense that I can encrypt and then decrypt to get the same value). However, the issue is that the encrypted value (<code>encrypted.ciphertext</code>) is not the same as the C# one, so I'm unable to authenticate.</p>
  
  <p>The comparison is done base comparing the base64 encrypted output strings.</p>
  
  <p><strong>How do I make crypto-js (or any other javascript encryption library) consistent with C# Rijndael?</strong></p>