Search code examples
assemblyencryptionx86bit-shift

x86 assembly language rotate bits of characters to encrypt / obfuscate


I am trying to make an application that can encrypt and decrypt a string with assembly language using the ROR/ROL instructions to encrypt/decrypt. I use the following code to encrypt using an array of char to encrypt:

    push EAX
    push EBX
    push ECX
    push EDX

    MOV ECX, ArraySize
    LEA EDI, CharArray

    CICLE :
    MOV EAX, [EDI]
        ROR EAX, 03
        MOV[EDI], EAX
        INC EDI
        LOOP CICLE

        pop EDX
        pop ECX
        pop EBX
        pop EAX

I have tried to use the same method to decrypt but using ROL instead of ROR and in this case using the encrypted text resulting from the previous method as input, but it throws me a different text and not the original.


Solution

  • I am loading an array of characters that can be any size and each character rotates with the loop

    You get a different text back from decrypting because the encrypting is using mixed operand sizes between the rotation itself and the stepping through the array. You change a dword but you only advance by a byte. That's what makes the process irreversible!

    Depending on the character size use:

    ; bytes
    CICLE :
      ror  byte ptr [edi], 3
      inc  edi
      dec  ecx
      jnz  CICLE
    

    or

    ; words
    CICLE :
      ror  word ptr [edi], 3
      add  edi, 2
      dec  ecx
      jnz  CICLE
    

    or

    ; dwords
    CICLE :
      ror  dword ptr [edi], 3
      add  edi, 4
      dec  ecx
      jnz  CICLE
    

    Don't push / pop registers that the code doesn't use or that don't need to be preserved.