Encryption Decryption Algorithm

Posted on 2004-10-03
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
LVL 55

Accepted Solution

Jaime Olivares earned 168 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).


Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 166 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 - 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 166 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

820 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