0% found this document useful (0 votes)
2 views6 pages

Neha

The document contains two C programs for encryption and decryption using the Rail Fence and Caesar ciphers. The Rail Fence cipher program encrypts and decrypts text based on a specified key using a zigzag pattern, while the Caesar cipher program shifts characters in the text by a key value. Both programs prompt the user for input and display the encrypted and decrypted messages.

Uploaded by

Sachhyam Sthapit
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)
2 views6 pages

Neha

The document contains two C programs for encryption and decryption using the Rail Fence and Caesar ciphers. The Rail Fence cipher program encrypts and decrypts text based on a specified key using a zigzag pattern, while the Caesar cipher program shifts characters in the text by a key value. Both programs prompt the user for input and display the encrypted and decrypted messages.

Uploaded by

Sachhyam Sthapit
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
You are on page 1/ 6

Lab 2: Rail Fence Cipher

#include <stdio.h>
#include <string.h>
void encryptRailFence(char *text, int key) {
int len = strlen(text);
char rail[key][len];
int i, j, dir_down = 0, row = 0, col = 0;

for (i = 0; i < key; i++)


for (j = 0; j < len; j++)
rail[i][j] = '\n';

for (i = 0; i < len; i++) {


if (row == 0 || row == key - 1)
dir_down = !dir_down;

rail[row][col++] = text[i];
row += dir_down ? 1 : -1;
}
printf("Encrypted text: ");
for (i = 0; i < key; i++)
for (j = 0; j < len; j++)
if (rail[i][j] != '\n')
printf("%c", rail[i][j]);
printf("\n");
}
void decryptRailFence(char *cipher, int key) {
int len = strlen(cipher);
char rail[key][len];
int i, j, dir_down = 0, row = 0, col = 0;
for (i = 0; i < key; i++)
for (j = 0; j < len; j++)
rail[i][j] = '\n';

for (i = 0; i < len; i++) {


if (row == 0 || row == key - 1)
dir_down = !dir_down;

rail[row][col++] = '*';
row += dir_down ? 1 : -1;
}
int index = 0;
for (i = 0; i < key; i++)
for (j = 0; j < len; j++)
if (rail[i][j] == '*' && index < len)
rail[i][j] = cipher[index++];

row = 0, col = 0;
dir_down = 0;
printf("Decrypted Text: ");
for (i = 0; i < len; i++) {
if (row == 0 || row == key - 1)
dir_down = !dir_down;

printf("%c", rail[row][col++]);
row += dir_down ? 1 : -1;
}
printf("\n");
}
int main() {
char text[100], cipher[100];
int key;

printf("Enter text to encrypt: ");


scanf("%s", text);
printf("Enter a key: ");
scanf("%d", &key);

encryptRailFence(text, key);

printf("Enter text to decrypt: ");


scanf("%s", cipher);
printf("Enter key: ");
scanf("%d", &key);
decryptRailFence(cipher, key);
return 0;
}
Lab 1: Caesar Cipher
#include <stdio.h>
#include <ctype.h>

void encrypt(char text[], int key) {


char ch;
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];

if (isalnum(ch)) {
// Encrypt lowercase letters
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
// Encrypt uppercase letters
else if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}
// Encrypt digits
else if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
} else {
printf("Invalid message\n");
return;
}
text[i] = ch; // Store encrypted character back in the string
}
printf("Encrypted message: %s\n", text);
}
void decrypt(char text[], int key) {
char ch;
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];

if (isalnum(ch)) {
// Decrypt lowercase letters
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
}
// Decrypt uppercase letters
else if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
// Decrypt digits
else if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
} else {
printf("Invalid message\n");
return;
}
text[i] = ch; // Store decrypted character back in the string
}
printf("Decrypted message: %s\n", text);
}
int main() {
char text[500];
int key;

printf("Enter a message to encrypt: ");


scanf("%s", text);
printf("Enter the key: ");
scanf("%d", &key);

// Encrypt the message


encrypt(text, key);

printf("Enter a message to decrypt: ");


scanf("%s", text);
printf("Enter the key: ");
scanf("%d", &key);

// Decrypt the message


decrypt(text, key);

return 0;
}

You might also like