XOR using an 8-bit key
Algorithm creator(s)
n/a
PB author(s)
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.
Note
Three variations are listed; one using PB syntax, one using lodsb/stosb/loop, and one using mov/cmp/je.
Source
https://forum.powerbasic.com/forum/user-to-user-discussions/powerbasic-for-windows/5449-xor-every-byte-in-a-string-using-inline-asm?t=5256
See also
n/a
Source Code
Download source code file xor1byte.bas (Right-click -> "Save as ...")
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