# Thread: Xor Algorith (need help !)

1. ## Xor Algorith (need help !)

Hello, it's my first time posting on this forum, i need help w/ my xor algorithm :
So basicly my whole program works, if this one condition is valid (and i don't understand why)
If my message (taken from a file) is in caps lock, my key (inputed "gets")should not be in caps lock and vice versa.

Here is my code, and any idea is welcomed !

Code:
```#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#defineMAX_BUFFER_SIZE10000
typedef unsigned char byte;

//strncat(key, key, strlen(phrase) - strlen(key));

void repeat_key(byte * phrase, byte * key)
{
byte new[MAX_BUFFER_SIZE];
int i;
int cmpt = 0;

for (i = 0; i < strlen(phrase); i++) {
if (cmpt == strlen(key))
cmpt = 0;
new = key[cmpt];
cmpt++;
}
new = '\0';
strcpy(key, new);
}

void cut_key(byte * phrase, byte * key)
{
byte new[MAX_BUFFER_SIZE];
int i;
for (i = 0; i < strlen(phrase); i++) {
new = key;
}
new = '\0';
strcpy(key, new);
}

void xorCipher(byte * key, FILE * file_src_P, FILE * file_dest_P)
{
byte msg[MAX_BUFFER_SIZE];
char output[MAX_BUFFER_SIZE];
int i;

fgets(msg, MAX_BUFFER_SIZE, file_src_P);

if (strlen(key) > strlen(msg)) {
cut_key(msg, key);
for (i = 0; i < strlen(msg); i++) {
output = msg ^ key;
}
output = '\0';
if (fprintf(file_dest_P, "%s", output) < 0) {
printf("Erreur fprintf !\n");
exit(1);
};
} else if (strlen(key) < strlen(msg)) {
repeat_key(msg, key);
for (i = 0; i < strlen(msg); i++) {
output = msg ^ key;
}
output = '\0';
if (fprintf(file_dest_P, "%s", output) < 0) {
printf("Erreur fprintf !\n");
exit(1);
};
} else if (strlen(key) == strlen(msg)) {
for (i = 0; i < strlen(msg); i++) {
output = msg ^ key;
}
output = '\0';
if (fprintf(file_dest_P, "%s", output) < 0) {
printf("Erreur fprintf !\n");
exit(1);
};
}
}

int main()
{
byte key[MAX_BUFFER_SIZE];
FILE *f_s_p = fopen("source.txt", "r");
FILE *f_d_p = fopen("destination.txt", "w");
//FILE * f_s_p = fopen("destination.txt","r");
//FILE * f_d_p = fopen("source.txt","w");

printf("Entrez clef : ");
gets(key);

if (strlen(key) == 0) {
printf("Entrez une clef valide !\n");
exit(1);
}

if (f_s_p == NULL || f_d_p == NULL) {
printf("Erreur fopen !\n");
exit(1);
}

xorCipher(key, f_s_p, f_d_p);

fclose(f_s_p);
fclose(f_d_p);

return 0;

}```

2. > So basicly my whole program works, if this one condition is valid (and i don't understand why)
Surely not.....

Code:
```foo.c:18:9: error: assignment to expression with array type
new = key[cmpt];
^
foo.c:21:7: error: assignment to expression with array type
new = '\0';
^```
And a whole raft of warnings.

3. Could you explain please ?
And quick update, my code is way too over complicated. Usin fgetc will help me i think. I just found out about it.