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