Skip to main content
1 of 23
Simon
  • 31
  • 2

Simplifying and reversing an encryption/decryption algorythn in ASM X86 / Python

I'm trying to reverse a decoding function in order to produce the function for encoding.

So far I have ported the decoding function from ASM X86 to Python and it's working fine. My problem is I'm not able to reverse the function because I don't understand what the code does with the bitwise operations.

I'd like some help to simplify/understand the function and reverse it.

My code so far:

def text_to_hex(text):

    with open("ENCODE_ARRAY.bin", mode='rb') as file:
        TEXT_ENCODE_ARRAY = file.read()

    decoded = []
    sn_char_index = 0
    i = 0
    HARDCODED_LIMIT = 0x26
    text_len = len(text)

    while i < HARDCODED_LIMIT:
        if sn_char_index >= text_len:
            break
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        esi = eax & 0xFF
        esi = esi << 0xB
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        eax = eax & 0xFF
        eax = eax << 6
        esi = esi | eax
        if i == HARDCODED_LIMIT:
          break
        eax = esi
        eax = eax >> 8
        decoded.append(chr(eax))
        i += 1

        if sn_char_index >= text_len:
            break
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = eax & 0xFF
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        ebx = eax & 0xFF
        ebx = ebx + ebx
        ebx = ebx | esi
        ebx = ebx << 0x18
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        eax = eax & 0xFF
        eax = eax << 0x14
        ebx = ebx | eax
        if i == HARDCODED_LIMIT:
          break
        eax = ebx
        eax = eax >> 0x18
        eax = eax & 0xFF
        decoded.append(chr(eax))
        i += 1

        if sn_char_index >= text_len:
            break
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        eax = eax & 0xFF
        eax = eax << 0xF
        ebx = ebx | eax
        if i == HARDCODED_LIMIT:
          break
        eax = ebx
        eax = eax >> 0x10
        eax = eax & 0xFF
        decoded.append(chr(eax))
        i += 1

        if sn_char_index >= text_len:
            break
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        eax = eax & 0xFF
        eax = eax << 0xA
        ebx = ebx | eax
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        eax = eax << 5
        ebx = ebx | eax
        if i == HARDCODED_LIMIT:
          break
        eax = ebx
        eax = eax >> 8
        eax = eax & 0xFF
        decoded.append(chr(eax))
        i += 1

        if sn_char_index >= text_len:
            break
        eax = ord(text[sn_char_index])
        sn_char_index += 1
        eax = TEXT_ENCODE_ARRAY[eax]
        if eax < 0:
          break
        if i == HARDCODED_LIMIT:
          break
        ebx = ebx | eax
        ebx = ebx & 0xFF
        decoded.append(chr(ebx))
        i += 1
        #END WHILE

    return decoded
Simon
  • 31
  • 2