-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyalphabetic.c
50 lines (41 loc) · 1.49 KB
/
polyalphabetic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX_LENGTH 100
// Function to perform Vigenere encryption or decryption
void vigenere(char *text, char *key, char *result, int encrypt)
{
int textLen = strlen(text);
int keyLen = strlen(key);
int i, j;
for (i = 0, j = 0; i < textLen; ++i)
{
char shift = toupper(key[j]) - 'A'; // Convert key character to uppercase and calculate shift
if (isalpha(text[i]))
{
char offset = isupper(text[i]) ? 'A' : 'a';
result[i] = encrypt ? (text[i] - offset + shift) % 26 + offset : (text[i] - offset - shift + 26) % 26 +offset;
j = (j + 1) % keyLen; // Move to the next character in the key
}
}
result[i] = '\0';
}
int main()
{
char plaintext[MAX_LENGTH];
char key[MAX_LENGTH];
char encrypted[MAX_LENGTH];
char decrypted[MAX_LENGTH];
printf("Enter the plaintext: ");
fgets(plaintext, MAX_LENGTH, stdin);
plaintext[strcspn(plaintext, "\n")] = '\0'; // Remove newline character if present
printf("Enter the key: ");
// fgets(key, MAX_LENGTH, stdin);
// key[strcspn(key, "\n")] = '\0'; // Remove newline character if present
scanf("%[^\n]", key);
vigenere(plaintext, key, encrypted, 1); // Encrypt
printf("Encrypted text: %s\n", encrypted);
vigenere(encrypted, key, decrypted, 0); // Decrypt
printf("Decrypted text: %s\n", decrypted);
return 0;
}