'
' ------------------------------------------------------------
' RC4 - CRYPTING AND DECRYPTING - RAPIDQ ASSEMBLER FUNCTIONS
' Version 002 Augut 13th, 2004 by Jacques PHILIPPE
' Speed : ~20 MegaBytes encrypted/second on a DURON 1200
' ------------------------------------------------------------
' Special thanks to Martin Wehner who pointed us to RC4
' See
http://groups.yahoo.com/group/rapidq/files/RC4crypt.zip' ------------------------------------------------------------
' What's New in August 2004 : New ASM code :
' - no need to pass the lenght of strings anymore
' - string can be any kind of strings, declared, immediate, ...
' ------------------------------------------------------------
' -- RC4 DOCUMENTATION --
' -----------------------
' - RC4 uses the same function to Encrypt and to Decrypt : f(fx)) = x
' - The Key is also called the Password.
' - The Table also called the Box is a 256 bytes memory space which
' is mixed with the Key and then used to Crypt/Decrypt the Datas.
' (Here we add 2 bytes - [257], [258] - to the box to store variables
' - i ,j - for Rc4Pipes). So here, the TABLE is 258 bytes long.
' Much more at :
http://www.lameindustries.org/tutorials/rc4/index.shtml' ------------------------------------------------------------
' -- WARNINGS : --
' ----------------
' - CRYPTING WITH KEYS LONGER THAN ?X? IS ILLEGAL IN CERTAIN COUNTRIES
' - EACH ENCRYPTATION OR DECRYPTATION OVERWRITES THE INPUT STRING
' ============================================================
' -- AVAILABLE FUNCTIONS : --
' ----------------------------
' Rc4BinStr (Datas As String, Key As String) As Long ' ASM
' RC4Init (bytes258Table As String, KEY As String) as Long ' ASM Only used with RC4Pipes
' RC4Pipe (bytes258Table As String, Datas As String) As Long ' ASM RC4Init must be called first
' RC4File (sPathFileNameIn As String, sPathFileNameOut As String, Key As String, iBitsSizeInByte As Long) As Long ' RQ
' GetRC4AsmErrorByName (Error As Long) As String ' RQ
'
' -- RETURNED VALUES FOR ALL FUNCTIONS --
' ---------------------------------------
' CONST RC4ASM_FINISHED_OK = 0
' CONST RC4ASM_ERROR_PTR_KEY = 1
' CONST RC4ASM_ERROR_LEN_KEY = 2
' CONST RC4ASM_ERROR_PTR_DATAS = 3
' CONST RC4ASM_ERROR_LEN_DATAS = 4
' CONST RC4ASM_ERROR_PTR_STABLE = 5
' CONST RC4ASM_ERROR_FILE_IN_CANT_BE_OPENED = 6
' CONST RC4ASM_ERROR_CANT_CREATE_FILE_OUT = 7
' CONST RC4ASM_ERROR_IBITSIZE_IN_RC4FILE_PIPE = 8
' CONST RC4ASM_ERROR_UNEXPECTED = 255
' ============================================================
' ============================================================
' -- EXAMPLES : --
' ----------------
' $INCLUDE "RC4ASM.Inc" ' ON TOP OF YOUR CODE, BEFORE EVERYTHING
' ' RC4ASM.INC dont need "RapidQ.Inc" file or
' ' any directive like $EscapeChars ...to be ON
' ------------------------------------------------------------
' 1 - BINARY STRINGS : RC4 a Binary String Datas with a Binary String Key
' ------------------
' DefStr sDatas = "The string " & Chr$(0) & "You want to encrypt" ' May Include NULLCHARS
' DefStr sKey = "The Key" & Chr$(0) & " with wich Encryption will be made" ' May Include NULLCHARS
' ' ENCRYPTATION
' DefInt iResult = RC4BinStr (sDatas, sKey)
' If iResult <> 0 Then ShowMessage (GetRC4AsmErrorByName(iResult)):End
' ' sDatas now contains the encrypted Datas
' ' DECRYPTATION
' iResult = RC4BinStr (sDatas), sKey)
' If iResult <> 0 Then ShowMessage (GetRC4AsmErrorByName(iResult)):End
' ' sDatas now contains the original Value (encrypted then decrypted)
' ------------------------------------------------------------
' 2 - PIPE : RC4Pipe to RC4 long files or long strings in bits via a buffer
' --------
' DefStr sDatas1 = "hgjhg" & Chr$(0) & "jhhj" & Chr$(0) & "gjgh" ' May Include NULLCHARS
' DefStr sDatas2 = "zre" & Chr$(0) & "azeazrarar" & Chr$(0) & "ayeyrtert" ' May Include NULLCHARS
' DefStr sKey = "Your" & Chr$(0) & "Password" ' May Include NULLCHARS
' Dim sTable As string * 258 ' you must DIM a TABLE of 258 bytes for EACH Pipe
' ' To encrypt, you must initialise the TABLE with your KEY first
' If RC4Init (sTable, sKey) <> 0 Then ShowMessage ("ERROR"):End
' ' Encryptation of two or more consecutive strings
' If RC4Pipe (sTable, sData1) <> 0 Then ShowMessage ("ERROR"):End
' ' !!! If you want to merge RC4(sDatas1) and RC4(sDatas2), you CANNOT reinitialise the Table !!!
' If RC4Pipe (sTable, sData2) <> 0 Then ShowMessage ("ERROR"):End
' sData1 = sDatas1 + sDatas2 ' sDatas1 is now the encryption of original (sDatas1 + sDatas2)
' ' To decrypt the result you MUST reinitialise the TABLE with the same KEY
' If RC4Init (sTable, sKey) <> 0 Then ShowMessage ("ERROR"):End
' If RC4Pipe (sTable, sData1) <> 0 Then ShowMessage ("ERROR"):End
' ' Now sDatas1 is the original value of (sDatas1 + sDatas2)
' ------------------------------------------------------------
' 3 - FILES : RC4 a file using Pipe. You choose the size of the bits
' --------- (100000 byte here) the larger the bits the faster it is RC4ed
' DefStr sKey = "Your" & Chr$(0) & "Password" ' May Include NULLCHARS
' ' RC4 by bits of 100000 chars
' If RC4File (YourPathFileNameIn, YourPathFileNameOut, sKey, 100000) <> 0 _
' Then ShowMessage (GetRC4AsmErrorByName):End
' ============================================================
'
' -- FUNCTIONS DETAILS AND NOTES --
' ---------------------------------
' - Rc4BinStr (Datas As String, Key As String) As Long
' . ENCRYPT/DECRYPT A BINARY STRING DATAS WITH A BINARY STRING KEY
' . DATAS and KEY may be Binary Strings : they may include Chr$(0)
' . RQ Example :
' iReturn = RC4BinStr (sYourDatas, sYourKEY)
'
' - RC4Init (Table258Bytes As String, KEY As String) as Long ' Only for Pipes
' . INITIALISE A TABLE FOR AN RC4Pipe USE
' . You must create a 258 bytes String TABLE and pass it to RC4Init.
' . 258 bytes because the ASM Pipe routine stores two variables As Byte
' there at i=[257] and j=[258] . Key may Contain NULLCHARS (Chr$(0))
' . RQ Example :
' Dim sMy258BytesTable As String * 258
' iReturn = RC4Init (sMy258bytesTable, sYourKEY)
'
' - RC4Pipe (Table258Bytes As String, Datas As String) As Long
' . ENCRYPT/DECRYPT A STRING SPLITTED IN MULTIPLE BITS
' . Datas may contain NULLCHARS (Chr$(0))
' . RC4Init must be called First to initialise the TABLE, only once for
' all the bits RC4'ed' with RC4Pipe
' . used to RC4 a long string or a file by splitting them in
' multiple bits, RC4 the bits and merge the RC4 bits.
' . you must create a 258 bytes string for each running Pipe and have
' it initialised by RC4Init one single time and then 'PIPE' all your
' bits
' . the bits'size may be different for cryprting than for decrypting
' . RQ Example :
' iReturn = RC4Init (sYour258bytesTable, sYourKEY)
' For Bit = 1 To Last
' iReturn = RC4Pipe (Your258BytesTable, Data(bit))
' Next Bit
' . much more clear with RC4File
'
' - RC4File (sPathFileNameIn As String, sPathFileNameOut As String, _
' Key As String, iBitsSizeInByte As Long) As Long
' . ENCRYPT/DECRYPT sPathFileNameIn in sPathFilNameOut by splitting
' sPathFileNameIn in Bits of equal Size = iBitsSize
' . the KEY may contain NULLCHARS (Chr$(0))
' . iBitsSize will define the Size of the Bits sent to RC4Pipe
' . the lenght of the bits to RC4 is set by iBitsSize
' . the larger the Bits the faster it RC4s
' . the bits'size may be different for crypting than for decrypting a file
' . RQ Example :
' DefStr sKey = "Your" & Chr$(0) & "Password"
' iReturn = RC4File (YourPathFileNameIn, YourPathFileNameOut, sKey, 100000)
' If iReturn <> 0 Then ShowMessage (GetRC4AsmErrorByName):End
'
' - GetRC4AsmErrorByName (Error As Long) As String
' . RETURNS A TEXT STRING FROM THE ERROR NUMBERS RETURNED BY ALL
' RC4Asm Functions.
' . RQ Example :
' ShowMessage (GetRC4AsmErrorByName(RC4File(YourPathFileNameIn, _
' YourPathFileNameOut, sKey, 100000))
' ============================================================