Archived Forum Post

Index of archived forum posts

Question:

Encrypt file using a stream for vb.net

Jun 01 '17 at 11:30

I am trying to encrypt a file using streaming and vb.net. All the examples I have found only show how to encrypt text in a stream. I have been able to figure out how to get that to work on a file.

Basically as soon as the encryption is started it says finished. It creates a file call source.dmp.encrypted and the size is 0 KB

I added some output for troubleshooting but that didn't help. I am using the newest chilkat dll ( .net 4.0 ).

What am I doing wrong?

Here is what I have so far:

    Try
        Dim crypt As Chilkat.Crypt2 = New Chilkat.Crypt2
        Dim success As Boolean = crypt.UnlockComponent("****************************")
        If Not success Then
            MessageBox.Show(crypt.LastErrorText)
            Return
        End If

        crypt.CryptAlgorithm = "aes"
        crypt.CipherMode = "cbc"
        crypt.KeyLength = 256
        crypt.SetEncodedIV("0000000000000000", "hex")
        crypt.SetEncodedKey("abcdefghijklmnop", "ascii")
        Dim fsIn As FileStream = File.OpenRead("source.dmp")
        Dim fsOut As FileStream = File.Create("source.dmp.encrypted")

        crypt.FirstChunk = True
        crypt.LastChunk = False

        Dim encryptedChunk() As Byte
        'chilkat example says aes has a block size of 16 bytes
        Dim b() As Byte = New Byte(1023 * 16) {}
        Dim n As Integer

        While ((n = fsIn.Read(b, 0, b.Length)) > 0)

            If (n < b.Length) Then
                ' Don't encrypt the full 1024 bytes, only the amount read...
                Dim tmp() As Byte = New Byte((n) - 1) {}
                Dim i As Integer
                i = 0
                Do While (i < n)
                    tmp(i) = b(i)
                    i = (i + 1)
                Loop

                encryptedChunk = crypt.EncryptBytes(tmp)
            Else
                encryptedChunk = crypt.EncryptBytes(b)
            End If
            Console.WriteLine("Chunk Length=" + encryptedChunk.Length.ToString)
            fsOut.Write(encryptedChunk, 0, encryptedChunk.Length)
            crypt.FirstChunk = False
            'Console.WriteLine("Writing Chunk")

        End While

        fsIn.Close()
        ' Flush any remaining encrypted data.
        crypt.LastChunk = True
        Dim empty() As Byte
        encryptedChunk = crypt.EncryptBytes(empty)
        If (encryptedChunk.Length > 0) Then
            fsOut.Write(encryptedChunk, 0, encryptedChunk.Length)

        End If

        fsOut.Close()

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

    MsgBox("Encryption finished")

Answer

The simplest solution is to just call Chilkat.Crypt2.CkEncryptFile(inputFilePath, outputFilePath)

The CkEncryptFile method will stream the file, so large files are not held entirely in memory.

Otherwise, if you really want to read chunk-by-chunk, this is a simpler example: https://www.example-code.com/vbnet/encrypt_file_chunks_cbc.asp