我如何将这种加密转换为解密?

时间:2019-07-03 08:18:53

标签: assembly x86 inline-assembly

我想知道如何将该加密代码转换为解密代码。

我知道这意味着我必须逆转某些指令并重新排序,但我无法弄清楚哪些指令需要重新排序,哪些不需要。

//---------------------------------------------------------------------------------------------------------------
//----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------

void encrypt_chars (int length, char EKey)
{
  char temp_char;                       // Character temporary store

  for (int i = 0; i < length; i++)      // Encrypt characters one at a time
  {                                     
    temp_char = OChars[i];              // Get the next char from Original Chars array

    __asm                               
    {                                   
      push   eax                        // stores the "eax" register out onto the stack
      push   ecx                        // stores the "ecx" register out onto the stack
      push   edx                        // stores the "edx" register out onto the stack
                                        //
      movzx  ecx, temp_char             // zeroise "ecx" register and move values in "temp_char" varaible to "ecx" register
      lea    eax, EKey                  // copies address of values contained within the EKey varaible and moves it into "eax"register
                                        //
      push eax                          //
      push ecx                          //
                                        //
      call   encryptX                   // runs the function called "encryptX"
      mov    temp_char, dl              // move values in "dl" register into "temp_char" variable
                                        //
      add esp, 8                        // 
      pop    edx                        // removes the "edx" register from the stack
      pop    ecx                        // removes the "ecx" register from the stack
      pop    eax                        // removes the "eax" register from the stack
    }                                   //
    EChars[i] = temp_char;              // store encrypted char in the Encrypted Chars array
  }
  return;

  __asm
  {

  encryptX:                
      push  ebp            // stores the pointer onto the stack

      mov   ebp, esp       // move values in "esp" register into "ebp" register
      mov   eax, [ebp + 12]// take value from the stack that is 8 bits above
                           // from the pointer a putting it in the "eax" register
      mov   ecx, [ebp + 8] // take value from the stack that is 8 bits above 
                           // from the pointer a putting it on ecx


      push  eax            // stores the Ekey address onto the stack
      xchg  eax, ecx       // puts temp_char's value into the EKey address register and Ekey address into temp_char register
      not   al             // multiply "al" register value by -1 (becomes equal to temp_char value)
      add   al, 1
      inc   eax            // increment temp_char value in bytes by 1
      rol   al, 1          // rotate the al register value in bytes to the left by 1 
      rol   al, 1          // rotate the al register value in bytes to the left by 1

      mov   ebx, eax       // move temp_char value into "ebx" register
      pop   eax            // removes temp_char from the stack
      push  ebx            // stores temp_char value onto the stack
      pop   edx            // removes "edx" register value from the stack
      movzx ecx, [eax]     // zeroise "ecx" register and move the address stored in "eax" register to "ecx" register
      ror   cl, 1          // rotate "cl" register value in bytes to the right by 1
      xor cl, 0x96         // Exclusive OR (or XOR) the byte values within "cl" register with hex value 0x96 in binary
      push  ecx            // stores the Ekey address onto the stack
      and   cl, 0x7        // AND the byte values within "cl" register with hex value 0x7 in binary

  X:                       // Position X:

      add dl, 2            // add 2 to value within dl
      sub cl, 1            // subtract 1 from value within cl
      jg X                 // jump to "X" position
      pop   ecx            // removes "ecx" register value from the stack
      xor   ecx, edx       // exclusive OR (or XOR) the byte values within "ecx" register with the byte values within "edx" register
      mov[eax], cl         // move "cl" register value into the address stored in "eax" register.
      pop   ebp            // returning ebp back to the orginal value
      ret                  // return, end of encryptX function
  }

  //--- End of Assembly code
}
//*** end of encrypt_chars function
//---------------------------------------------------------------------------------------------------------------




//---------------------------------------------------------------------------------------------------------------
//----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
//
void decrypt_chars(int length, char EKey)
{
    char temp_char;                       // Character temporary store

    for (int i = 0; i < length; i++)      // Encrypt characters one at a time
    {
        temp_char = EChars[i];              // Get the next char from Original Chars array

        __asm
        {
            push   eax                        // stores the "eax" register out onto the stack
            push   ecx                        // stores the "ecx" register out onto the stack
            push   edx                        // stores the "edx" register out onto the stack
                                              //
            movzx  ecx, temp_char             // zeroise "ecx" register and move values in "temp_char" varaible to "ecx" register
            lea    eax, EKey                  // copies address of values contained within the EKey varaible and moves it into "eax"register
                                              //
            push eax                          //
            push ecx                          //
                                              //
            call   decryptX                   // runs the function called "encryptX"
            mov    temp_char, dl              // move values in "dl" register into "temp_char" variable
                                              //
            add esp, 8                        // 
            pop    edx                        // removes the "edx" register from the stack
            pop    ecx                        // removes the "ecx" register from the stack
            pop    eax                        // removes the "eax" register from the stack
        }                                     //
        DChars[i] = temp_char;                // store encrypted char in the Encrypted Chars array
    }
    return;

    __asm
    {

    decryptX:
        push  ebp            // stores the pointer onto the stack

            mov   ebp, esp       // move values in "esp" register into "ebp" register
            mov   eax, [ebp + 12]// take value from the stack that is 8 bits above
                                 // from the pointer a putting it in the "eax" register
            mov   ecx, [ebp + 8] // take value from the stack that is 8 bits above
                                 // from the pointer a putting it on ecx


            push  eax            // stores the Ekey address onto the stack
            xchg  eax, ecx       // puts temp_char's value into the EKey address register and Ekey address into temp_char register

            ror   al, 1          // rotate the al register value in bytes to the left by 1 
            ror   al, 1          // rotate the al register value in bytes to the left by 1 
            dec   eax
            sub   al, 1

            mov   ebx, eax       // move temp_char value into "ebx" register
            pop   eax            // removes temp_char from the stack
            push  ebx            // stores temp_char value onto the stack
            pop   edx            // removes "edx" register value from the stack
            movzx ecx, [eax]     // zeroise "ecx" register and move the address stored in "eax" register to "ecx" register
            ror   cl, 1          // rotate "cl" register value in bytes to the right by 1
            xor cl, 0x96         // Exclusive OR (or XOR) the byte values within "cl" register with hex value 0x96 in binary
            push  ecx            // stores the Ekey address onto the stack
            and   cl, 0x7        // AND the byte values within "cl" register with hex value 0x7 in binary

            X:                   // Position X:

            add dl, 2            // add 2 to value within dl
            sub cl, 1            // subtract 1 from value within cl
            jg X                 // jump to "X" position
            pop   ecx            // removes "ecx" register value from the stack
            xor   ecx, edx       // exclusive OR (or XOR) the byte values within "ecx" register with the byte values within "edx" register
            mov[eax], cl         // move "cl" register value into the address stored in "eax" register.
            pop   ebp            // returning ebp back to the orginal value
            ret                  // return, end of encryptX function
    }
}

//*** end of decrypt_chars function
//---------------------------------------------------------------------------------------------------------------

输入为“ hello” 加密后,输出为“ hv ^ \ L” 解密后,输出应该是“ hello”,但我目前得到的是“ \x1Aíø\ x19 \ x1F”

0 个答案:

没有答案