C ++加密和解密[关闭]

问题描述 投票:-7回答:1
void encrypt_chars(int length)
{
    char temp_char;                 

    for (int i = 0; i < length; i++)    
    {
        temp_char = OChars[i];          

        __asm {                         
            push   eax                  
            push   ecx                  

            movzx  ecx, temp_char       
            call   encrypt_nn           
            mov    temp_char, al        

            pop    ecx                  
            pop    eax                  
        }
        EChars[i] = temp_char;          
    }
    return;


    __asm {

    encrypt_nn:
        mov eax, ecx        
            inc eax 
            ret
    }

加密部分很好,但我想我会将代码复制并粘贴到解密中,而不是递增,我会减少值,以便它会返回并解密数据


//---------------------------------------------------------------------------------------------------------------
//----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
//
void decrypt_chars(int length)
{
    char temp_char;                     

    for (int i = 0; i < length; i--)
    {
        temp_char = OChars[i];

        __asm {                     
            push   eax                  
            push   ecx                  

            movzx  ecx, temp_char       
            call   encrypt_nn           
            mov    temp_char, al        

            pop    ecx                   
            pop    eax              
        }
        EChars[i] = temp_char;      
    }
    return;
}
c++ assembly
1个回答
3
投票

回答:

为了执行与加密操作相反的操作,您应该使用递减而不是递增的新例程替换对encrypt_nn的调用:

 __asm {

    decrypt_nn:
        mov eax, ecx        
            dec eax 
            ret
    }

评论:

你已经改变了循环语句

for (int i = 0; i < length; i++)  

for (int i = 0; i < length; i--)  

在第一种情况下,循环将遍历0length-1之间的所有值。这意味着你将迭代你的字符数组(假设长度是它的大小)。

在第二种情况下,您将获得不可预测的行为,因为您正在测试i < length但在每次循环迭代中执行i--。查看更多信息here

© www.soinside.com 2019 - 2024. All rights reserved.