Random characters at the end of string

2

I have done two functions of simple encryption of strings in C. It seems to be fine but when it comes to displaying the result, 2 random characters are added to the end of the resulting string.

The function what it does is create a new string, in the first character the size of the original string is stored (the strings will never exceed 20 or 25 characters), in the second character the key used (the first character of the string -1) and then the encrypted string.

The complete code is this:

#include <stdio.h> //printf
#include <stdlib.h> //malloc
#include <string.h> //strlen

char* x(const char* toEncrypt);
char* y(const char* toDecrypt);

int main() {
    const char *secret = "hellohello";
    printf("original: %s\n", secret);

    const char* crypted = x(secret);
    printf("crypted: %s\n", crypted);

    const char* decrypted = y(crypted);
    printf("decrypted: %s\n", decrypted);

    return 0;
}

char* x(const char* toEncrypt) {
    printf("x->toEncrypt = %s\n", toEncrypt);

    int offset = 0, i = 0;
    int length = strlen(toEncrypt);
    printf("\tx->length = %d\n", length);

    char key = ((char)(*toEncrypt))-1;
    printf("\tx->key = %c\n", key);

    char* output = (char*)malloc(length+3); //toEncrypt length + (length + key + null)

    output[offset++] = (char)length;
    output[offset++] = key;

    for (i = 0; i < length; i++) {
        printf("i = %d\toffset = %d\tchar = %c\n", i, offset, toEncrypt[i]);
        output[offset++] = toEncrypt[i] ^ key;
    }
    output[offset] = '
original: hellohello
x->toEncrypt = hellohello
        x->length = 10
        x->key = g
i = 0   offset = 2      char = h
i = 1   offset = 3      char = e
i = 2   offset = 4      char = l
i = 3   offset = 5      char = l
i = 4   offset = 6      char = o
i = 5   offset = 7      char = h
i = 6   offset = 8      char = e
i = 7   offset = 9      char = l
i = 8   offset = 10     char = l
i = 9   offset = 11     char = o
        x->output =
g
crypted:
g
y->toDecrypt =
g
        x->length = 10
        y->key = g
i = 0   offset = 2      char = h
i = 1   offset = 3      char = e
i = 2   offset = 4      char = l
i = 3   offset = 5      char = l
i = 4   offset = 6      char = o
i = 5   offset = 7      char = h
i = 6   offset = 8      char = e
i = 7   offset = 9      char = l
i = 8   offset = 10     char = l
i = 9   offset = 11     char = o
        y->output = hellohellobe
decrypted: hellohellobe
'; printf("\tx->output = %s\n", output); return output; } char* y(const char* toDecrypt) { printf("y->toDecrypt = %s\n", toDecrypt); int offset = 0, i = 0; int length = (int)toDecrypt[offset++] ; printf("\tx->length = %d\n", length); char key = (char)toDecrypt[offset++]; printf("\ty->key = %c\n", key); char* output = (char*)malloc(length+1); for (i = 0; i < length; i++) { printf("i = %d\toffset = %d\tchar = %c\n", i, offset, (char)toDecrypt[offset] ^key); output[i] = toDecrypt[offset++] ^ key; } output[offset] = '
#include <stdio.h> //printf
#include <stdlib.h> //malloc
#include <string.h> //strlen

char* x(const char* toEncrypt);
char* y(const char* toDecrypt);

int main() {
    const char *secret = "hellohello";
    printf("original: %s\n", secret);

    const char* crypted = x(secret);
    printf("crypted: %s\n", crypted);

    const char* decrypted = y(crypted);
    printf("decrypted: %s\n", decrypted);

    return 0;
}

char* x(const char* toEncrypt) {
    printf("x->toEncrypt = %s\n", toEncrypt);

    int offset = 0, i = 0;
    int length = strlen(toEncrypt);
    printf("\tx->length = %d\n", length);

    char key = ((char)(*toEncrypt))-1;
    printf("\tx->key = %c\n", key);

    char* output = (char*)malloc(length+3); //toEncrypt length + (length + key + null)

    output[offset++] = (char)length;
    output[offset++] = key;

    for (i = 0; i < length; i++) {
        printf("i = %d\toffset = %d\tchar = %c\n", i, offset, toEncrypt[i]);
        output[offset++] = toEncrypt[i] ^ key;
    }
    output[offset] = '
original: hellohello
x->toEncrypt = hellohello
        x->length = 10
        x->key = g
i = 0   offset = 2      char = h
i = 1   offset = 3      char = e
i = 2   offset = 4      char = l
i = 3   offset = 5      char = l
i = 4   offset = 6      char = o
i = 5   offset = 7      char = h
i = 6   offset = 8      char = e
i = 7   offset = 9      char = l
i = 8   offset = 10     char = l
i = 9   offset = 11     char = o
        x->output =
g
crypted:
g
y->toDecrypt =
g
        x->length = 10
        y->key = g
i = 0   offset = 2      char = h
i = 1   offset = 3      char = e
i = 2   offset = 4      char = l
i = 3   offset = 5      char = l
i = 4   offset = 6      char = o
i = 5   offset = 7      char = h
i = 6   offset = 8      char = e
i = 7   offset = 9      char = l
i = 8   offset = 10     char = l
i = 9   offset = 11     char = o
        y->output = hellohellobe
decrypted: hellohellobe
'; printf("\tx->output = %s\n", output); return output; } char* y(const char* toDecrypt) { printf("y->toDecrypt = %s\n", toDecrypt); int offset = 0, i = 0; int length = (int)toDecrypt[offset++] ; printf("\tx->length = %d\n", length); char key = (char)toDecrypt[offset++]; printf("\ty->key = %c\n", key); char* output = (char*)malloc(length+1); for (i = 0; i < length; i++) { printf("i = %d\toffset = %d\tchar = %c\n", i, offset, (char)toDecrypt[offset] ^key); output[i] = toDecrypt[offset++] ^ key; } output[offset] = '%pre%'; printf("\ty->output = %s\n", output); return output; }
'; printf("\ty->output = %s\n", output); return output; }

And the result in console:

%pre%

EDIT

Taking into account the response of @ SJuan76 adding the following code in the function y :

for(i = 0; i < length + 5; i++){ printf("i = %d\tchar = %c\n", i, output[i]); }

shows the following:

i = 0 char = h i = 1 char = e i = 2 char = l i = 3 char = l i = 4 char = o i = 5 char = h i = 6 char = e i = 7 char = l i = 8 char = l i = 9 char = o i = 10 char = b i = 11 char = e i = 12 char = i = 13 char = ; i = 14 char = C

    
asked by J. Doe 18.10.2016 в 00:33
source

1 answer

2

I was able to solve the problem by copying output to a new string. The complete code is:

#include <stdio.h> //printf
#include <stdlib.h> //malloc
#include <string.h> //strlen

char* x(const char* toEncrypt);
char* y(const char* toDecrypt);

int main() {
    const char *secret = "hellohello";
    printf("original: %s\n", secret);

    const char* crypted = x(secret);
    printf("crypted: %s\n", crypted);

    const char* decrypted = y(crypted);
    printf("decrypted: %s\n", decrypted);

    return 0;
}

char* x(const char* toEncrypt) {
    printf("x->toEncrypt = %s\n", toEncrypt);

    int offset = 0, i = 0;
    int length = strlen(toEncrypt);
    printf("\tx->length = %d\n", length);

    char key = ((char)(*toEncrypt))-1;
    printf("\tx->key = %c\n", key);

    char* output = (char*)malloc(length+3); //toEncrypt length + (length + key + null)

    output[offset++] = (char)length;
    output[offset++] = key;

    for (i = 0; i < length; i++) {
        printf("i = %d\toffset = %d\tchar = %c\n", i, offset, toEncrypt[i]);
        output[offset++] = toEncrypt[i] ^ key;
    }
    output[offset] = '
#include <stdio.h> //printf
#include <stdlib.h> //malloc
#include <string.h> //strlen

char* x(const char* toEncrypt);
char* y(const char* toDecrypt);

int main() {
    const char *secret = "hellohello";
    printf("original: %s\n", secret);

    const char* crypted = x(secret);
    printf("crypted: %s\n", crypted);

    const char* decrypted = y(crypted);
    printf("decrypted: %s\n", decrypted);

    return 0;
}

char* x(const char* toEncrypt) {
    printf("x->toEncrypt = %s\n", toEncrypt);

    int offset = 0, i = 0;
    int length = strlen(toEncrypt);
    printf("\tx->length = %d\n", length);

    char key = ((char)(*toEncrypt))-1;
    printf("\tx->key = %c\n", key);

    char* output = (char*)malloc(length+3); //toEncrypt length + (length + key + null)

    output[offset++] = (char)length;
    output[offset++] = key;

    for (i = 0; i < length; i++) {
        printf("i = %d\toffset = %d\tchar = %c\n", i, offset, toEncrypt[i]);
        output[offset++] = toEncrypt[i] ^ key;
    }
    output[offset] = '%pre%';

    char* realoutput = (char*)malloc(length+3);
    memcpy(realoutput, output, length+3);
    free(output);
    realoutput[length+3] = '%pre%';

    printf("\tx->output = %s\n", output);
    return realoutput;
}

char* y(const char* toDecrypt) {
    printf("y->toDecrypt = %s\n", toDecrypt);

    int offset = 0, i = 0;
    int length = (int)toDecrypt[offset++] ;
    printf("\tx->length = %d\n", length);

    char key = (char)toDecrypt[offset++];
    printf("\ty->key = %c\n", key);

    char* output = (char*)malloc(length+1);

    for (i = 0; i < length; i++) {
        printf("i = %d\toffset = %d\tchar = %c\n", i, offset, (char)toDecrypt[offset] ^key);
        output[i] = toDecrypt[offset++] ^ key;
    }
    printf("offset = %d\n", offset);
    output[offset] = '%pre%';

    char* realoutput = (char*)malloc(length+1);
    memcpy(realoutput, output, length+1);
    free(output);
    realoutput[length] = '%pre%';

    printf("\ty->output = %s\n", output);
    return realoutput;
}
'; char* realoutput = (char*)malloc(length+3); memcpy(realoutput, output, length+3); free(output); realoutput[length+3] = '%pre%'; printf("\tx->output = %s\n", output); return realoutput; } char* y(const char* toDecrypt) { printf("y->toDecrypt = %s\n", toDecrypt); int offset = 0, i = 0; int length = (int)toDecrypt[offset++] ; printf("\tx->length = %d\n", length); char key = (char)toDecrypt[offset++]; printf("\ty->key = %c\n", key); char* output = (char*)malloc(length+1); for (i = 0; i < length; i++) { printf("i = %d\toffset = %d\tchar = %c\n", i, offset, (char)toDecrypt[offset] ^key); output[i] = toDecrypt[offset++] ^ key; } printf("offset = %d\n", offset); output[offset] = '%pre%'; char* realoutput = (char*)malloc(length+1); memcpy(realoutput, output, length+1); free(output); realoutput[length] = '%pre%'; printf("\ty->output = %s\n", output); return realoutput; }
    
answered by 18.10.2016 в 02:19