title: How to make Rijndael CBC mode work in vb.net
body: <p>I'm trying to make rijndael work in CBC mode. I'm not exactly sure how should I do it. I think problem in my current code is that the stream is initialized every time in the beginning of encryption, so no avalanche effect occurs (same data is encrypted twice and the output of those two encryption is the same which it should not be).</p>
  
  <p>I tried to initialize the cryptostream only once but then my coded crashed because the canwrite property of cryptostream goes to false after the first write to the cryptostream.</p>
  
  <p>Here is the code what I have now:</p>
  
  <pre><code>
  Sub Main()
  
          Dim rij As New RijndaelManaged
          Dim iv(15) As Byte
          Dim key(15) As Byte
          Dim secret() As Byte = {59, 60, 61}
  
          Dim cs As ICryptoTransform
          Dim cstream As CryptoStream
  
          Dim out() As Byte
          Dim NewRandom As New RNGCryptoServiceProvider()
  
          NewRandom.GetBytes(iv)
          NewRandom.GetBytes(key)
  
          rij = New RijndaelManaged()
  
          rij.KeySize = 128
          rij.Padding = PaddingMode.PKCS7
  
          rij.Mode = CipherMode.CBC
  
          rij.IV = iv
          rij.Key = key
          cs = rij.CreateEncryptor()
  
          Dim ms_in As New MemoryStream
          cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
  
  
          Using cstream
              cstream.Write(secret, 0, 3)
          End Using
  
          out = ms_in.ToArray
          Console.WriteLine(ArrayToString(out, out.Length))
          Erase out
  
          ms_in = New MemoryStream
          cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
  
  
          Using cstream
              cstream.Write(secret, 0, 3)
          End Using
  
          out = ms_in.ToArray
          Console.WriteLine(ArrayToString(out, out.Length))
  
      End Sub
  
  </code></pre>
  
  <p>and the conversion function to convert an array to string</p>
  
  <pre><code>
   Public Function ArrayToString(ByVal bytes() As Byte, ByVal length As Integer) As String
  
          If bytes.Length = 0 Then Return String.Empty
          Dim sb As New System.Text.StringBuilder(length)
  
          Dim k As Integer = length - 1
          Dim i As Integer
  
          For i = 0 To k
              sb.Append(Chr(bytes(i)))
          Next
  
  
          Return sb.ToString()
  
      End Function
  </code></pre>
  
  <hr>
  
  <p>This is what I need:</p>
  
  <pre><code>    cs = rij.CreateEncryptor()
      Dim ms_in As New MemoryStream
      cstream = New CryptoStream(ms_in, cs, CryptoStreamMode.Write)
  
      Using cstream
          cstream.Write(secret, 0, 3) 'encrypt
      End Using
  
      out = ms_in.ToArray
      Console.WriteLine(ArrayToString(out, out.Length)) 'see the encrypted message
      Erase out
  
      Using cstream
          cstream.Write(secret, 0, 3) 'encrypt, this will crash here and this is the problem I'm trying to solve
      End Using
  
      out = ms_in.ToArray
      Console.WriteLine(ArrayToString(out, out.Length)) 'see the encrypted message this should not be the same as the first one
  </code></pre>