2016-06-29 5 views
0

String Obfuscationのコードを書こうとしていますが、実行中にトラップを中止するエラーが発生します。すべてのアイデアは高く評価されています:エラー:トラップを破棄する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; 
} 

私の復号化機能が正常に実行されています。

+1

:1)一貫してコードをインデント。すべての開きブレース '{'の後にインデントします。すべての閉じ括弧 '}'の前にインデントします。 2)空白行を介してコードブロック(for、else、while、do ... while、switch、case、default)を区切ります。 – user3629249

答えて

2

readLine_str_test_encryptには、変数outputへのポインタが返されます。この変数はローカル変数であり、関数が終了するとスコープから外れます。

static char output[9]; 

とエラーに

変更それが消えます。

ローカル変数hereを返さない理由を詳しく読んでください。

+0

すばらしい、ありがとう。 :) –

2

投稿コードは:ヘッダファイルを含む

  1. それらの内容は単一バイトローカル変数へのポインタを返す
  2. に `3チャー整数)「のsprintf(しようと
  3. に使用されていませんsize_tはAWです:
  4. はNULバイト

注意して印刷する文字列を終了されていませんunsigned 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 
関連する問題