如何从此加密创建解密

Posted

技术标签:

【中文标题】如何从此加密创建解密【英文标题】:How to create a Decryption From this Encryption 【发布时间】:2020-07-10 19:26:58 【问题描述】:

所以我得到了这个任务,我必须使用“encrpypt_chars”函数作为基础创建一个解密函数。据我了解,代码采用字符串的长度和加密密钥,然后使用该加密密钥单独操作存储在字符串中的每个值:

temp_char = OChars[i];              // Get the next char from Original Chars array

我的目标是对解密例程进行类似的操作,但相反,以便我将加密字符串恢复到其原始值。

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                        // stores the "eax" register out onto the stack
        push ecx                        // stores the "ecx" register out onto the stack

        call   encrypt_5                // runs the function called "decryptX"
        mov    temp_char, dl            // move values in "dl" register into "temp_char" variable

        add esp, 8                      // add 8 to the "esp" register
        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;

            // Inputs: register EAX = 32-bit address of Ekey,
            //                  ECX = the character to be encrypted 
            //                  (in the low 8-bit field, CL).

            // Output: register EDX = the encrypted value of the source character 
            //                        (in the low 8-bit field, DL).

  __asm
  
  encrypt_5:
        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
        mov   al, byte ptr[eax]         // move the pointer of the eax register to the al register in bytes
        push  ecx                       // move the encrytped charcter value on the stack
        and   eax, 0x7C                 // and eax with 0x7c (1111100 in binary)
        ror   eax, 1                    // Rorate eax register value right by 1 byte shift
        not eax                         // not the eax values within the eax register
        ror   eax, 1                    // Rotate eax register value right by 1 byte shift
        inc   eax                       // increase the byte value of eax by 1
        mov   edx, eax                  // moves the values within eax to edx
        pop   ecx                       // removes ecx from the stack
        pop   eax                       // removes eax from the stack
        mov   byte ptr[eax], dl         // move dl to the pointer of the eax register in bytes 
        xor   edx, ecx                  // Exclusive or ecx with edx and stores the value with edx
        rol   dl, 1                     // Rotate dl register value left by 1 byte shift
        pop   ebp                       // returning ebp back to the orginal value
        ret                             // end 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                        // stores the "eax" register out onto the stack
            push ecx                        // stores the "ecx" register out onto the stack

            call   decrypt_5                // runs the function called "decryptX"
            mov    temp_char, dl            // move values in "dl" register into "temp_char" variable

            add esp, 8                      // add 8 to the "esp" register
            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;

    // Inputs: register EAX = 32-bit address of Ekey,
    //                  ECX = the character to be encrypted 
    //                  (in the low 8-bit field, CL).

    // Output: register EDX = the encrypted value of the source character 
    //                        (in the low 8-bit field, DL).

    __asm
    
    decrypt_5:
            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
            mov   al, byte ptr[eax]         // move the pointer of the eax register to the al register in bytes
            push  ecx                       // move the encrytped charcter value on the stack

            and   eax, 0x7C                 // and eax with 0x7c (1111100 in binary)
            ror   eax, 1                    // Rorate eax register value right by 1 byte shift
            not   eax                       // not the eax values within the eax register
            ror   eax, 1                    // Rotate eax register value right by 1 byte shift
            inc   eax                       // increase the byte value of eax by 1

            mov   edx, eax                  // moves the values within eax to edx
            pop   ecx                       // removes ecx from the stack
            pop   eax                       // removes eax from the stack
            mov   byte ptr[eax], dl         // move dl to the pointer of the eax register in bytes 
            xor   edx, ecx                  // Exclusive or ecx with edx and stores the value with edx
            rol   dl, 1                     // Rotate dl register value left by 1 byte shift
            pop   ebp                       // returning ebp back to the orginal value
            ret                             // end function
    

    //--- End of Assembly code


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

我觉得答案与这部分有关,并以某种方式“反转”它,因为这是唯一改变 Ekey 的部分:

            and   eax, 0x7C                 // and eax with 0x7c (1111100 in binary)
            ror   eax, 1                    // Rorate eax register value right by 1 byte shift
            not   eax                       // not the eax values within the eax register
            ror   eax, 1                    // Rotate eax register value right by 1 byte shift
            inc   eax                       // increase the byte value of eax by 1

请帮我解决这个问题。

【问题讨论】:

将 eax 寄存器值右移 1 个字节。不,它只是 32 位寄存器的 1 位,而不是整个字节。 与***.com/questions/60787907/…类似,唯一需要修改的部分是xor edx, ecx\rol dl, 1。试试ror cl, 1 \ xor dl, cl 【参考方案1】:

请参考我对x86 assembly encryption to decryption的回复。我用another revision 更新了repo,用你的encrypt_5decrypt_5 替换了他们的encrypt_3decrypt_3。以下是encrypt_5 中实际相关的特定部分:

        mov   byte [eax], dl
        xor   edx, ecx
        rol   dl, 1

这是decrypt_5 中的反面部分。至于链接的答案,密钥字节计算保持完全相同。 (您错误地将密钥字节计算识别为要反转的部分,而它应该是密钥字节对明文字节的应用。)

        mov   byte [eax], dl
        ror cl, 1
        xor   edx, ecx

(由于只使用了低字节,xor dl, clxor 行的有效替换。)

【讨论】:

以上是关于如何从此加密创建解密的主要内容,如果未能解决你的问题,请参考以下文章

加密解密技术基础,PKI以及创建私有CA

加密和解密技术基础PKI及创建私有CA

DESCryptoServiceProvider 类加密解密

如何使用mcrypt解密

加密解密技术基础PKI及创建私有私有CA

创建私有CA, 加密解密基础, PKI, SSL