Neha
Neha
#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;
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';
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;
encryptRailFence(text, key);
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;
return 0;
}