title: Crypto-Js with c#
body: <p>I am learning about encryption and using <strong>crypto-js</strong> I have made a <strong>Js &amp; c#</strong> version.
  What I am trying to accomplish is that the JS or c# version will be able to decode each other messages. </p>
  
  <p>For testing I have kept the <strong>IV</strong> and <strong>KEY</strong> , <strong>paddding</strong> and <strong>mode</strong> the same in both the JS and C# instance.</p>
  
  <p>I have them both decrypting and encrypting data respectivly but what I have yet to accomplish is providing an encrypted from JS be able to decode using c#.</p>
  
  <p>JS</p>
  
  <pre><code>var key = CryptoJS.enc.Base64.parse('7061737323313233'); 
  var iv = CryptoJS.enc.Base64.parse('7061737323313233'); 
  var encrypted = CryptoJS.AES.encrypt("It works", key, 
   { keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC,padding: CryptoJS.pad.Pkcs7 }); 
  
  var decrypted = CryptoJS.AES.decrypt(encrypted, key, { 
  keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); 
  
  document.write('Encrypted :' + encrypted + '&lt;br&gt;');
  document.write('Key :' + encrypted.key + '&lt;br&gt;');
  document.write('Salt :' + encrypted.salt + '&lt;br&gt;');
  document.write('iv :' + encrypted.iv + '&lt;br&gt;');
  document.write('Decrypted : ' + decrypted + '&lt;br&gt;');
  document.write('utf8 = ' + decrypted.toString(CryptoJS.enc.Utf8) + '&lt;br&gt;');
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>  public void startEncryption(string original )
          {
  
              using (RijndaelManaged myRijndael = new RijndaelManaged())
              {
                  //Settings
                  myRijndael.Mode = CipherMode.CBC;
                  myRijndael.Padding = PaddingMode.PKCS7;
                  myRijndael.FeedbackSize = 128;
  
                  keybytes = Encoding.UTF8.GetBytes("7061737323313233");
                  //Should be made unique for each message!. TODO
                  iv = Encoding.UTF8.GetBytes("7061737323313233");
  
                  // Encrypt the string to an array of bytes.
                  encrypted = EncryptStringToBytes(original, keybytes, iv);
  
                  //Show Encrypted data
                  txt_Output.Text = Convert.ToBase64String(encrypted);
  
                  // Decrypt the bytes to a string.
                  string roundtrip = DecryptStringFromBytes(encrypted, keybytes, iv);
  
                  //Display the original data and the decrypted data.
                  Console.WriteLine("Original:   {0}", original);
                  Console.WriteLine("Round Trip: {0}", roundtrip);
              }
  
  
          }
  </code></pre>
  
  <p>Where the problem arises in decryption.</p>
  
  <pre><code>  private void btn_Decrypt_Click(object sender, EventArgs e)
      {
          Console.WriteLine("Decrypting..");
          using (RijndaelManaged myRijndael = new RijndaelManaged())
          {
              //Settings
              myRijndael.Mode = CipherMode.CBC;
              myRijndael.Padding = PaddingMode.PKCS7;
              myRijndael.FeedbackSize = 128;
  
              keybytes = Encoding.UTF8.GetBytes("7061737323313233");
              //Should be made unique for each message!. TODO
              iv = Encoding.UTF8.GetBytes("7061737323313233");
  
              // Decrypt the bytes to a string.
              string roundtrip = DecryptToString(txt_Output.Text);
  
              txt_Output.Text = roundtrip;
              //Display the original data and the decrypted data.
  
          }
      }
  
    public string DecryptToString(string TextValue)
      {
  
          return DecryptStringFromBytes(Convert.FromBase64String(TextValue), keybytes, iv);
      }
  
  
           static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
      {
          // Check arguments.
          if (cipherText == null || cipherText.Length &lt;= 0)
              throw new ArgumentNullException("cipherText");
          if (Key == null || Key.Length &lt;= 0)
              throw new ArgumentNullException("Key");
          if (IV == null || IV.Length &lt;= 0)
              throw new ArgumentNullException("Key");
  
          // Declare the string used to hold
          // the decrypted text.
          string plaintext = null;
  
          // Create an RijndaelManaged object
          // with the specified key and IV.
          using (RijndaelManaged rijAlg = new RijndaelManaged())
          {
              rijAlg.Key = Key;
              rijAlg.IV = IV;
              rijAlg.Padding = PaddingMode.PKCS7;
              rijAlg.Mode = CipherMode.CBC;
  
              // Create a decrytor to perform the stream transform.
              ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
  
              // Create the streams used for decryption.
             using (MemoryStream msDecrypt = new MemoryStream(cipherText))
             {
           using (CryptoStream csDecrypt =
          new CryptoStream(msDecrypt,decryptor,CryptoStreamMode.Read))
                   {
                   using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                   {
  
                    // Read the decrypted bytes from the decrypting stream
                     // and place them in a string.
                    plaintext = srDecrypt.ReadToEnd();
                   }
               }
           }
  
         }
  
        return plaintext;
  
      }
  </code></pre>
  
  <p>I am producing different encrypted size strings:</p>
  
  <p>JS:MhAP11fHa+fUfRzSw2UHVQ==
  C#:+Ijpt1GDVgM4MqMAQUwf0Q==</p>
  
  <p>I get Padding is invalid and cannot be removed, when trying to decrypt the JS string in c#
  Where have I got wrong?.</p>