Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Encryption Decryption Algorithm

Posted on 2004-10-03
Medium Priority
Last Modified: 2008-03-03
Hi all,
I need a fast encryption decryption algorithm. I have a text file that I want to attach to an application. The file is about 30 MB and I don't want to split it. I have an algorithm but it takes a lot of time to run (15 mins).

Question by:abass_a_hajj
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 55

Accepted Solution

Jaime Olivares earned 672 total points
ID: 12212928
fastest algorithm is XOR operation, just choose an arbitrary value, and apply it to each byte of your file, but you must process your file in 'chunks' stored in memory, don't read every byte and write every byte at once, but in portions of 32K maybe.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12212950
LVL 12

Expert Comment

ID: 12215008
Since the text file is rather large, you could use a compression algorithm to make it smaller and attach it to your application. Depending on your requirements, this may be sufficient. Optionally, you could "encrypt" the compressed file by XOR-ing it before or after the compression (I'm not sure what's better).


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 55

Expert Comment

by:Jaime Olivares
ID: 12216785
>Optionally, you could "encrypt" the compressed file by XOR-ing it before or after the compression (I'm not sure what's better).
It is better to encode AFTER compressing, because it is harder to know if you have reached the solution when the original data is not plain-text.

Author Comment

ID: 12265965
I have loaded the whole file into memory and assigned it to a string variable. Then I have applied  a caharacter by character XOR operation to each character using the following code:

Public Function EncryptString(Strg As String, PassWord As String) As String
    Dim b As String, S As String, i As Long, j As Long
    Dim A1 As Long, A2 As Long, A3 As Long, P As String
    Dim StartAt As Long, AddString As String
    StartAt = CLng(Rnd() * Len(PassWord))
    AddString = Hex$(StartAt) & Len(Hex$(StartAt))
    j = 1
    For i = StartAt To Len(PassWord)
        P = P & Asc(Mid$(PassWord, i, 1))
    For i = 1 To StartAt - 1
        P = P & Asc(Mid$(PassWord, i, 1))
    For i = 1 To Len(Strg)
        A1 = Asc(Mid$(P, j, 1))
        j = j + 1: If j > Len(P) Then j = 1
        A2 = Asc(Mid$(Strg, i, 1))
        A3 = A1 Xor A2
        b = Hex$(A3)
        If Len(b) < 2 Then b = "0" + b
        S = S + b
    EncryptString = S & AddString
End Function

I wonder how the idea of taking 32K at once works...!!! Please tell me more..
LVL 12

Assisted Solution

fulscher earned 664 total points
ID: 12266108

just a few suggestions to simplify the code and make it a bit faster:

Public Function EncryptString(Strg As String, ByVal PassWord As String) As String
    Dim pwArr() as Long
    Dim pwLen as Long
    Dim i as Integer
    Dim c as Long

    ' prepare a buffer to contain Password for faster operation; mid$ is very slow
    pwLen = Len(PassWord)
    redim pwArr(1 to pwLen)
    for i = 1 to len(PassWord)
        pwArr(i) = Asc(Mid$(Password, i, 1))
    next i

    ' XOR string
    For i = 1 to len(Strg)
        c = Asc(Mid$(Strg, i, 1)) XOR Password(i Mod pwLen + 1)  ' use MOD function to avoid IF ... THEN
        Mid$(Strg, i, 1) = Chr(c)

    EncryptString = Strg
End Function

Now, this may be a bit faster but not a lot. The worst part is the MID$ function in the XOR loop; extracting a character takes a lot of time. A much faster way would be to create a small DLL which does the "encryption" in C; in C, strings are just a sequence of bytes, so this would be extremly fast.

Another option could be to use an array of bytes instead of a string for the input; accessing elements of an array is again faster than extracting parts of a string.

For the encryption - check out the page http://www.di-mgt.com.au/crypto.html - they have some code which can be used in VB. Much better than XOR and may even be faster because they would be coded in C. The penalty again is that you need another DLL with your app.

Since your data is in a file, you could go even a step further and program the loading, reading and decrypting of the file in C. I think there are some open source libraries around which will do this.

LVL 28

Assisted Solution

vinnyd79 earned 664 total points
ID: 12365180
There is a free commandline blowfish utility here that is pretty fast:


You could easily call it from VB using something like this:

Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function

Private Sub Command1_Click()
ShellWait Environ("ComSpec") & " /c bfish /P:mypassword /I:inputfile.txt /O:outputfile.txt /E", vbHide
MsgBox "Done"
End Sub

I tested using a 34 MB file and it took 27 sec. on a 350MHZ PII w/128MB ram
LVL 12

Expert Comment

ID: 12560645
DanRollins - I believe all experts involved have shown several options which could have been used by abass_a_hajj. I think the points should be split.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12561211
I agree

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question