Code:
' From ' http://www.powerbasic.com/support/forums/Forum6/HTML/001173.html #COMPILE EXE "Scramb.exe" #INCLUDE "WIN32API.INC" '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB time_stamp_count(tick AS QUAD) ' CPU Clock count Charles E V Pegge '---------------------------' ' ' approx because it is not a serialised instruction ' ' it may execute before or after other instructions ' ' in the pipeline. ! mov ebx,tick ' var address where count is to be stored. ! db &h0f,&h31 ' RDTSC read time-stamp counter into edx:eax hi lo. ! mov [ebx],eax ' save low order 4 bytes. ! mov [ebx+4],edx ' save high order 4 bytes. '---------------------------' END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB JohnG( sFile AS STRING, sPass AS STRING ) ' 8 chr password. If less, pad out to 8 with any chr$ of choice LOCAL soph, m2, m3, i, k AS LONG LOCAL m1 AS DWORD LOCAL qaPtr AS DWORD PTR LOCAL qaPtr2, pLong AS LONG PTR LOCAL qAns AS QUAD soph = &h7C69CA85 ' prime const. we need m1 = CVDWD(LEFT$ (sPass, 4)) AND &h7fffffff 'to make sure password is small enough to fit algorithm m2 = CVDWD(RIGHT$(sPass, 4)) AND &h3fffffff 'to make sure password is small enough to fit algorithm qaPtr = VARPTR(qAns) ' qAns 1st half ptr qaPtr2 = qaPtr + 4 ' " 2nd " " pLong = STRPTR(sFile) ' record string ptr FOR i = 1 TO LEN(sFile) \ 4 ' up to 3 chrs not encoded unless record len is multiple of 4 qAns = m1 * soph + m2 m1 = @qaPtr ' save for next loop m2 = @qaPtr2 ' save for next loop m3 = m1 ' convert m1 to a LONG @pLong = @pLong XOR m3 ' encode/decode 4 bytes of string INCR pLong NEXT END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB ASMROT13( sFile AS STRING ) ' asmROT13 encryption/decryption - no extended ANSI here (a-z, A-Z only). ' Repeat action to encrypt/decrypt IF LEN(sFile) = 0 THEN EXIT SUB #REGISTER NONE LOCAL ln AS LONG ln = LEN(sFile) ' get string's len into ln ! cmp ln, 0 ; does the string have a length? ! je Exit13Loop ; exit if zero ! mov eax, sFile ; eax = pointer to string handle ! mov eax, [eax] ; eax = pointer to string data Bgn13Loop: ! mov edx, [eax] ; move current char into edx ! cmp ln, 0 ; exit when ln = zero ! jne Continue13Loop ! jmp Exit13Loop ; jump out on end of string Continue13Loop: ! cmp dl, 65 ; CASE 65 TO 90 ! jb NoRot ; if dl < 65 then get next character ! cmp dl, 90 ! jbe Rot13Upper ; if dl <= 90 ! cmp dl, 97 ; CASE 97 TO 122 ! jb NoRot ; if dl < 97 then next character ! cmp dl, 122 ! jbe Rot13Lower ; if dl <= 122 ! jmp NoRot ; CASE ELSE - get next character Rot13Lower: ! add dl, 13 ; Rotate 13 steps forward ! cmp dl, 122 ! ja AdjChar ; if dl became > 122 ! mov [eax], dl ; else write changed char back into sFile ! jmp NoRot Rot13Upper: ! add dl, 13 ; Rotate 13 steps forward ! cmp dl, 90 ! ja AdjChar ; if dl became > 90 ! mov [eax], dl ; else write changed char back into sFile ! jmp NoRot AdjChar: ! sub dl, 26 ! mov [eax], dl ; else write changed char back into sFile ! jmp NoRot NoRot: ! inc eax ! dec ln ; decrease the ln (length) counter ! jmp bgn13Loop Exit13Loop: END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB Scramble( sFile AS STRING ) REGISTER i AS LONG LOCAL lStrPtr AS BYTE PTR lStrPtr = STRPTR(sFile) FOR i = 0 TO LEN(sFile)-1 @lStrPtr[i] = @lStrPtr[i] XOR (i AND 6) + 1 NEXT END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB ASMScramble( sFile AS STRING ) LOCAL ln AS LONG ln = LEN(sFile) ' get string's len into ln ! cmp ln, 0 ; does the string have a length? ! je ExitScLoop ; exit if zero ! mov eax, sFile ; eax = pointer to string handle ! mov eax, [eax] ; eax = pointer to string data BgnScLoop: ! mov edx, [eax] ; move current char into edx ! cmp ln, 0 ; exit when ln = zero ! jne ContinueScLoop ! jmp ExitScLoop ; jump out on end of string ContinueScLoop: ! XOR dl, 2 ; xor dl with 2 (or 1, 3, whatever..) ! mov [eax], dl ; write changed char back into sFile ! inc eax ; get next character ! dec ln ; decrease the ln (length) counter ! jmp bgnScLoop ExitScLoop: END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB ScrampleAll( sFile AS STRING ) ' Scrambles all Chars REGISTER i AS LONG LOCAL nBlks, nBytes, FileLen AS LONG LOCAL pLong AS LONG POINTER pLong = STRPTR(sFile) FileLen = LEN(sFile) nBlks = FileLen\4 ' Integer Division nBytes = FileLen MOD 4 ' FOR i = 1 TO nBlks @pLong = - @pLong INCR pLong NEXT i IF nBytes THEN ' Some trailing Bytes LOCAL pByte AS BYTE POINTER pByte = pLong FOR i = 1 TO nBytes @pByte = [email protected] INCR pByte NEXT END IF END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' SUB Scrample( sFile AS STRING ) ' up to 3 trailing chars may be unscrambled REGISTER i AS LONG LOCAL pLong AS LONG PTR pLong = STRPTR(sFile) FOR i = 1 TO LEN(sFile)\4 ' Integer Division please @pLong = - @pLong INCR pLong NEXT i END SUB '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤' FUNCTION PBMAIN() AS LONG LOCAL sKey, sFile, sCipher, t, sText AS STRING LOCAL FileLen, i, hFile, PadLen, RetVal, nLoops AS LONG LOCAL cBeg, cEnd AS QUAD sText = "Low voltage cutoff should not be of great concern to the average user." ' Default ' hFile = FREEFILE ' Save the Declarations ' OPEN "1MBBinary.exe" FOR BINARY SHARED AS hFile ' IF ERR THEN MSGBOX "Problem opening file to be encrypted",48,"File Error"+STR$(ERRCLEAR) ' GET$ #hFile, LOF(hFile), sFile ' CLOSE hFile ' t = t + "Input File length :" + STR$(LEN(sText)) + $CRLF t = t + LEFT$(sText,80) + $CRLF + $CRLF ' COMMENT OUT FOR BINARY FILE ************** nLoops = 999 ' -------------------------------------- sFile = sText time_stamp_count(cBeg) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks FOR i = 1 TO nLoops CALL JohnG(sFile, "as34fd3A") ' byte array to string NEXT time_stamp_count(cEnd) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks t = t + "JohnG Avg Clks="+STR$( (cEnd-cBeg)\nLoops ) + $CRLF t = t + "Cipher length :" + STR$(LEN(sFile)) + $CRLF t = t + ">>" + LEFT$(sFile, 80) + "<<" + $CRLF + $CRLF ' -------------------------------------- sFile = sText time_stamp_count(cBeg) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks FOR i = 1 TO nLoops CALL ASMROT13(sFile) ' byte array to string NEXT time_stamp_count(cEnd) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks t = t + "SMROT13 Avg Clks="+STR$( (cEnd-cBeg)\nLoops ) + $CRLF t = t + "Cipher length :" + STR$(LEN(sFile)) + $CRLF t = t + ">>" + LEFT$(sFile, 80) + "<<" + $CRLF + $CRLF ' -------------------------------------- sFile = sText time_stamp_count(cBeg) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks FOR i = 1 TO nLoops CALL Scramble(sFile) ' byte array to string NEXT time_stamp_count(cEnd) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks t = t + "Scramble Avg Clks="+STR$( (cEnd-cBeg)\nLoops ) + $CRLF t = t + "Cipher length :" + STR$(LEN(sFile)) + $CRLF t = t + ">>" + LEFT$(sFile, 80) + "<<" + $CRLF + $CRLF ' -------------------------------------- sFile = sText time_stamp_count(cBeg) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks FOR i = 1 TO nLoops CALL ASMScramble(sFile) ' byte array to string NEXT time_stamp_count(cEnd) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks t = t + "ASMScramble Avg Clks="+STR$( (cEnd-cBeg)\nLoops ) + $CRLF t = t + "Cipher length :" + STR$(LEN(sFile)) + $CRLF t = t + ">>" + LEFT$(sFile, 80) + "<<" + $CRLF + $CRLF ' -------------------------------------- sFile = sText time_stamp_count(cBeg) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks FOR i = 1 TO nLoops CALL ScrampleAll(sFile) ' byte array to string NEXT time_stamp_count(cEnd) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks t = t + "ScrampleAll Avg Clks="+STR$( (cEnd-cBeg)\nLoops ) + $CRLF t = t + "Cipher length :" + STR$(LEN(sFile)) + $CRLF t = t + ">>" + LEFT$(sFile, 80) + "<<" + $CRLF + $CRLF ' -------------------------------------- sFile = sText time_stamp_count(cBeg) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks FOR i = 1 TO nLoops CALL Scrample(sFile) ' byte array to string NEXT time_stamp_count(cEnd) ' measuring cpu clock cycles. The overhead just for making this call is about 25 clocks t = t + "Scrample Avg Clks="+STR$( (cEnd-cBeg)\nLoops ) + $CRLF t = t + "Cipher length :" + STR$(LEN(sFile)) + $CRLF t = t + ">>" + LEFT$(sFile, 80) + "<<" + $CRLF + $CRLF MSGBOX t,64,"Text Scramble" END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤'