Q:

Problems encrypting and decrypting a byte array in VB.NET

I am having difficulty encrypting and decrypting a byte array in .NET. I would appreciate some help in understanding why it is not currently working. 
Here is the code: 
Public Shared Function GenerateKey(password As String, Size As Int32) As Byte()
    Dim rfc As New Rfc2898DeriveBytes(password, Salt, iterations:=973)
    Return rfc.GetBytes(Size)
End Function

Public Shared Function EncryptArray(ByRef data As Byte(), password As String) As Byte()
    Dim key() As Byte = GenerateKey(password, 8)
    Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
    Using cp As New DESCryptoServiceProvider
        Using ms As New MemoryStream
            Dim bf As New BinaryFormatter
            Using cs As New CryptoStream(ms, cp.CreateEncryptor(key, IV), CryptoStreamMode.Write)
                bf.Serialize(cs, data)
                cs.FlushFinalBlock()
                Return ms.GetBuffer
            End Using
        End Using
    End Using
End Function

Public Shared Sub DecryptArray(ByRef data As Byte(), password As String)
    Dim key() As Byte = GenerateKey(password, 8)
    Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
    Using cp As New DESCryptoServiceProvider
        Using ms As New MemoryStream(data)                 
            Using cs As New CryptoStream(ms, cp.CreateDecryptor(key, IV), CryptoStreamMode.Read)
                Using br As New BinaryReader(cs)
                    Dim bf As New BinaryFormatter                          
                    data = DirectCast(bf.Deserialize(cs), Byte())
                End Using
            End Using
        End Using
    End Using
End Sub

My calling routine:
Public Sub TestArrayEncryption()
    Dim text As String = "  imkkj r As ing = Hash.gHa.ing(s, ""sh2"", ""DGF&^***YHGJ&^*&(KI~@"")"""
    Dim pw As String = "pasword12345678901234567890"
    Dim arr As Byte() = Encryption.EncryptArray(Encryption.ConvertUTF8ToByteArray(text), pw)

    Encryption.DecryptArray(arr, pw)
    Dim txt2 As String = Encryption.ConvertByteArrayToUTF8(arr)
    Assert.AreEqual(txt2, text)
End Sub

The code is failing on this line data = DirectCast(bf.Deserialize(ms), Byte()) with the Cryptographic Exception 'Bad Data'. 
Addition:
I managed to get this working using other code found on this site. I noticed when using these new routines the encrypted array is larger than the input array. This was not the case in my original code. I would still be interested to know why the original code did not work.
Working code:
        Public Shared Function DecryptArray2(ByRef data As Byte(), password As String) As Byte()
        Dim key() As Byte = GenerateKey(password, 8)
        Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
        Dim ddata As Byte()
        Using ms As New MemoryStream
            Using cp As New DESCryptoServiceProvider
                Using cs As New CryptoStream(ms, cp.CreateDecryptor(key, IV), CryptoStreamMode.Write)
                    cs.Write(data, 0, data.Length)
                    cs.Close()
                    ddata = ms.ToArray
                End Using
            End Using
        End Using
        Return ddata
    End Function

    Public Shared Function EncryptArray2(ByRef data As Byte(), password As String) As Byte()
        Dim key() As Byte = GenerateKey(password, 8)
        Dim IV() As Byte = {18, 52, 86, 120, 144, 171, 205, 239}
        Dim edata As Byte()
        Using ms As New MemoryStream
            Using cp As New DESCryptoServiceProvider
                Using cs As New CryptoStream(ms, cp.CreateEncryptor(key, IV), CryptoStreamMode.Write)
                    cs.Write(data, 0, data.Length)
                    cs.Close()
                    edata = ms.ToArray
                End Using
            End Using
        End Using
        Return edata
    End Function

A:

You shouldn't serialize your CryptoStream. Just write the data into it and get the array from underlying stream. For encryption:
    Using ms As New MemoryStream
        Using cs As New CryptoStream(ms, cp.CreateEncryptor(key, IV), CryptoStreamMode.Write)
            cs.Write(data, 0, data.Length)
            cs.FlushFinalBlock()
            Return ms.ToArray()
        End Using
    End Using

and for decryption:
    Using ms As New MemoryStream(data)                 
        Using cs As New CryptoStream(ms, cp.CreateDecryptor(key, IV), CryptoStreamMode.Read)
            Using ms2 As New MemoryStream()
                cs.CopyTo(ms2) 
                data = ms2.ToArray()
            End Using
        End Using
    End Using

