title: Overflow error for encryption
body: <p>I am getting an overflow error in the following code:</p>
  
  <pre><code>    Public Shared Function AESFileEncrypt(inputFilename As String, outputFilename As String) As Boolean
  
      'Create SymmetricAlgorithm object and specify the Key and IV.
      Dim AES As RijndaelManaged = New RijndaelManaged
  
      AES.Key = objKeys.aesKey
      AES.IV = objKeys.aesIV
  
      'Create an ICryptoTransform (Encryptor) object.
      Dim Encryptor As ICryptoTransform
  
      Encryptor = AES.CreateEncryptor
  
      'Read the unencrypted file.
      Dim InputFileStream As FileStream
      InputFileStream = New FileStream(inputFilename, FileMode.Open, FileAccess.Read)
  
      Dim InputFileData(CType(InputFileStream.Length, Integer)) As Byte
      InputFileStream.Read(InputFileData, 0, CType(InputFileStream.Length, Integer))
  
      Dim OutputFileStream As FileStream
      OutputFileStream = New FileStream(outputFilename, FileMode.Create, FileAccess.Write)
  
      'Create a CryptoStream object using the Stream and ICryptoTransform objects.
      Dim EncryptCryptoStream As CryptoStream
      EncryptCryptoStream = New CryptoStream(OutputFileStream, Encryptor, CryptoStreamMode.Write)
      EncryptCryptoStream.Write(InputFileData, 0, InputFileData.Length)
      EncryptCryptoStream.FlushFinalBlock()
  
      'Clear any sensitive data from the cyptographic object.
      AES.Clear()
  
      'Close stream objects.
      EncryptCryptoStream.Close()
      InputFileStream.Close()
      OutputFileStream.Close()
  
      Return True
  
      'Catch ex As Exception
      'Debug.Print("AESFileEncrypt", ex.Message)
      'Return False
      'End Try
  
  End Function
  </code></pre>
  
  <p>The error is at this line:</p>
  
  <pre><code>        Dim InputFileData(CType(InputFileStream.Length, Integer)) As Byte
  </code></pre>
  
  <p>This happens on large files. I know it is because the byte array cannot be that large. Can I please have some help to modify this code to get it working?</p>
  
  <p>Thanks</p>
  
  <p><em>EDIT</em></p>
  
  <p>This is my current work on this function:</p>
  
  <pre><code>    Public Function AESFileEncrypt(inputFilename As String, outputFilename As String, Password As String, Salt As String) As Boolean
  
      Dim AES As RijndaelManaged = New RijndaelManaged
  
      Dim HashAlgorithm As String = "SHA1" 'Can be SHA1 or MD5
      Dim PasswordIterations As String = 2
      Dim InitialVector As String = "WinStorePassword" 'This should be a string of 16 ASCII characters.
      Dim KeySize As Integer = 256 'Can be 128, 192, or 256.
  
      Dim InitialVectorBytes As Byte() = Encoding.ASCII.GetBytes(InitialVector)
      Dim SaltValueBytes As Byte() = Encoding.ASCII.GetBytes(Salt)
      Dim DerivedPassword As PasswordDeriveBytes = New PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations)
      Dim KeyBytes As Byte() = DerivedPassword.GetBytes(KeySize / 8)
  
      Dim Encryptor As ICryptoTransform
  
      Encryptor = AES.CreateEncryptor(KeyBytes, InitialVectorBytes)
  
      Dim InputFileStream As FileStream
      InputFileStream = New FileStream(inputFilename, FileMode.Open, FileAccess.Read)
  
      Dim OutputFileStream As FileStream
      OutputFileStream = New FileStream(outputFilename, FileMode.Create, FileAccess.Write)
  
      Dim EncryptCryptoStream As CryptoStream
      EncryptCryptoStream = New CryptoStream(OutputFileStream, Encryptor, CryptoStreamMode.Write)
  
      Const BUFFER_SIZE As Integer = 4096
      Dim buffer(BUFFER_SIZE - 1) As Byte
      Dim Position As Long
  
      Do
  
          If (Position + BUFFER_SIZE) &gt; InputFileStream.Length Then
              InputFileStream.Read(buffer, Position, InputFileStream.Length - Position)
              EncryptCryptoStream.Write(buffer, Position, BUFFER_SIZE)
              Exit Do
          Else
              InputFileStream.Read(buffer, Position, BUFFER_SIZE)
              EncryptCryptoStream.Write(buffer, Position, BUFFER_SIZE)
          End If
  
          Position += BUFFER_SIZE
  
      Loop
  
      EncryptCryptoStream.FlushFinalBlock()
  
      AES.Clear()
  
      EncryptCryptoStream.Close()
      InputFileStream.Close()
      OutputFileStream.Close()
  
      Return True
  
  End Function
  </code></pre>
  
  <p>Is this correct? I did a test an a small file, and the output was a lot larger than i expected?</p>