#include <stdio.
h>
#include <string.h>
#include <ctype.h>
// Encrypts plaintext using key
void encrypt(char *plaintext, char *key, char *ciphertext) {
int i;
for (i = 0; plaintext[i] != '\0'; i++) {
// Ensure characters are uppercase A-Z
char p = toupper(plaintext[i]);
char k = toupper(key[i]);
if (p < 'A' || p > 'Z' || k < 'A' || k > 'Z') {
printf("Error: Only A-Z letters are allowed.\n");
ciphertext[0] = '\0';
return;
int pVal = p - 'A';
int kVal = k - 'A';
ciphertext[i] = ((pVal + kVal) % 26) + 'A';
ciphertext[i] = '\0'; // Null-terminate the string
}
// Decrypts ciphertext using key
void decrypt(char *ciphertext, char *key, char *plaintext) {
int i;
for (i = 0; ciphertext[i] != '\0'; i++) {
char c = toupper(ciphertext[i]);
char k = toupper(key[i]);
int cVal = c - 'A';
int kVal = k - 'A';
plaintext[i] = ((cVal - kVal + 26) % 26) + 'A';
plaintext[i] = '\0';
int main() {
char plaintext[100], key[100], ciphertext[100], decrypted[100];
printf("Enter plaintext (A-Z only): ");
scanf("%s", plaintext);
printf("Enter key (same length, A-Z only): ");
scanf("%s", key);
if (strlen(plaintext) != strlen(key)) {
printf("Error: Plaintext and key must be the same length.\n");
return 1;
encrypt(plaintext, key, ciphertext);
printf("Encrypted: %s\n", ciphertext);
decrypt(ciphertext, key, decrypted);
printf("Decrypted: %s\n", decrypted);
return 0;