程序集asm x86加密/解密程序

时间:2015-04-21 23:35:05

标签: c++ assembly encryption

我已经创建了这个程序一段时间了,我无法弄清楚如何解决解密例程,任何帮助将不胜感激。

截至目前,代码的加密部分正常工作。

#include <conio.h>      // for kbhit
#include <iostream>     // for cin >> and cout <<
#include <iomanip>      // for fancy output
using namespace std;

#define MAXCHARS 6      // feel free to alter this, but 6 is the minimum
#define dollarchar '$'  // string terminator

char OChars[MAXCHARS],
     EChars[MAXCHARS],
     DChars[MAXCHARS] = "Soon!";    // Global Original, Encrypted, Decrypted character strings

//----------------------------- C++ Functions ----------------------------------------------------------

void get_char (char& a_character)
{
    cin >> a_character;
    while (((a_character < '0') | (a_character > 'z')) && (a_character != dollarchar))
    {   cout << "Alphanumeric characters only, please try again > ";
        cin >> a_character;
    }
}
//-------------------------------------------------------------------------------------------------------------

void get_original_chars (int& length)
{   char next_char;
    length = 0;
    get_char (next_char);

    while ((length < MAXCHARS) && (next_char != dollarchar))
    {
        OChars [length++] = next_char;
        get_char (next_char);
    }
}

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

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                  // Push last parameter first 
            lea    eax,EKey 
            push   temp_char
            push   eax
            call   encrypt3             // encrypt the character
            mov    temp_char, al

            add    esp, 8                   // Clean parameters from stack

            pop    ecx                  // restore original register values from stack
            pop    eax                  //
        }
        EChars [i] = temp_char;         // Store encrypted char in the encrypted chars array
    }
   return;



   __asm {

   encrypt3: 
        push ebp                    // Save the old base pointer value
        mov ebp, esp                // Set the new base pointer value

        push edx                    // Save EDX to the first unused empty stack
        push ecx                    //ecx register containing the temp_char is pushed to the stack
        push eax                    // Save EAX to the first unused empty stack
        mov edx, [ebp + 8]          // Accessing the last value of ebp
        movzx eax, byte ptr[eax]    // Move 4 bytes to the EAX register
        rol al, 1                   // Rotate AL register one position to the left
        rol al, 1                   // Rotate AL register one position to the left
        rol al, 1                   // Rotate AL register one position to the left
        mov edx, eax                // Move 4 bytes from EAX into edx
        pop eax                     // Restore original EAX
        mov byte ptr[eax], dl       //moves the Ekey value into the EAX register as an 8-bit value
        pop ecx                     //stores the current letter being encrypted within the ECX register (it was pushed to the stack earlier in the assembly code).
        xor ecx, edx                //clears the EDX register of all values
        mov eax, ecx                // Move 4 bytes from ECX into EAX
        ror al, 1                   // Rotate AL register one position to the left
        ror al, 1                   // Rotate AL register one position to the left
        ror al, 1                   // Rotate AL register one position to the left
        pop edx                     // Restore the value of EDX
        pop ebx                     // Restore original EBX
        mov esp, ebp                // Dellocate local variables
        pop ebp                     // Restore the original value of EBP
        ret                         // Return EAX value





    }

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




//---------------------------------------------------------------------------------------------------------------
//----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
//
void decrypt_chars(int length, char EKey)
{

    return;

}
// end of decrypt_chars function
//---------------------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:1)

当然,在放入汇编语言之前,请用高级语言编写代码。

以下是一些原因:

Clobbering edx注册

mov edx, [ebp + 8]          // Accessing the last value of ebp
movzx eax, byte ptr[eax]    // Move 4 bytes to the EAX register
rol al, 1                   // Rotate AL register one position to the left
rol al, 1                   // Rotate AL register one position to the left
rol al, 1                   // Rotate AL register one position to the left
mov edx, eax                // Move 4 bytes from EAX into edx

在上面的代码中,您将[ebp + 8]移至edx。然后,您稍后将eax复制到edx四条说明中。为什么要在这里打扰第一条指令?

重复说明

汇编语言编码的一个常见原因是效率。据说你可以比编译器更好地编码或者比编译器更好地使用特殊指令。你没有,正如这个例子所示:

rol al, 1                   // Rotate AL register one position to the left
rol al, 1                   // Rotate AL register one position to the left
rol al, 1                   // Rotate AL register one position to the left

上述内容应编码为rol al, 3 另外,您是否有理由使用al注册代替eax

清除edx个注册表是错误的 该操作与注释不匹配。

   xor ecx, edx                //clears the EDX register of all values

edx注册表是错误的 语句xor edx, edx实际上清除了edx寄存器。

使用高级语言重新启动。

我建议废弃汇编语言并用高级语言重写函数。让它先工作。检查编译器的汇编语言。如果您可以比编译器更有效地编写算法代码,那么就这样做。