Page 1 of 1
Inline asm: Traditional XOR-with-password
Posted: Thu Jan 01, 2004 5:15 pm
by Wayne Diamond
Code: Select all
; EnableAsm
Procedure XorWithKey (sText.l, LenText.l, sKey.l, LenKey.l)
XOR ecx, ecx
MOV esi, sText
MOV edi, sKey
MOV edx, LenText
MOV ebp, LenKey
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 l_xorcomplete
CMP ecx, edx
JGE l_xornextround
JMP l_xornextbyte
xornextround:
XOR ecx, ecx
SUB edi, edx
JMP l_xornextbyte
xorcomplete:
EndProcedure
sText.s = "Text to encrypt"
sKey.s = "Wayne Diamond"
;// Encrypt
XorWithKey(@sText, Len(sText), @sKey, Len(sKey))
Debug sText
;// And again, which decrypts it
XorWithKey(@sText, Len(sText), @sKey, Len(sKey))
Debug sText
Posted: Thu Jan 01, 2004 5:21 pm
by Wayne Diamond
Here is the more-traditional version where each byte is XOR'd with the same value.
Code: Select all
; EnableAsm
Procedure XOrByte(sText.l, TextLen.l, Key.b)
MOV ecx, TextLen
MOV esi, sText
MOV edi, sText
CLD
Cipher:
!lodsb
XOR al, Key
!stosb
LOOP l_cipher
EndProcedure
sText.s = "Text to encrypt"
bKey.b = 04
;// Encrypt
XorByte(@sText, Len(sText), bKey)
Debug sText
;// And again, which decrypts it
XorByte(@sText, Len(sText), bKey)
Debug sText
Posted: Thu Jan 01, 2004 7:06 pm
by Andre
Thanks Wayne for sharing, nice codes !
You will become a good "member" of the
PB Codearchiv.

Posted: Thu Jan 01, 2004 8:15 pm
by newbie
infinite source of knowledge for me, beginner in ASM, and very motivating since the code "seems" (when it's done lol) not so hard as i would have expected.
For sure i will study it, always nice to have source code in ASM shared, thx you

Posted: Mon Jan 05, 2004 9:57 am
by Seldon
Thanks for sharing. Please could you try this code ? I didn't get back the original string. The version you posted (using Debug to show variables) does work here too, but if I use Print() , it doesn't. I'm using PB3.81 and Win98SE.
Code: Select all
Procedure XorWithKey (sText.l, LenText.l, sKey.l, LenKey.l)
XOR ecx, ecx
MOV esi, sText
MOV edi, sKey
MOV edx, LenText
MOV ebp, LenKey
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 l_xorcomplete
CMP ecx, edx
JGE l_xornextround
JMP l_xornextbyte
xornextround:
XOR ecx, ecx
SUB edi, edx
JMP l_xornextbyte
xorcomplete:
EndProcedure
sKey.s = "Wayne Diamond"
OpenConsole()
Print("Insert a line: ")
a$=Input()
PrintN("")
XorWithKey(@a$, Len(a$), @sKey, Len(sKey))
PrintN(a$)
t$=Input()
XorWithKey(@a$, Len(a$), @sKey, Len(sKey))
PrintN(a$)
t$=Input()
End
Thank you.
Re: Inline asm: Traditional XOR-with-password
Posted: Tue Apr 13, 2010 7:26 pm
by PeterH
Please excuse the late reply, but it's actually quiet frightening that noone has mentioned this earlier.
The code above for encrypting a string with a "code word" does not work as intended. It only encrypts the first bytes of the string up to the length of the key.
Here's a sample of a working code snippet (not thoroughly tested but I can't see any reason why it should fail like the one above), a bit shorter:
Code: Select all
Procedure XorWithKey (sText.l, LenText.l, sKey.l, LenKey.l)
MOV ebx, LenKey
MOV ecx, LenText
DEC ecx
MOV edi, sKey
MOV esi, sText
xornextbyte:
MOV eax, ecx
CDQ
DIV ebx
MOV al, [edx + edi]
XOR [ecx + esi], al
DEC ecx
JNS l_xornextbyte
EndProcedure
Re: Inline asm: Traditional XOR-with-password
Posted: Tue Apr 13, 2010 9:01 pm
by Thorium
Why xoring just 1 byte?
It's much faster to xor the whole register = 4 byte on x86 and 8 byte on x64.
You just need to take care of the password, make it a multiply of 4 or 8.
Re: Inline asm: Traditional XOR-with-password
Posted: Wed Apr 14, 2010 8:59 am
by Fred
You need to preserve all the non-volatile registers in your ASM procedure.
Re: Inline asm: Traditional XOR-with-password
Posted: Thu Apr 15, 2010 10:13 pm
by PeterH
Fred: Ah, thank you very much. I thought it would be odd of me not to miss anything. I regularly do. Update:
Code: Select all
Procedure XorWithKey (sText.l, LenText.l, sKey.l, LenKey.l)
PUSHA
MOV ebx, LenKey
MOV ecx, LenText
DEC ecx
MOV edi, sKey
MOV esi, sText
xornextbyte:
MOV eax, ecx
CDQ
DIV ebx
MOV al, [edx + edi]
XOR [ecx + esi], al
DEC ecx
JNS l_xornextbyte
POPA
EndProcedure
Re: Inline asm: Traditional XOR-with-password
Posted: Sat May 08, 2010 12:58 pm
by doctorized
@PeterH
Your last code returns Invalid Memory Access error at:
with PB x86. It is not working with x64.