2017-10-20

Navigation

Skip Navigation Links

Hash algorithms

Asymmetric Algorithms

Symmetric Cipher Algorithms

Encoding Algorithms

Compression Algorithms

Pseudo Random Number Algorithms

Steganography

Library Wrappers

String Comparison

Others

Syntax highlighting by Prism
PBCrypto.com Mirror

XOR using dynamic-length key

Algorithm creator(s)

n/a


PB author(s)

Stavros A. Petridis, Wayne Diamond, Steve Hutchesson


Description

XORs every byte in a string with the single byte that is defined as the key


Note

Easily broken if two or more ciphertexts can be obtained, but stronger than the 8-bit (single-byte) XOR cipher.


Source

https://forum.powerbasic.com/forum/user-to-user-discussions/programming/19566-inline-assembler?t=19037


See also

n/a


Source Code

Download source code file xorkey.bas (Right-click -> "Save as ...")

'Included are three variations of the same cipher, by Stavros A. Petridis and Steve Hutchesson


'########################################################################
'PB XOR cipher
'Author: Stavros A. Petridis
'Source: http://www.powerbasic.com/support/forums/Forum6/HTML/002540.html
'
Sub Cipher(InString As String,PassWord As String)
    Register i            As Dword
    Register Len_PassWord As Dword
    Local    Init_y       As Dword
    Local x               As Byte Ptr
    Local y               As Byte Ptr
    x            = StrPtr(InString)
    Init_y       = StrPtr(PassWord)
    y            = Init_y
    Len_PassWord = Len(PassWord) + Init_y - 1
    For i = 1  To Len(InString)
        @x = @x Xor @y
        Incr x
        If y = Len_PassWord Then
           y = Init_y
        Else
           Incr y
        End If
    Next
End Sub


'########################################################################
'Inline Assembly XOR cipher
'Author: Stavros A. Petridis
'Source: http://www.powerbasic.com/support/forums/Forum6/HTML/002540.html
Sub CipherAsm(InString As String,PassWord As String)
    Local Len_PassWord As Dword
    Local Len_Str      As Dword
    Local Init_y       As Dword
    Local x            As Dword 
    x            = StrPtr(InString)
    Init_y       = StrPtr(PassWord)-1 
    Len_PassWord = Len(PassWord) '+ Init_y
    Len_Str      = Len(InString) 
    !Push ecx
    !Push esi
    !Push edi
    !Push eax
    !Push ebx 
    !Mov  ecx,Len_Str
    !Mov  esi,Init_Y
    !Mov  edi,x
    !Mov  eax,Len_PassWord
    !Add  eax,esi
l1:
    !Inc  esi
    !Mov  bh,[esi]
    !Mov  bl,[edi]
    !Xor  bl,bh
    !Mov  [edi],bl
    !Inc  edi
    !Cmp  esi,eax
    !Jne  n1
    !Mov  esi,Init_Y
n1:
'    !Loop l1 This line was rem out and replaced with the 2 lines below after Berns suggestion.
    !Dec ecx 
    !Jnz l1 
    !Pop ebx
    !Pop eax
    !Pop edi
    !Pop esi
    !Pop ecx 
End Sub


'########################################################################
'Author: Wayne Diamond
'
SUB XorKey(sTest AS STRING, sKey AS STRING)
#REGISTER NONE
DIM len1 AS LONG, len2 AS LONG, ptr1 AS LONG, ptr2 AS LONG
len1 = LEN(sTest)
len2 = LEN(sKey)
ptr1 = STRPTR(sTest)
ptr2 = STRPTR(sKey)
! push ebp
! xor ecx, ecx
! mov esi, ptr1  ;esi = text to xor
! mov edi, ptr2  ;edi = key
! mov edx, len2  ;edx = len key
! mov ebp, len1  ;ebp = last byte
! add ebp, esi
xornextbyte:
! mov al, [esi]
! mov bl, [edi]
! xor al, bl
! mov [esi], al
! inc ecx
! inc esi
! inc edi
! cmp esi, ebp
! jge xorcomplete
! cmp ecx, edx
! jge xornextround
! jmp xornextbyte
xornextround:
! xor ecx, ecx
! sub edi, edx
! jmp xornextbyte
xorcomplete:
! pop ebp
END SUB


'########################################################################
'Inline Assembly XOR cipher
'Author: Steve Hutchesson
'Source: http://www.powerbasic.com/support/forums/Forum4/HTML/005357.html
'
FUNCTION XorString(bString$,KeyString$) AS LONG
ON ERROR RESUME NEXT
#REGISTER NONE
      LOCAL ln   AS LONG      ' source length
      LOCAL lkey AS LONG      ' key length
      LOCAL lref AS LONG      ' counter reference for key char position
      LOCAL src  AS LONG, lpBt AS LONG, pcnt AS LONG
      LOCAL bvar AS BYTE
      ! push ebx
      ! push esi
      ! push edi
      ln   = LEN(bString$)
      lkey = LEN(KeyString$)
      IF ln = 0 OR lkey = 0 THEN EXIT FUNCTION
      lpBt = STRPTR(KeyString$)   ' get starting address of key string
      pcnt = lpBt                 ' copy it to another variable
      lref = pcnt + lkey
      ! mov esi, lpBt
      ! mov al,[esi]
      ! mov bvar, al      ; put 1st byte in bvar
      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 the 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
      ! mov ebx, lref
      ! cmp pcnt, ebx     ; if pcnt = lref
      ! jne xsNxt
      ! mov edx, lpBt
      ! 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      ; put the next byte in the variable
    ' ============================================================
      ! pop esi
      ! pop eax
      ! mov [edi], al
      ! inc edi
      ! dec ecx
      ! cmp ecx, 0
      ! jne xsSt
      ! pop edi
      ! pop esi
      ! pop ebx
      FUNCTION = 1
END FUNCTION
 
'########################################################################

Mirror provided by Knuth Konrad