0% found this document useful (0 votes)
17 views3 pages

Program 1

This C program implements a simple encryption and decryption mechanism using a key for a given plaintext. It ensures that both the plaintext and key consist of uppercase A-Z letters and are of the same length. The program encrypts the plaintext using the key and then decrypts the ciphertext back to the original plaintext.

Uploaded by

antarasartale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Program 1

This C program implements a simple encryption and decryption mechanism using a key for a given plaintext. It ensures that both the plaintext and key consist of uppercase A-Z letters and are of the same length. The program encrypts the plaintext using the key and then decrypts the ciphertext back to the original plaintext.

Uploaded by

antarasartale11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#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;

You might also like