title: Rijndael managed: plaintext length detction
body: <p>I am spending some time learning how to use the RijndaelManaged library in .NET, and developed the following function to test encrypting text with slight modifications from the MSDN library:</p>
  
  <pre><code>Function encryptBytesToBytes_AES(ByVal plainText As Byte(), ByVal Key() As Byte, ByVal IV() As Byte) As Byte()
          ' Check arguments.
          If plainText Is Nothing OrElse plainText.Length &lt;= 0 Then
              Throw New ArgumentNullException("plainText")
          End If
          If Key Is Nothing OrElse Key.Length &lt;= 0 Then
              Throw New ArgumentNullException("Key")
          End If
          If IV Is Nothing OrElse IV.Length &lt;= 0 Then
              Throw New ArgumentNullException("IV")
          End If
  
          ' Declare the RijndaelManaged object
          ' used to encrypt the data.
          Dim aesAlg As RijndaelManaged = Nothing
  
          ' Declare the stream used to encrypt to an in memory
          ' array of bytes.
          Dim msEncrypt As MemoryStream = Nothing
  
          Try
              ' Create a RijndaelManaged object
              ' with the specified key and IV.
              aesAlg = New RijndaelManaged()
              aesAlg.BlockSize = 128
              aesAlg.KeySize = 128
              aesAlg.Mode = CipherMode.ECB
              aesAlg.Padding = PaddingMode.None
              aesAlg.Key = Key
              aesAlg.IV = IV
  
                  ' Create a decrytor to perform the stream transform.
                  Dim encryptor As ICryptoTransform = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV)
  
                  ' Create the streams used for encryption.
                  msEncrypt = New MemoryStream()
                  Using csEncrypt As New CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)
                      Using swEncrypt As New StreamWriter(csEncrypt)
  
                          'Write all data to the stream.
                          swEncrypt.Write(plainText)
                      End Using
                  End Using
  
          Finally
              ' Clear the RijndaelManaged object.
              If Not (aesAlg Is Nothing) Then
                  aesAlg.Clear()
              End If
          End Try
          ' Return the encrypted bytes from the memory stream.
          Return msEncrypt.ToArray()
  
      End Function
  </code></pre>
  
  <p>Here's the actual code I am calling encryptBytesToBytes_AES() with:</p>
  
  <pre><code>Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEncrypt.Click
      Dim bZeroKey As Byte() = {&amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0, &amp;H0}
      PrintBytesToRTF(encryptBytesToBytes_AES(bZeroKey, bZeroKey, bZeroKey))
  End Sub
  </code></pre>
  
  <p>However, I get an exception thrown on <code>swEncrypt.Write(plainText)</code> stating that the 'Length of the data to encrypt is invalid.'</p>
  
  <p>However, I know that the size of my key, iv, and plaintext are 16 bytes == 128 bits == aesAlg.BlockSize.  <strong>Why is it throwing this exception?</strong>  Is it because the StreamWriter is trying to make a String (ostensibly with some encoding) and it doesn't like &amp;H0 as a value?</p>
  
  <hr>
  
  <p>EDIT:  I think I need to come up with a new way to encrypt the byte array, other than using the StreamWriter.  A gander on the <a href="http://msdn.microsoft.com/en-us/library/system.io.streamwriter.write.aspx" rel="nofollow noreferrer">MSDN page</a> shows that this will do some sort of string conversion first, which I don't want.  Any ideas?</p>