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:

    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")

asked May 31 at 17:00

kevindcs's gravatar image

kevindcs
11557


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

link

answered May 31 at 17:08

chilkat's gravatar image

chilkat ♦♦
12.4k316361451

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 at 17:15) kevindcs

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

(May 31 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 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

By RSS:

Answers

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

Tags:

×72
×47
×7

Asked: May 31 at 17:00

Seen: 139 times

Last updated: Jun 01 at 11:30

powered by OSQA