login about faq

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:

        Dim crypt As Chilkat.Crypt2 = New Chilkat.Crypt2
        Dim success As Boolean = crypt.UnlockComponent("****************************")
        If Not success Then
        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)

                encryptedChunk = crypt.EncryptBytes(tmp)
                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

        ' 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


    Catch ex As Exception
    End Try

    MsgBox("Encryption finished")

asked May 31 '17 at 17:00

kevindcs's gravatar image


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


answered May 31 '17 at 17:08

chilkat's gravatar image

chilkat ♦♦

I never even saw that example. I was thinking chunks so that I could give the user some kind of progress.

I will try this and let you know. Thank you for your quick reply.

(May 31 '17 at 17:15) kevindcs

If you add a handler for PercentDone, then the CkEncryptFile and CkDecryptFile methods should call it..

(May 31 '17 at 17:19) chilkat ♦♦

Could you provide an example of the progress part.

I tried to follow the example from https://www.example-code.com/vbnet/http_download_progress.asp but it doesn't report any progress.

(Jun 01 '17 at 11:24) kevindcs
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or __italic__
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: May 31 '17 at 17:00

Seen: 2,285 times

Last updated: Jun 01 '17 at 11:30

powered by OSQA