程序集加密解密程序

Assembly encryption decryption program

本文关键字:程序 加密解密 程序集      更新时间:2023-10-16

以下程序编译并工作。
但是我不知道该为解密部分写什么。

任何人都可以帮我写相应的decrypt_chars()例程吗?

void encrypt_chars(int length, char EKey)
{
    char temp_char; // char temporary store
    for (int i = 0; i < length; i++) // encrypt characters one at a time
    {
        temp_char = OChars[i]; 
        __asm {                         
            push   eax  // save register values on stack to be safe
            push   ecx          //
            movzx  ecx, temp_char       // 
            lea    eax, EKey                
            call   encrypt     // encrypt the character
            mov    temp_char, al            
            pop    ecx // restore original register values from stack
            pop    eax                  //
        }
        EChars[i] = temp_char;  // Store encrypted char in the encrypted chars array
    }
    return;
    // --- Start of Assembly code
    __asm {
// Inputs: register EAX = 32-bit address of Ekey, 
//ECX = the character to be encrypted (in the low 8-bit field, CL).
// Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).

    encrypt5: push eax
        mov  al, byte ptr[eax]
        push ecx
        and eax, 0x7C
        ror eax, 1
        ror eax, 1
        inc eax
        mov edx, eax
        pop ecx
        pop eax
        mov byte ptr[eax], dl
        xor edx, ecx
        mov eax, edx
        rol al, 1
        ret
    encrypt:
        mov eax, ecx    // get character
        inc eax  
        ret
    }
    //--- End of Assembly code
}
// end of encrypt_chars function

void decrypt_chars(int length, char EKey)
{
    /* needs to be written */
    return;
}

就目前而言,解密似乎几乎是微不足道的。尽管encrypt5代码试图做一些更精细的事情,但这里似乎实际使用的只是encrypt例程,它只是增加每个输入(完全忽略键),因此A变得BB变得C,依此类推。

因此,解密例程可能同样微不足道:

void decrypt(char *data, int length) {
    for (int i=0; i<length; i++)
        --data[i];
}

如果你真的坚持用汇编语言来做这件事,核心应该是这样的:

_asm { 
    mov eax, ecx
    dec eax
    ret
}

然后,您希望使用加密,并为输入字符串中的每个字符调用一次。

当/如果修复加密以执行不仅仅是增加每个输入字符的功能时,则需要更新解密以匹配。当然,就目前而言,这种加密根本不配得上"加密"这个名字——因为它没有密钥,所以它提供的安全性恰好为零。