quba88
quba88

Reputation: 174

assembler XOR encrypt/decrypt file

Looking for a program written in assembler which will encrypt / decrypt files. I use NASM as a compiler. I found the sample code but it throws errors

encrypt_xor proc lpBuffor:dword, dwSize:dword, dwKey:dword

   mov eax,lpBuffor
   mov ecx,dwSize
   mov edx,dwKey

  next_byte:

   xor byte ptr[eax+ecx-1],dl
   dec ecx
   jne next_byte

   ret

encrypt _xor endp

I have error in:

 encrypt_xor proc lpBuffor:dword, dwSize:dword, dwKey:dword

and

 encrypt_xor proc lpBuffor:dword, dwSize:dword, dwKey:dword

error parser: instruction expected and also here

xor byte ptr[eax+ecx-1],dl

error coma or end of line expected

What is wrong in this code?

Upvotes: 0

Views: 6892

Answers (1)

adelphus
adelphus

Reputation: 10316

The code you provided seems more like MASM syntax. An equivilent NASM syntax can be something like this:

; void encrypt_xor(LPBYTE lpBuffor, DWORD dwSize, DWORD dwKey)
encrypt_xor:
  push    ebp 
  mov     ebp,esp 

  %stacksize flat
  %arg lpBuffor:dword, dwSize:dword, dwKey:dword

   mov eax,[lpBuffor]
   mov ecx,[dwSize]
   mov edx,[dwKey]

  next_byte:

   xor [eax+ecx-1],dl
   dec ecx
   jne next_byte

   mov esp, ebp
   pop ebp

   ret

There are a few things to note here. Use of %stacksize and %arg assumes a cdecl calling convention (i.e all parameters are passed on the stack). In NASM, this requires the a stack frame to be included (hence the esp and ebp statements).

One last note - if this code is shared, you should think about changing the name. The routine does not provide any kind of strong encryption and it's a bad idea to lull developers into a false sense of security.

Upvotes: 3

Related Questions