title: Thread Safety of .NET Encryption Classes?
body: <p>I have a high-level goal of creating a <strong>static</strong> utility class that encapsulates the encryption for my .NET application.  Inside I'd like to minimize the object creations that aren't necessary.</p>
  
  <p>My question is: <strong>what is the thread-safety of the classes which implement symmetric encryption within the .NET Framework?</strong>  Specifically <code>System.Security.Cryptography.RijndaelManaged</code> and the <code>ICryptoTransform</code> types it generates.</p>
  
  <p>For instance, in my class constructor can I simply do something along the following lines?</p>
  
  <pre><code>static MyUtility()
  {
      using (RijndaelManaged rm = new RijndaelManaged())
      {
          MyUtility.EncryptorTransform = rm.CreateEncryptor(MyUtility.MyKey, MyUtility.MyIV);
          MyUtility.DecryptorTransform = rm.CreateDecryptor(MyUtility.MyKey, MyUtility.MyIV);
      }
  }
  </code></pre>
  
  <p><em>Side-stepping the issue of is it secure to have Key and IV exist within this class</em>, this example block brings up a number of other questions:</p>
  
  <ol>
  <li><p>Can I continually reuse the EncryptorTransform and DecryptorTransform over and over? The <code>*.CanReuseTransform</code> and <code>*.CanTransformMultipleBlocks</code> properties imply "yes", but are there any caveats I should be aware of?</p></li>
  <li><p>Since <code>RijndaelManaged</code> implements <code>IDisposable</code> my inclination is to put it within a <code>using</code> block especially since it probably ties into external OS-level libs.  Are there any caveats with this since I'm keeping the <code>ICryptoTransform</code> objects around?</p></li>
  <li><p>Potentially the most important question, in a highly multithreaded environment, will I run into issues with sharing the <code>ICryptoTransform</code> objects between threads?</p></li>
  <li><p>If the answer to #3 is that it isn't thread-safe, will I experience serious performance degradation from locking while I use the <code>ICryptoTransform</code> objects? (Depends on load I suppose.)</p></li>
  <li><p>Would it be more performant to simply instantiate new <code>RijndaelManaged</code> each time? Or store one <code>RijndaelManaged</code> and generate <code>new RijndaelManaged().CreateEncryptor(...)</code> each time?</p></li>
  </ol>
  
  <p>I am hoping that someone out there knows how these work under the hood or are experienced with issues from similar implementations.  I've found that a lot of these kinds of performance and thread-related issues typically do not manifest themselves until there is a sizable amount of load.</p>
  
  <p>Thanks!</p>