Jump to content
Sign in to follow this  
pixartist

Help me speed up my encryption

Recommended Posts

pixartist

Hi, i built a little symmetric encryption algorithm. I don't know too much about encryption, but i tried to get as asymetric results as possible irrespective of the symmetry of the encrypted file, in other words i tried to get no patterns in the result even though the input may have very simple patterns (eg.: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...)

the encryption key is scrambled for every iteration which makes the algorithm VERY slow.

1. i wanna know what you think of the algorithm

2. i wanna know if you have any ideas to do this faster in autoit

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#Include <GuiEdit.au3>

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("pixcrypt", 315, 126, 193, 115)
$key = GUICtrlCreateInput("", 128, 8, 177, 21)
$Label1 = GUICtrlCreateLabel("Key:", 96, 16, 25, 17)
$Button1 = GUICtrlCreateButton("En/Decrypt", 8, 8, 75, 25, 0)
$Label2 = GUICtrlCreateLabel("->", 8, 64, 13, 17)
$Input2 = GUICtrlCreateInput("", 8, 40, 297, 21)
GUICtrlSetState(-1, $GUI_DISABLE)
$Input3 = GUICtrlCreateInput("", 8, 80, 297, 21)
GUICtrlSetState(-1, $GUI_DISABLE)

$Progress1 = GUICtrlCreateProgress(8, 104, 302, 16)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
$fsize = 0
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            
            $f = FileOpenDialog("Choose File",@ScriptDir, "Any (*.*)", 1)
            if $f then
                
                $s = FileSaveDialog( "Save result to", @ScriptDir, "Any (*.*)", 18)
                if $s Then
                    GUICtrlSetData($Input2,$f)
                    GUICtrlSetData($Input3,$s)
                    $fi= FileOpen ( $s, 18 )
                    If $fi = -1 Then
                        MsgBox(0, "Error", "Unable to open file.")
                        Exit
                    EndIf
                    $timerstart = TimerInit()
                    $enc = Binary(encrypt($f,  GUICtrlRead($key)))
                    GUICtrlSetData($Progress1, 0)
                    $timetaken = TimerDiff($timerstart)
                    MsgBox(0,"Done", "Process took "& (floor($timetaken/100)/10) &" seconds for "& $fsize &" bytes." & @CRLF & $fsize/$timetaken & " bytes per ms")
                    FileWrite($fi, $enc)
                    FileClose($fi)
                EndIf
                
            EndIf
    EndSwitch
WEnd


Func ArraySwap($array, $p1, $p2)
    $tval = $array[$p1];
    $array[$p1] = $array[$p2]
    $array[$p2] = $tval
    Return $array
EndFunc


Func encrypt($file, $key)
    
    Dim $res; = Binary(0);
    $t = 0
    $keyarr = StringSplit($key, "")
    Dim $filedata  = Binary(0);
    Dim $erg  = Binary(0);
    $filedata = FileRead(FileOpen ( $file, 16 ))
    $filelen = BinaryLen( $filedata )
    $keytstart2 = Ceiling($keyarr[0]/2)
    $keytbend2 = Floor($keyarr[0]/2)
    $tlen2 = ($keyarr[0]-$keytstart2)+1;
    $fsize = $filelen
    for $i = 0 to $filelen-1 ;iterate every singly byte in the file
        GUICtrlSetData($Progress1, (100/$filelen)*$i)
        For $im = 0 to $keytbend2-1 ;iterate through half the key chars (floored)
            $val = Asc($keyarr[$im+1])+$im+$i
            if $tlen2 > $val Then ;SCRAMBLE!
                $spos = Mod($tlen2, $val)
            Else
                $spos = Mod($val, $tlen2)
            EndIf
            
            $keyarr = ArraySwap($keyarr, $im+1, $keytstart2+$spos);swapping char positions, yes swapping bytes would be nice, but i'm lazy
        Next
        $kstr = ""
        For $im = 1 to $keyarr[0]
            $kstr &= $keyarr[$im] ;imploding the new key
        Next
        
        $keybinary = StringToBinary($kstr);
        $keylen = BinaryLen($keybinary);

        $c_keybyte = Mod($i, $keylen)

        
        ;keykeykeykeykeyke 
        ;bytes in the file
        $erg = BitXOR(BinaryMid($keybinary, $c_keybyte+1, 1),BinaryMid($filedata, $i+1, 1)) ;simple xor with bytes in the key and bytes in the file

        if $i > 1 Then
            $c_keybytePrior = Mod($i-1, $keylen)
            
            $erg = BitXOR(BinaryMid($keybinary, $c_keybytePrior+1, 1), $erg) ;xoring with the previous byte in the key
        EndIf
        FileClose($file)
        $res = $res & Hex($erg,2)
        
    Next

    return Binary("0x"& $res)
EndFunc

thanks

Share this post


Link to post
Share on other sites
pixartist

no one wanna help me? :)

Share this post


Link to post
Share on other sites
Pain

Take a look at SkinnyWhiteGuy's Crypto Suite and you will see real Blowfish and Rijndael algorithms. Rijndael is really fast for being in AutoIt.

I believe the S-Box is one way to make it faster, if you don't know about S-Box you can read more here http://en.wikipedia.org/wiki/Rijndael_S-box and here http://en.wikipedia.org/wiki/Substitution_box

Also try put as many things as possible in loops and arrays to make it faster.

Share this post


Link to post
Share on other sites
pixartist

Take a look at SkinnyWhiteGuy's Crypto Suite and you will see real Blowfish and Rijndael algorithms. Rijndael is really fast for being in AutoIt.

I believe the S-Box is one way to make it faster, if you don't know about S-Box you can read more here http://en.wikipedia.org/wiki/Rijndael_S-box and here http://en.wikipedia.org/wiki/Substitution_box

Also try put as many things as possible in loops and arrays to make it faster.

well, i dont wanna use someone elses algorithm, but my own :)

Share this post


Link to post
Share on other sites
pixartist

bump... no further comments?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×