I have started a new topic as the last one was getting a bit long.
Phillipe was kind enough to build a test piece in what I think was
Microsoft C/C++ to compare the compiled size against a PowerBASIC and MASM
DLL.
Both Eric and Phillipe expressed some doubt that DLLs built at there
minimum size were any use as an indicator to the efficiency of the
compiler used and that "real world" examples would probably expand at much
the same rate.
What follows is a DLL written in PowerBASIC that uses API file IO, it has
initialised the PowerBASIC string engine and has an encryption function
written in inline asm, things that PowerBASIC can mix with ease.
With thanks to Eric for the piece of genius about the wrapper functions in
the win32api.inc file, the minimum DLL size built at 4608 bytes. The
following DLL code builds at 6144 bytes excluding the same set of wrappers
which is roughly 1500 bytes of code added for initialising the string
engine in PowerBASIC, the API calls and the inline asm function.
The DLL has a single callable function that opens a file, encrypts it and
writes it back to disk. The encryption technique is an XOR type that uses
a variable length key as the source of the XOR extra byte and it reads the
key in ascending order until it reaches the end and then it returns to the
beginning and does it again.
Call it once with a key text of any length and it will encrypt the file,
call it again with the identical key and it will restore the file to
original. I would not keep state secrets encrypted with this technique but
it will stop all but the very serious encryption cracker.
Regards,
[email protected]
Phillipe was kind enough to build a test piece in what I think was
Microsoft C/C++ to compare the compiled size against a PowerBASIC and MASM
DLL.
Both Eric and Phillipe expressed some doubt that DLLs built at there
minimum size were any use as an indicator to the efficiency of the
compiler used and that "real world" examples would probably expand at much
the same rate.
What follows is a DLL written in PowerBASIC that uses API file IO, it has
initialised the PowerBASIC string engine and has an encryption function
written in inline asm, things that PowerBASIC can mix with ease.
With thanks to Eric for the piece of genius about the wrapper functions in
the win32api.inc file, the minimum DLL size built at 4608 bytes. The
following DLL code builds at 6144 bytes excluding the same set of wrappers
which is roughly 1500 bytes of code added for initialising the string
engine in PowerBASIC, the API calls and the inline asm function.
The DLL has a single callable function that opens a file, encrypts it and
writes it back to disk. The encryption technique is an XOR type that uses
a variable length key as the source of the XOR extra byte and it reads the
key in ascending order until it reaches the end and then it returns to the
beginning and does it again.
Call it once with a key text of any length and it will encrypt the file,
call it again with the identical key and it will restore the file to
original. I would not keep state secrets encrypted with this technique but
it will stop all but the very serious encryption cracker.
Regards,
[email protected]
Code:
The DECLARATION for the DLL function is as follows, DECLARE FUNCTION EncryptFile lib "encrypt.dll" ALIAS "EncryptFile" _ (ByVal lpszSourceFile as LONG, _ ByVal lpszDestFile as LONG) as LONG Call it in the calling application as follows, fName$ = "test.txt" KeyText$ = "You can make this key text any length you like"+ _ "The longer the better as it makes the combinations"+ _ "harder to brute force crack." EncryptFile ByVal StrPtr(fName$),ByVal StrPtr(KeyText$) The DLL code is as follows, NOTE: The line "%Small = 1" is a conditional compile statement to exclude the wrapper functions in win32api.inc. ' ########################################################################### #COMPILE DLL GLOBAL DLLinstance as LONG ' the DLLs instance handle %Small = 1 ' ----------------------------------- ' set correct paths for include files ' ----------------------------------- #INCLUDE "d:\pb6\winapi\win32api.inc" DECLARE FUNCTION SysAllocStringByteLen LIB _ "OLEAUT32.DLL" ALIAS "SysAllocStringByteLen" _ (BYVAL p1 as LONG,BYVAL p2 as LONG) as LONG DECLARE FUNCTION SysFreeString LIB "OLEAUT32.DLL" _ ALIAS "SysFreeString"(BYVAL p1 as LONG) as LONG DECLARE FUNCTION XorString(bString$,KeyString$) as STRING ' ########################################################################### FUNCTION LibMain(BYVAL hInst AS LONG, _ BYVAL Reason AS LONG, _ BYVAL Reserved AS LONG) EXPORT AS LONG DLLinstance = hInst FUNCTION = 1 END FUNCTION ' ########################################################################### FUNCTION EncryptFile ALIAS "EncryptFile"(ByVal lpszDiskFile as LONG, _ ByVal lpszKeyText as LONG) EXPORT as LONG #REGISTER NONE LOCAL hFile as LONG LOCAL lFile as LONG LOCAL bytes as LONG LOCAL hBuffer as LONG LOCAL pbString$ LOCAL KeyString$ hFile = CreateFile(ByVal lpszDiskFile,%GENERIC_READ or %GENERIC_WRITE, _ %FILE_SHARE_READ,ByVal %NULL,%OPEN_EXISTING, _ %FILE_ATTRIBUTE_NORMAL,ByVal %NULL) lFile = GetFileSize(hFile,ByVal %NULL) dest$ = space$(lFile) dst& = strptr(dest$) hBuffer = SysAllocStringByteLen(0,lFile) ReadFile hFile,ByVal hBuffer,lFile,ByVal VarPtr(bytes),ByVal %NULL ! mov eax, hBuffer ! mov pbString$, eax ! mov eax, lpszKeyText ! mov KeyString$, eax pbString$ = XorString(pbString$,KeyString$) SetFilePointer hFile,0,ByVal %NULL,%FILE_BEGIN WriteFile hFile,ByVal StrPtr(pbString$),lFile,ByVal VarPtr(bytes),ByVal %NULL SysFreeString hBuffer CloseHandle hFile FUNCTION = 0 END FUNCTION ' ########################################################################### FUNCTION XorString(bString$,KeyString$) as STRING #REGISTER NONE LOCAL ln as LONG LOCAL src as LONG LOCAL lpBt as LONG LOCAL pcnt as LONG LOCAL bvar as BYTE lpBt = StrPtr(KeyString$) ' get starting address of key string pcnt = lpBt ' copy it to another variable ! mov esi, lpBt ! mov al,[esi] ! mov bvar, al ; put 1st byte in bvar ln = len(bString$) src = StrPtr(bString$) ! mov ecx, ln ! mov esi, src ! mov edi, src xsSt: ! mov al,[esi] ; copy 1st byte of source into al ! inc esi ! xor al, bvar ; xor al with next byte in bvar ! push eax ! push esi ' ====== This code gets the next byte in the key string ====== ' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ! inc pcnt ; increment byte address ! mov esi, pcnt ; put it in esi ! mov al,[esi] ! inc esi ! cmp al, 0 ; see if its zero ! jne xsNxt ; if not, jump over next code ! mov edx, lpBt ; if al = 0 ! mov pcnt, edx ; reset pcnt to ariginal address ! mov esi, pcnt ; put original address in esi ! mov al,[esi] ! inc esi xsNxt: ! mov bvar, al ' ============================================================ ! pop esi ! pop eax ! mov [edi], al ! inc edi ! dec ecx ! cmp ecx, 0 ! jne xsSt FUNCTION = bString$ END FUNCTION ' ###########################################################################
Comment