News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Three encryption algos for working with random pads.

Started by hutch--, March 24, 2010, 12:41:33 AM

Previous topic - Next topic

hutch--

I needed this recently for a toy I am playing with. Note that the three algos require a pad at least as long as the data to be encrypted and while the example is written using alphabetical characters, it is designed to work on the full binary range.


IF 0  ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
                      Build this template with "CONSOLE ASSEMBLE AND LINK"
ENDIF ; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    include \masm32\include\masm32rt.inc

    rolbytes PROTO :DWORD,:DWORD,:DWORD
    rorbytes PROTO :DWORD,:DWORD,:DWORD
    xorbytes PROTO :DWORD,:DWORD,:DWORD

    .data?
      value dd ?

    .data
      txt1 db "This is a test",0
      pad1 db "qpwoeirutyalsk",0
      pad2 db "kslayturieowpq",0

    .code

start:
   
; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    call main
    inkey
    exit

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

  ; encrypt
    invoke xorbytes,ADDR txt1,ADDR pad1,LENGTHOF txt1 - 1
    invoke rolbytes,ADDR txt1,ADDR pad2,LENGTHOF txt1 - 1


  ; decrypt
    invoke rorbytes,ADDR txt1,ADDR pad2,LENGTHOF txt1 - 1
    invoke xorbytes,ADDR txt1,ADDR pad1,LENGTHOF txt1 - 1
    print ADDR txt1,13,10

    ret

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

xorbytes proc psrc:DWORD,ppad:DWORD,lpad:DWORD

    mov eax, [esp+4]    ; psrc
    mov edx, [esp+8]    ; ppad
    mov ecx, [esp+12]   ; lpad
    add eax, ecx
    add edx, ecx
    neg ecx

    push ebx

  lbl0:
    movzx ebx, BYTE PTR [edx+ecx]
    xor [eax+ecx], bl
    add ecx, 1
    jz lbl1

    movzx ebx, BYTE PTR [edx+ecx]
    xor [eax+ecx], bl
    add ecx, 1
    jz lbl1

    movzx ebx, BYTE PTR [edx+ecx]
    xor [eax+ecx], bl
    add ecx, 1
    jz lbl1

    movzx ebx, BYTE PTR [edx+ecx]
    xor [eax+ecx], bl
    add ecx, 1
    jnz lbl0
  lbl1:

    pop ebx

    ret 12

xorbytes endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

rolbytes proc psrc:DWORD,ppad:DWORD,lpad:DWORD

    push ebx

    mov eax, [esp+4][4]    ; psrc
    mov edx, [esp+8][4]    ; ppad
    mov ebx, [esp+12][4]   ; lpad
    add eax, ebx
    add edx, ebx
    neg ebx

  lbl0:
    movzx ecx, BYTE PTR [edx+ebx]
    rol BYTE PTR [eax+ebx], cl
    add ebx, 1
    jz lbl1

    movzx ecx, BYTE PTR [edx+ebx]
    rol BYTE PTR [eax+ebx], cl
    add ebx, 1
    jz lbl1

    movzx ecx, BYTE PTR [edx+ebx]
    rol BYTE PTR [eax+ebx], cl
    add ebx, 1
    jz lbl1

    movzx ecx, BYTE PTR [edx+ebx]
    rol BYTE PTR [eax+ebx], cl
    add ebx, 1
    jnz lbl0
  lbl1:

    pop ebx

    ret 12

rolbytes endp

OPTION PROLOGUE:PrologueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

OPTION PROLOGUE:NONE
OPTION EPILOGUE:NONE

rorbytes proc psrc:DWORD,ppad:DWORD,lpad:DWORD

    push ebx

    mov eax, [esp+4][4]    ; psrc
    mov edx, [esp+8][4]    ; ppad
    mov ebx, [esp+12][4]   ; lpad
    add eax, ebx
    add edx, ebx
    neg ebx

  lbl0:
    movzx ecx, BYTE PTR [edx+ebx]
    ror BYTE PTR [eax+ebx], cl
    add ebx, 1
    jz lbl1

    movzx ecx, BYTE PTR [edx+ebx]
    ror BYTE PTR [eax+ebx], cl
    add ebx, 1
    jz lbl1

    movzx ecx, BYTE PTR [edx+ebx]
    ror BYTE PTR [eax+ebx], cl
    add ebx, 1
    jz lbl1

    movzx ecx, BYTE PTR [edx+ebx]
    ror BYTE PTR [eax+ebx], cl
    add ebx, 1
    jnz lbl0
  lbl1:

    pop ebx

    ret 12

rorbytes endp

OPTION PROLOGUE:ProrogueDef
OPTION EPILOGUE:EpilogueDef

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

end start
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

PBrennick

The GeneSys Project is available from:
The Repository or My crappy website