I am trying to create an encryptor in C with the following algorithm I thought of. But, for some reasons it is not working.
#include <stdio.h>
#include <string.h>
void
encrypt(
char word[],
char password[]
)
{
char res[strlen(word)];
int temp;
for (int i = 0; i < strlen(word); i++){
temp = (int) word[i];
temp = temp + (int) password[i % strlen(password)];
temp = (temp - 30)/2;
res[i] = (char) temp;
}
printf("%s\n", res);
}
void decrypt(
char word[],
char password[]
)
{
char res[strlen(word)];
int temp;
for (int i = 0; i < strlen(word); i++){
temp = (int) word[i];
temp = temp * 2;
temp = temp + 30;
temp = temp - (int) password[i % strlen(password)];
res[i] = (char) temp;
}
printf("%s\n", res);
}
int main(void) {
encrypt("Hello World", "main");
decrypt("KT[^_1Q_`WW", "main");
return 0;
}
I got the following output
Output :
KT[^_1Q_`WW
GekloWnqkc
I noticed that some of the text are matching while some got + 1 somehow but i am unable to figure out how. While The decrypted text should be Hello World. I am new in programming and started with C. How can I fix this ?