NAME: XOR using an 8-bit key
CREATOR: n/a
PB AUTHOR: Wayne Diamond, Lothar Pink
DESCRIPTION: Classic XOR scrambling - XORs every byte in a string with the single byte that is defined as the key. Easily broken in just 255 rounds even if only one ciphertext is available.
NOTES: Three variations are listed; one using PB syntax, one using lodsb/stosb/loop, and one using mov/cmp/je.
SOURCE: http://www.powerbasic.com/support/forums/Forum4/HTML/005357.html
Viewing source from xor1byte.bas   1517 bytes   Last modified Wed, 20 September 2006

SUB XorString(sStr$, BYVAL xKey&)   '// by Wayne Diamond
REGISTER I AS DWORD
FOR I = 1 TO LEN(sStr$): MID$(sStr$,i,1) = CHR$(ASC(MID$(sStr$,i,1)) XOR xKey&): NEXT
END SUB
 
 
SUB XorString(sStr$, BYVAL xKey&)   '// by Wayne Diamond
#REGISTER NONE
LOCAL straddr AS STRING PTR, dwLen AS DWORD
straddr = STRPTR(sStr$)
dwLen = LEN(sStr$)
! mov ecx, dwLen    ; ecx = str length
! mov esi, straddr  ; esi = straddr
! mov edi, straddr  ; edi = straddr (we're overwriting)
! cld               ; ensure direction flag is clear
Cipher:            '; start of loop
! lodsb             ; al = next byte from str
! xor al, xKey&     ; encrypt al
! stosb             ; store al in dec$ at edi
! loop Cipher       ; dec ecx, loop if ecx != 0
! mov straddr, edx  ; dec$ = @edx
END SUB
 
 
SUB XorString(sStr$, BYVAL xKey&)   '// by Lothar Pink
#REGISTER NONE
DIM I AS LONG, lsPtr AS LONG, myStrLen&
lsPtr = STRPTR(sStr$)
myStrLen& = LEN(sStr$)
! mov eax, lsPtr     ; string descriptor in eax
! cmp eax, 0         ; no str data
! je EndXorLoop      ; jump to EndXorLoop
! mov ecx, myStrLen& ; ecx = Len(sStr)
BeginXorLoop:
! mov dl, [eax]      ; move 1 byte to dl (low byte of edx)
! xor dl, xKey&      ; xor that byte (DL)
! mov [eax], dl      ; move DL back to eax
! dec ecx            ; decrement ecx
! cmp ecx, 0         ; detect end of string
! je EndXorLoop      ; jump to EndXorLoop
! inc eax            ; increase eax
! jmp BeginXorLoop   ; jump to BeginXorLoop
EndXorLoop:
END SUB

Back to The Archives