Home Forums ActiveX components Xceed Encryption for ActiveX How can I show progress when encrypting or decrypting a file?

Viewing 1 post (of 1 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #43028 |

    This example shows you how to do it. To use the code, put a command button named “Command1” on a new form and use the following code for the button’s Click event handler. Change “efpEncrypt” to “efpDencrypt” when decrypting. Make sure to set the SourceFile and DestinationFile constants.

    The following example uses the Rijndael encryption method.

    Private Sub Command1_Click()

    Const SourceFile = “c:\temp\source.txt”
    Const DestinationFile = “c:\temp\source.aes”
    Const lChunkSize = 1024 ‘ recommended: 32768 for fastest processing vs. progress info

    ‘ Make sure the Xceed Encryption Library is in your project’s references
    Dim xEnc As New XceedEncryption
    Dim vaBytesRead As Variant

    Dim lBytesProcessed As Long ‘ Our running total of bytes processed
    Dim lTotalBytes As Long ‘ The total amount of bytes to process
    Dim lBytesThisChunk As Long ‘ The amount of bytes to process each pass of the loop

    ‘ Use Rijndael encryption method to encrypt
    Dim xEncMethod As New XceedRijndaelEncryptionMethod

    Set xEnc.EncryptionMethod = xEncMethod

    ‘ Set the passphrase to use to encrypt the file
    Call xEnc. EncryptionMethod.SetSecretKeyFromPassPhrase(“This is a weak pass phrase!”, 128)

    ‘ Get file’s size in bytes
    lTotalBytes = FileLen(SourceFile)

    ‘ Loop until entire file is processed
    On Error Resume Next
    While (lBytesProcessed < lTotalBytes)
       If lBytesProcessed + lChunkSize > lTotalBytes Then
          lBytesThisChunk = lTotalBytes – lBytesProcessed
       Else
          lBytesThisChunk = lChunkSize
       End If

    Call xEnc.ProcessFile(SourceFile, lBytesProcessed, lBytesThisChunk, efpEncrypt, (lBytesThisChunk <> lChunkSize), DestinationFile, lBytesProcessed > 0, vaBytesRead)

    lBytesProcessed = lBytesProcessed + lBytesThisChunk

    Command1.Caption = Str$(lBytesProcessed) + ” / ” + Str$(lTotalBytes) + ” bytes or ” + Str$(Int(lBytesProcessed * 100 / lTotalBytes)) + “%”

    If Err.Number > 0 Then
       Call MsgBox(“An error has occured: ” & Err.Description)
       lBytesProcessed = lTotalBytes
    End If
    Wend

    If Err.Number = 0 Then
      Call MsgBox(“Encryption successful!”)
    End If
    End Sub

    Imported from legacy forums. Posted by Xceed admin (had 1695 views)

Viewing 1 post (of 1 total)
  • You must be logged in to reply to this topic.