Solved

Encryption Decryption Algorithm

Posted on 2004-10-03
12
609 Views
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).

Regards
0
Comment
Question by:abass_a_hajj
12 Comments
 
LVL 55

Accepted Solution

by:
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12212950
0
 
LVL 12

Expert Comment

by:fulscher
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).

HTH, J


0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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.
0
 

Author Comment

by:abass_a_hajj
ID: 12265965
Clarification:
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))
    Next
   
    For i = 1 To StartAt - 1
        P = P & Asc(Mid$(PassWord, i, 1))
    Next
   
    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
    Next
   
    EncryptString = S & AddString
End Function


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

Assisted Solution

by:fulscher
fulscher earned 166 total points
ID: 12266108
abass_a_hajj,

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

    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.

HTH, J
0
 
LVL 28

Assisted Solution

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

http://www.snapfiles.com/get/blowfishcom.html


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
Const PROCESS_QUERY_INFORMATION = &H400

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))
    Do
        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
0
 
LVL 12

Expert Comment

by:fulscher
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.
J
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12561211
I agree
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

803 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