0

I am trying to write code for String Obfuscation but it gives me Abort trap: 6 error while running. Any ideas are much appreciated:

#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;
}

My decrypt function is running successfully.

Jolta
  • 2,620
  • 1
  • 29
  • 42
  • 1
    for readability and ease of understanding by us humans: 1) consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. 2) separate code blocks (for, if, else, while, do...while, switch, case, default) via a blank line. – user3629249 Jun 30 '16 at 14:22

2 Answers2

2

In the readLine_str_test_encrypt you are returning a pointer to variable output. This variable is a local variable and goes out of scope when the function exits.

Change it to

static char output[9];  

and the error goes away.

Read more as to why you should not return a local variable here.

Community
  • 1
  • 1
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31
2

the posted code is:

  1. including a header file those contents are not used
  2. trying to 'sprintf()` a 3 char integer into a single byte
  3. returning pointers to local variables
  4. is not terminating the strings to be printed with a NUL byte

Note: size_t is a well known definition for unsigned long int

The following suggested code corrects all of the above problems and a few others.

#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

the output from the above code is:

dswrZxs
readLine
user3629249
  • 16,402
  • 1
  • 16
  • 17