Inline asm: Traditional XOR-with-password

Share your advanced PureBasic knowledge/code with the community.
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Inline asm: Traditional XOR-with-password

Post 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
Wayne Diamond
User
User
Posts: 38
Joined: Tue Dec 30, 2003 1:37 pm
Location: Australia

Post 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
Last edited by Wayne Diamond on Fri Jan 02, 2004 5:57 am, edited 1 time in total.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Post by Andre »

Thanks Wayne for sharing, nice codes ! :D

You will become a good "member" of the PB Codearchiv. :wink:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
newbie
Enthusiast
Enthusiast
Posts: 296
Joined: Tue Jul 29, 2003 5:47 pm
Location: FRANCE
Contact:

Post 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 :P
- Registered PB user -

Using PB 4.00
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post 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.
PeterH
User
User
Posts: 28
Joined: Sun Apr 11, 2010 11:01 am

Re: Inline asm: Traditional XOR-with-password

Post 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
Thorium
Addict
Addict
Posts: 1305
Joined: Sat Aug 15, 2009 6:59 pm

Re: Inline asm: Traditional XOR-with-password

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Inline asm: Traditional XOR-with-password

Post by Fred »

You need to preserve all the non-volatile registers in your ASM procedure.
PeterH
User
User
Posts: 28
Joined: Sun Apr 11, 2010 11:01 am

Re: Inline asm: Traditional XOR-with-password

Post 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
User avatar
doctorized
Addict
Addict
Posts: 882
Joined: Fri Mar 27, 2009 9:41 am
Location: Athens, Greece

Re: Inline asm: Traditional XOR-with-password

Post by doctorized »

@PeterH

Your last code returns Invalid Memory Access error at:

Code: Select all

MOV al, [edx + edi]
with PB x86. It is not working with x64.
Post Reply