title: Rijndael cryptography not working on Windows Server 2012
body: <p>I have a Windows Application (x64) that has been working fine on Winodws 7, 8 and now 10. Today we failed at running the program under a <strong>Windows 2012 Server</strong>. When we looked at the event log, we found an error originating from <code>System.Security.Cryptography.RijndaelManaged..ctor()</code> (unfortunately the log did not give us the complete path).</p>
  
  <p>I have used the <em>Rijndael</em> algorithm to for encrypting sensitive data in my program. The first thing the program does is to retrieve the encrypted config file and decrypt it in order to get all the settings. And this is where my program does not start.</p>
  
  <p>This is the decrypt method in my program:</p>
  
  <pre><code>public static string Decrypt(string cipherText, string passPhrase)
  {
      byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
      using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
      {
          byte[] keyBytes = password.GetBytes(keysize / 8);
          using (RijndaelManaged symmetricKey = new RijndaelManaged())
          {
              symmetricKey.Mode = CipherMode.CBC;
              using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
              {
                  using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                  {
                      using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                      {
                          byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                          int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                          return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And this is the error message I get in the log:</p>
  
  <blockquote>
    <p>Application: Postbag.exe Framework Version: v4.0.30319 Description:
    The process was terminated due to an unhandled exception. Exception
    Info: System.InvalidOperationException at
    System.Security.Cryptography.RijndaelManaged..ctor() at
    Common.StringCipher.Decrypt(System.String, System.String) at
    Common.Conf..cctor() Exception Info:
    System.TypeInitializationException at Common.Conf.get_DataProvider()
    at Postbag.FormMain..ctor() at Postbag.Program.Main()</p>
  </blockquote>
  
  <p>The new server also has the same versions of the .Net framework.</p>