title: C# TripleDES Provider without an Initialization Vector?
body: <p>I have a set of encrypted documents encoded with TripleDES coming from a remote system. I need to decode the data in C# and I have no control over the key or encoding algorithm. All I have is the key and the mode (CBC) and the data located in a file.</p>
  <p>The TripleDESCryptoServiceProvider is easy enough to use, but I can't figure out how to use the Decryptor without an Initialization Vector.</p>
  <p>We have a have 24 byte (192bit) key to decrypt with, but nothing else.</p>
  <pre><code>   string key = &quot;1468697320656E6372797174696F6E206973737265206933&quot;;            
     byte[] keyData = ParseHex(key);  //  key is OK at 24 bytes                     
  
     TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
     des.Mode = CipherMode.CBC;            
     des.GenerateIV();
  
     var decryptor = des.CreateDecryptor(keyData,null);  // des.IV
          
     var encoded = File.ReadAllBytes(@&quot;..\..\..\..\test.tdes&quot;);
     byte[] output = decryptor.TransformFinalBlock(encoded, 0, encoded.Length);
  </code></pre>
  <p>This fails outright with Bad data. If I switch to TransformBlock the code at least runs but produces just gibberish:</p>
  <pre><code>   byte[] output = new byte[10000];
     var count = decryptor.TransformBlock(encoded, 0, encoded.Length, output, 0);
  </code></pre>
  <p>So the questions are:</p>
  <ul>
  <li>If I only have a key is the InitializationVector required?</li>
  <li>If not is null the right thing to pass?</li>
  <li>What else would I possibly need to set beyond the key and mode?</li>
  <li>Why does TransformBlock at least work and TransformFinalBlock just fails?</li>
  </ul>
  <h3>Update - found the problem</h3>
  <p>It turns out the decoding problem was caused, not by the missing Initialization Vector, but by incorrect information from the provider of the encrypted data. The updated working code looks like this:</p>
  <pre><code>        // Read the test data
          byte[] encoded = File.ReadAllBytes(@&quot;..\..\..\..\test.tdes&quot;);            
  
          // Get the key into a byte array
          string key = &quot;1468697320656E6372797174696F6E206973737265206933&quot;;           
          byte[] keyData = ParseHex(key);                        
          
  
          TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
          des.Mode = CipherMode.ECB;      // Make sure this is correct!!!
          des.Padding = PaddingMode.Zeros;   // Make sure this is correct!!!
          des.Key = keyData;
         
          var decryptor = des.CreateDecryptor();  
          byte[] output = decryptor.TransformFinalBlock(encoded, 0, encoded.Length);
  
          string dataString = Encoding.Default.GetString(encoded);
          Console.WriteLine(dataString);
  
          Console.WriteLine(&quot;\r\n\r\nDecoded:&quot;);
          string result = Encoding.Default.GetString(output);
          Console.WriteLine(result);
  
          Console.Read();
  </code></pre>
  <p>The key in our case was using the proper CipherMode and Padding. Fixing the padding made TransformFinalBlock() work without <em>Bad Data</em> errors. Fixing the CipherMode made properly unencrypted the data.</p>
  <p>Moral of the story: In CipherMode.ECB mode at least an Initialization Vector you don't need to provide an initialization vector. If no IV is provided the provider will auto-generate one, but the decryption still works (at least with ECB).</p>
  <p>In the end it's CRUCIAL to make sure you have <em>all the information</em> from the provider that encrypted the data.</p>