Solved

Encryption Decryption Algorithm

Posted on 2004-10-03
12
596 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
Comment Utility
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
Comment Utility
0
 
LVL 12

Expert Comment

by:fulscher
Comment Utility
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
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
>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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:abass_a_hajj
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I agree
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now