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.
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.