A
A
Alexey2016-09-23 17:06:56
.NET
Alexey, 2016-09-23 17:06:56

Why can't the decompiler convert the code?

There is this code:

.method private hidebysig 
  instance uint8[] '1' (
    string '1'
  ) cil managed 
{
  // Method begins at RVA 0x22c4
  // Code size 43 (0x2b)
  .maxstack 5
  .locals init (
    [0] uint8[],
    [1] int32
  )

  IL_0000: call void [mscorlib]System.Console::WriteLine(string)
  IL_0005: ldarg.1
  IL_0006: callvirt instance uint8[] [mscorlib]System.Text.Encoding::GetBytes(string)
  IL_000b: stloc.0
  IL_000c: ldc.i4.0
  IL_000d: stloc.1
  IL_000e: br.s IL_0023
  // loop start (head: IL_0023)
    IL_0010: ldloc.0
    IL_0011: ldloc.1
    IL_0012: ldloc.0
    IL_0013: ldloc.1
    IL_0014: ldelem.u1
    IL_0015: ldc.i4 168
    IL_001a: ldloc.1
    IL_001b: sub
    IL_001c: xor
    IL_001d: conv.u1
    IL_001e: stelem.i1
    IL_001f: ldloc.1
    IL_0020: ldc.i4.2
    IL_0021: add
    IL_0022: stloc.1

    IL_0023: ldloc.1
    IL_0024: ldloc.0
    IL_0025: ldlen
    IL_0026: conv.i4
    IL_0027: blt.s IL_0010
  // end loop

  IL_0029: ldloc.0
  IL_002a: ret
} // end of method '1'::'1'

But I'm not strong in IL, so I can't understand what exactly is happening. That is, in general, I understand that there is a loop through the array obtained from Encoding.GetBytes (), which is xored, then it is added to the value 2 and stuff like that. But you can't keep EVERYTHING in your head. Yes, and I wonder why ILSpy cannot decompile this code ...
Actually the question, wtf

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2016-09-23
@PsyHaSTe

The code of the loop itself from the point of view of ilspy looks like this:

for (int i = 0; i < bytes.Length; i += 2)
    bytes[i] = (byte)((int)bytes[i] ^ 168 - i);

but in my opinion like this:
for (int i = 0; i < bytes.Length; i += 2)
    bytes[i] = (byte)((i - 168) ^ bytes[i]);

But above the cycle is generally heresy. The System.Console::WriteLine(string) method requires 1 argument, and System.Text.Encoding::GetBytes(string) also requires an object on which the method will be called. And where then are the corresponding ldarg,ldloc ? I also failed to run this piece of IL code. It spits out an exception System.InvalidProgramException, maybe this piece is just a trick, but the real one is in the exception handler? It is also quite possible that this application is a polymorph and this code will actually be rewritten at run time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question