examples/xor_cipher.c
raw| 1 | #include <string.h> |
| 2 | #include <stdlib.h> |
| 3 | |
| 4 | char * |
| 5 | string_crypt(const char *input, int len, const char *key) |
| 6 | { |
| 7 | char *output; |
| 8 | int i, n; |
| 9 | |
| 10 | if(input == NULL) |
| 11 | return NULL; |
| 12 | |
| 13 | output = malloc(len+1); |
| 14 | output[len] = '\0'; |
| 15 | |
| 16 | for(i=0, n=0; i<len; i++, n++) { |
| 17 | if(key[n] == '\0') |
| 18 | n = 0; |
| 19 | output[i] = input[i] ^ key[n]; |
| 20 | } |
| 21 | |
| 22 | return output; |
| 23 | } |
| 24 | |
| 25 | void |
| 26 | string_crypt_free(char *output) |
| 27 | { |
| 28 | if(output != NULL) |
| 29 | free(output); |
| 30 | } |