I need some help in caesar cipher, here's my keygen and encrypt function
char *CSK_keygen(char key[]) {
int i, j, k, n;
strcat(key, letters);
n = strlen(key);
for(i=0; i<n; i++)
for(j = i+1; j<n;)
if(key[i] == key[j]) {
for(k=j; k<n; k++)
key[k]=key[k+1];
n--;
}
else
j++;
return key;
}
void CSK_encrypt(char msg[])
{
char key[26], enc[25];
int i, j, k;
printf("Key: ");
scanf("%s", &key);
CSK_keygen(key);
for(i=0; i<strlen(msg); i++)
for(j=0; j<strlen(letters); j++)
if(msg[i] == letters[j]) {
k = j%strlen(key);
enc[i] = key[k];
break;
}
printf("Enc: ");
for (i=0; i<strlen(msg); i++)
printf("%c", enc[i]);
printf("\n");
}
And here is the decrypt function which is not working properly, maybe the formula is not correct?
void CSK_decrypt(char msg[])
{
char key[26], enc[25];
int i, j, k;
printf("Key: ");
scanf("%s", &key);
CSK_keygen(key);
for(i=0; i<strlen(msg); i++)
for(j=0; j<strlen(letters); j++)
if(msg[i] == letters[j]) {
k = j%strlen(key) - k % 26;
enc[i] = key[k];
break;
}
printf("Denc: ");
for (i=0; i<strlen(msg); i++)
printf("%c", enc[i]);
printf("\n");
}
Can someone pleae tellme what is wrong in the formula and what should I do?