title: "Padding is invalid and cannot be removed" when decrypting with Rijndael
body: <p>I'm trying to decrypt a string crypted with Rijndael algorythm. This type of cryptation apply a padding with "#" on the right of the Key and IV, if they are less than 16 characters long. The string to decrypt is received from a Webservice that sends it, and the Key, to me in XML SOAP Format. The IV is the Mac Address of my machine (that the server use as IV to encrypt the string). When i try to decrypt the received string, my program crash at this instruction:</p>
  
  <pre><code>while ((num5 = stream3.ReadByte()) != -1)
  </code></pre>
  
  <p>and it give to me this error "Padding is not valid and it cannot be removed".
  I've searched this error on MSDN, and it says that it happen when the IV used to encrypt is different from the IV used to decrypt, but, i repeat, the IV is the MacAddress and it is the same everytime.</p>
  
  <p>This is the sourcecode of Encrypt and Decrypt functions:</p>
  
  <pre><code>public static string Decrypt(string strInputString, string strKeyString, string myIV)
  {
          if ((strInputString == null) || (strInputString.Length == 0))
          {
              return strInputString;
          }
          try
          {
              int num5;
              int keySize = 0x100;
              int blockSize = 0x100;
              int length = keySize / 0x10;
              if (strKeyString.Length &gt; length)
              {
                  strKeyString = strKeyString.Substring(0, length);
              }
              if (strKeyString.Length &lt; length)
              {
                  strKeyString = strKeyString.PadRight(length, '#');
              }
              Encoding.Unicode.GetBytes(strKeyString);
              if (myIV.Length &gt; length)
              {
                  myIV = myIV.Substring(0, length);
              }
              if (myIV.Length &lt; length)
              {
                  myIV = myIV.PadRight(length, '#');
              }
              Encoding.Unicode.GetBytes(myIV);
              byte[] bytes = Encoding.Unicode.GetBytes(strKeyString);
              byte[] rgbIV = Encoding.Unicode.GetBytes(myIV);
              RijndaelManaged managed = new RijndaelManaged {
                  BlockSize = blockSize,
                  KeySize = keySize
              };
              MemoryStream stream = new MemoryStream();
              for (int i = 0; i &lt; strInputString.Length; i += 2)
              {
                  stream.WriteByte(byte.Parse(strInputString.Substring(i, 2), NumberStyles.AllowHexSpecifier));
              }
              stream.Position = 0L;
              MemoryStream stream2 = new MemoryStream();
              CryptoStream stream3 = new CryptoStream(stream, managed.CreateDecryptor(bytes, rgbIV), CryptoStreamMode.Read);
              while ((num5 = stream3.ReadByte()) != -1)
              {
                  stream2.WriteByte((byte) num5);
              }
              stream3.Close();
              stream2.Close();
              stream.Close();
              byte[] buffer3 = stream2.ToArray();
              return Encoding.Unicode.GetString(buffer3);
          }
          catch (Exception exception)
          {
              Log.Error(exception.Message);
          }
  }
  
  public static string Encrypt(string strInputString, string strKeyString, string myIV)
  {
          if ((strInputString == null) || (strInputString.Length == 0))
          {
              return strInputString;
          }
          try
          {
              int num4;
              int keySize = 0x100;
              int blockSize = 0x100;
              int length = keySize / 0x10;
              if (strKeyString.Length &gt; length)
              {
                  strKeyString = strKeyString.Substring(0, length);
              }
              if (strKeyString.Length &lt; length)
              {
                  strKeyString = strKeyString.PadRight(length, '#');
              }
              Encoding.Unicode.GetBytes(strKeyString);
              if (myIV.Length &gt; length)
              {
                  myIV = myIV.Substring(0, length);
              }
              if (myIV.Length &lt; length)
              {
                  myIV = myIV.PadRight(length, '#');
              }
              Encoding.Unicode.GetBytes(myIV);
              byte[] bytes = Encoding.Unicode.GetBytes(strKeyString);
              byte[] rgbIV = Encoding.Unicode.GetBytes(myIV);
              string str = "";
              RijndaelManaged managed = new RijndaelManaged {
                  BlockSize = blockSize,
                  KeySize = keySize
              };
              MemoryStream stream = new MemoryStream(Encoding.Unicode.GetBytes(strInputString));
              MemoryStream stream2 = new MemoryStream();
              CryptoStream stream3 = new CryptoStream(stream2, managed.CreateEncryptor(bytes, rgbIV), CryptoStreamMode.Write);
              while ((num4 = stream.ReadByte()) != -1)
              {
                  stream3.WriteByte((byte) num4);
              }
              stream3.Close();
              stream2.Close();
              stream.Close();
              foreach (byte num5 in stream2.ToArray())
              {
                  str = str + num5.ToString("X2");
              }
              return str;
          }
          catch (Exception exception)
          {
              Log.Error(exception.Message);
          }
      }
  </code></pre>
  
  <p>}</p>