9static int key_shift(
const char *key,
size_t i) {
10 size_t klen = strlen(key);
11 if (klen == 0)
return 0;
12 char k = key[i % klen];
13 if (!isalpha((
unsigned char)k))
return 0;
14 return (
int)(tolower((
unsigned char)k) -
'a');
18 if (!text || !key || strlen(key) == 0)
return;
21 for (
size_t i = 0; text[i] !=
'\0'; i++) {
22 if (!isalpha((
unsigned char)text[i]))
continue;
24 int shift =
key_shift(key, key_index) * direction;
25 char base = isupper((
unsigned char)text[i]) ?
'A' :
'a';
26 int c = (text[i] - base + shift) % 26;
28 text[i] = (char)(base + c);
void vigenere_decrypt(char *text, const char *key)
void vigenere_encrypt(char *text, const char *key)
static int key_shift(const char *key, size_t i)
static void vigenere_apply(char *text, const char *key, int direction)
Vigenère cipher — polyalphabetic shift keyed by a string.