Solved

Encryption Decryption Algorithm

Posted on 2004-10-03
12
618 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
[X]
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
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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

738 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