title: Padding is invalid and cannot be removed AES PKCS7
body: <p>I am attempting to test a simple class to encrypt and decrypt data in C#. </p>
  
  <p>`</p>
  
  <pre><code>{  [TestFixture]
      public class CryptTest
      {
          [Test]
          public void TestMethod()
          {
              String text = "Hello World!";
  
              String crypt = EncryptionService.Encrypt(text, Config.KEY_STRING);
              Console.WriteLine(crypt);
              String clear = EncryptionService.Decrypt(crypt, Config.KEY_STRING);
              Console.WriteLine(clear);
              Assert.That(clear, Is.EqualTo(text));
  
  
          }
  </code></pre>
  
  <p>`However, I am receiving the following exception:</p>
  
  <pre><code>Message: System.Security.Cryptography.CryptographicException : Padding is invalid and cannot be removed.
  </code></pre>
  
  <p>with the stack:</p>
  
  <pre><code>        StackTrace  "   at System.Security.Cryptography.CapiSymmetricAlgorithm.DepadBlock(Byte[] block, Int32 offset, Int32 count)\r\n   at System.Security.Cryptography.CapiSymmetricAlgorithm.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)\r\n   at System.Security.Cryptography.CryptoStream.Read(Byte[] buffer, Int32 offset, Int32 count)\r\n   at System.IO.StreamReader.ReadBuffer()\r\n   at System.IO.StreamReader.ReadToEnd()\r\n   at InsuranceMidAm.Services.EncryptionService.Decrypt(String cipher, String key) in C:\\Visual Studio Projects\\InsuranceMidAm\\InsuranceMidAm\\Services\\EncryptionService.cs:line 72\r\n   at InsuranceMidAm.Tests.CryptTest.TestMethod() in C:\\Visual Studio Projects\\InsuranceMidAm\\InsuranceMidAm.Tests\\CryptTest.cs:line 20" string
  </code></pre>
  
  <p>This is the class under test:</p>
  
  <pre><code>namespace InsuranceMidAm.Services
  {
      public class EncryptionService
      {
          // Reference: https://stackoverflow.com/questions/273452/using-aes-encryption-in-c-sharp
          public static String Encrypt(String text, String key)
          {
  
              byte[] value = UTF8Encoding.UTF8.GetBytes(text);
              byte[] crypt;
              byte[] iv;
              using (Aes myAes = Aes.Create())
              {
  
                  myAes.KeySize = 256;
                  myAes.Mode = CipherMode.CBC;
                  myAes.Key = HexToBin(key);
                  myAes.GenerateIV();
                  myAes.Padding = PaddingMode.PKCS7;
  
                  using (MemoryStream ms = new MemoryStream())
                  {
                      using (CryptoStream cs = new CryptoStream(ms, myAes.CreateEncryptor(), CryptoStreamMode.Write))
                      {
                          cs.Write(value, 0, value.Length);
                          cs.FlushFinalBlock();
                          crypt = ms.ToArray();
                      }
                  }
                  iv = myAes.IV;
                  myAes.Clear();
              }
              return ByteArrayToString(crypt) + ":" + ByteArrayToString(iv);
  
          }
  
          public static string Decrypt(String cipher, String key)
          {
              String outputString = "";
              byte[] ivBytes = HexToBin(getIV(cipher));
              byte[] valBytes = HexToBin(getSSN(cipher));
  
              using (Aes myAes = Aes.Create())
              {
                  int size = valBytes.Count();
  
                  myAes.KeySize = 256;
                  myAes.Mode = CipherMode.CBC;
                  myAes.Key = HexToBin(key);
                  myAes.IV = ivBytes;
                  myAes.Padding = PaddingMode.PKCS7;
  
                  char[] output = new char[256];
  
                  ICryptoTransform myDecrypter = myAes.CreateDecryptor(myAes.Key, myAes.IV);
  
                  using (MemoryStream memory = new MemoryStream(ivBytes))
                  {
                      using (CryptoStream cryptStream = new CryptoStream(memory, myDecrypter, CryptoStreamMode.Read))
                      {
                          using (StreamReader reader = new StreamReader(cryptStream))
                          {
  
                              outputString = reader.ReadToEnd();
                          }
  
                          return outputString;
                      }
                  }
  
              }
          }
  
          private static byte[] HexToBin(String hexString)
          {
  
  
              int charCount = hexString.Length;
              byte[] output = new byte[charCount / 2];
              for (int i = 0; i &lt; charCount; i += 2)
              {
                  output[i / 2] = Convert.ToByte(hexString.Substring(i, 2), 16);
              }
  
  
              return output;
          }
  
          private static String getSSN(String cipher)
          {
              int delimiterIndex = cipher.IndexOf(":");
              String SSN = cipher.Substring(0, delimiterIndex);
              return SSN;
          }
  
  
          private static String getIV(String cipher)
          {
              int delimiterIndex = cipher.IndexOf(":");
              String IV = cipher.Substring(delimiterIndex + 1);
              return IV;
          }
  
  
  
  
          // Reference: https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa
          private static string ByteArrayToString(byte[] ba)
          {
              string hex = BitConverter.ToString(ba);
              return hex.Replace("-", "");
          }
  
      }
  }
  </code></pre>
  
  <p>Line 73 (where the exception is encountered) is the end of the using block for the StreamReader in the decrypt method:</p>
  
  <pre><code>    using (StreamReader reader = new StreamReader(cryptStream))
                      {
  
                          outputString = reader.ReadToEnd();
                      }
  </code></pre>
  
  <p>I referenced the <a href="https://stackoverflow.com/questions/8583112/padding-is-invalid-and-cannot-be-removed">following question</a>, but could not resolve my issue.</p>
  
  <p>Originally, the data was encrypted in a PHP application, and decrypted using a C# application (using nearly exactly the same decrypt method above). Now,  I am wanting to both encrypt and decrypt the data using C#; however, I must still be able to properly decrypt the existing data (that was encrypted using PHP), so I would rather not modify the decrypt method too much. </p>
  
  <p>Any advice would be appreciated.</p>