错误:中止陷阱6 - 字符串混淆

时间:2016-06-29 05:56:41

标签: c string obfuscation abort

我正在尝试为String Obfuscation编写代码,但它在运行时给了我Abort trap:6错误。任何想法都非常感谢:

#include <stdio.h>
#include <math.h>
#include <string.h>

char* readLine_str_test_encrypt();
char* readLine_str_test_decrypt();

int main () { //body 

printf("%s\n", readLine_str_test_encrypt());

printf("%s\n", readLine_str_test_decrypt());


}

char* readLine_str_test_decrypt() {
static unsigned char string[9] = {100, 115, 119, 114, 90, 127, 120, 115, 22};

static int i = 0;

for (; i < 9; ++i) {
    string[i] ^= 22;
}

return (char*)string;
}

char* readLine_str_test_encrypt()
{
static unsigned char string[9] = "readLine";
char output[9];
char* output_start=output;
static int i =0;
for(; i < 9; ++i)
{
    //string[i] = string[i]^22;
    output_start += sprintf(output_start,"%d",string[i]^22);

}

return output_start;
}

我的解密功能正在成功运行。

2 个答案:

答案 0 :(得分:2)

readLine_str_test_encrypt中,您将返回指向变量output的指针。该变量是局部变量,在函数退出时超出范围。

将其更改为

static char output[9];  

并且错误消失了。

详细了解为什么不应该返回局部变量here

答案 1 :(得分:2)

发布的代码是:

  1. 包括未使用这些内容的头文件
  2. 尝试'sprintf()`一个3字节整数成一个字节
  3. 返回指向本地变量的指针
  4. 未终止要用NUL字节打印的字符串
  5. 注意:size_tunsigned long int

    的众所周知的定义

    以下建议的代码纠正了上述所有问题和其他一些问题。

    #include <stdio.h>   // printf()
    #include <stdlib.h>  // exit(), EXIT_FAILURE, malloc(), free()
    //#include <math.h>
    #include <string.h>  // strlen()
    
    char* readLine_str_test_encrypt( char *, size_t );
    char* readLine_str_test_decrypt( char *, size_t );
    
    
    int main ( void )
    { //body
        char string[] = "readLine";
    
        char * encryptedString = readLine_str_test_encrypt( string, strlen(string) );
        // Note: the encrypted string may not be printable
        printf("%s\n", encryptedString );
    
        char * decryptedString = readLine_str_test_decrypt( encryptedString, strlen(string) );
        printf( "%s\n", decryptedString );
    
        free( encryptedString );
        free( decryptedString );
    } // end function: main
    
    
    char* readLine_str_test_decrypt( char *encryptedString, size_t length)
    {
        char *string = NULL;
    
        if( NULL == ( string = malloc( length +1) ) )
        {// then malloc failed
            perror( "malloc for decrypted string failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        for ( size_t i=0; encryptedString[i]; ++i )
        {
            string[i] = encryptedString[i] ^22;
        }
    
        string[length] = '\0';
    
        return string;
    } // end function: readLine_str_test_decrypt
    
    
    char* readLine_str_test_encrypt( char *stringToEncrypt, size_t length )
    {
        char *output = NULL;
    
        if( NULL == (output = malloc( length+1 ) ) )
        { // then, malloc failed
            perror( "malloc for work area failed" );
            exit( EXIT_FAILURE );
        }
    
        // implied else, malloc successful
    
        for( size_t i=0; stringToEncrypt[i]; ++i)
        {
            output[i] = stringToEncrypt[i] ^22;
        }
    
        output[length] = '\0';
    
        return output;
    } // end function: readLine_str_test_encrypt
    

    上面代码的输出是:

    dswrZxs
    readLine