title: Occasionally getting 'System.Security.Cryptography.CryptographicException: Bad Data' Exception while decrypting string
body: <p>I have implemented a class as shown below which is responsible for encrypting/decrypting the text data to a utility class called TextEncryptionData. </p>
  
  <p>It works fine for  %99.99 of the time. But sometime (very rare) it fails to decrypt the text and I don't know why it happens. Is there any problem in the way I implemented it?</p>
  
  <pre><code>public class SubscriptionStateEncryptionLogic : IEncryptionLogic
  {
      private static byte[] KEY = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 71 };
      private static byte[] IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8};
      public EncryptionData Encrypt(EncryptionData source)
      {
          if (source == null)
          {
              return null;
          }
  
          try
          {
              TextEncryptionData data = source as TextEncryptionData;
              using (TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider())
              {
                  cryptoProvider.KeySize = 128;
                  cryptoProvider.Key = KEY;
                  cryptoProvider.IV = IV;
                  cryptoProvider.Mode = CipherMode.CBC;
                  cryptoProvider.Padding = PaddingMode.PKCS7;
                  ICryptoTransform transform = cryptoProvider.CreateEncryptor();
                  using (MemoryStream memoryStream = new MemoryStream())
                  {
                      using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write))
                      {
                          using (StreamWriter writer = new StreamWriter(cryptoStream))
                          {
                              writer.Write(data.Data);
                              writer.Flush();
                              cryptoStream.FlushFinalBlock();
                              writer.Flush();
                              return new TextEncryptionData() { Data = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length) };
                          }
                      }
                  }
              }
          }
          catch (Exception ex)
          {
              throw new SubscriptionStateEncryptionException("Error in encrypting the subscription status.", ex);
          }
      }
  
      public EncryptionData Decrypt(EncryptionData source)
      {
          if (source == null)
          {
              return null;
          }
  
          try
          {
              TextEncryptionData data = source as TextEncryptionData;
              using (TripleDESCryptoServiceProvider cryptoProvider = new TripleDESCryptoServiceProvider())
              {
                  cryptoProvider.KeySize = 128;
                  cryptoProvider.Key = KEY;
                  cryptoProvider.IV = IV;
                  cryptoProvider.Mode = CipherMode.CBC;
                  cryptoProvider.Padding = PaddingMode.PKCS7;
                  ICryptoTransform transform = cryptoProvider.CreateDecryptor();
                  using (MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(data.Data)))
                  {
                      using (CryptoStream cryptoStream = new CryptoStream(memoryStream, transform, CryptoStreamMode.Read))
                      {
                          using (StreamReader reader = new StreamReader(cryptoStream))
                          {
                              return new TextEncryptionData() { Data = reader.ReadToEnd() };
                          }
                      }
                  }
              }
          }
          catch (Exception ex)
          {
              throw new SubscriptionStateEncryptionException("Error in decrypting the subscription status.", ex);
          }
      }
  }
  </code></pre>