Question: Help, please! What's wrong with my C code? It isn't working. Can you write the correct decryption and encryption logic? And include the railcipher.c ,

Help, please! What's wrong with my C code? It isn't working. Can you write the correct decryption and encryption logic? And include the railcipher.c, railcipher.h, and makefile. The screen shot at the end is what the 'Expected Outcome' should be once you compile and run the code (the cipher text as shown in the screen shot is supposed to be converted to uppercase): #include "railcipher.h"
#include
#include
#include
#include
#define MAX_TEXT_LENGTH 1024
#define MAX_KEY_LENGTH 26
int main(int argc, char *argv[]){
// Array to store encryption key
int key[MAX_KEY_LENGTH];
// Validate the encryption key provided as command-line arguments
int keyLength = validateKey(argc, argv, key);
// If key validation fails, exit the program
if (keyLength ==-1)
return 1;
// Buffers to store user input command and result
char command[MAX_TEXT_LENGTH +8], result[MAX_TEXT_LENGTH];
// Prompt user for command
printf("Enter command (encrypt/decrypt/quit): ");
// Loop to read commands until the user enters "quit"
while (fgets(command, sizeof(command), stdin)){
// Remove trailing newline
command[strcspn(command,"
")]=0;
// If user enters "quit", exit the loop
if (strncmp(command, "quit", 4)==0){
break;
} else {
// Process user command
processCommand(command, key, keyLength, result);
// Print the result
printf("%s
", result);
}
// Prompt user for next command
printf("Enter command (encrypt/decrypt/quit): ");
}
return 0;
}
// Function to validate the encryption key provided as command-line arguments
int validateKey(int argc, char *argv[], int *key){
// Check if sufficient command-line arguments are provided
if (argc 3){
printf("Usage: %s
", argv[0]);
return -1;
}
// Extract key length from command-line arguments
int keyLength = atoi(argv[1]);
// Check if key length is within acceptable bounds
if (keyLength 1|| keyLength > MAX_KEY_LENGTH){
printf("Error: Invalid key length.
");
return -1;
}
// Parse and store key elements
for (int i =0; i keyLength; i++){
key[i]= atoi(argv[i +2]);
// Additional validation for the key elements can be added here
}
// Validation passed, return key length
return keyLength;
}
// Function to process user command
void processCommand(const char *command, const int *key, int keyLength, char *result){
// Buffer to store text extracted from command
char text[MAX_TEXT_LENGTH];
// Attempt to extract command and associated text from input
if (sscanf(command, "encrypt %1023s", text)==1){
// If command is "encrypt", call encryption function
encrypt(text, key, keyLength, result);
} else if (sscanf(command, "decrypt %1023s", text)==1){
// If command is "decrypt", call decryption function
decrypt(text, key, keyLength, result);
} else {
// If command is unrecognized, set result to error message
strcpy(result, "Invalid command. Please enter 'encrypt', 'decrypt', or 'quit'.");
}
}
// Function to perform rail fence cipher encryption
void encrypt(const char matrix[][7], int rows, const int* key, char* ciphertext){
int index =0;
for (int k =0; k 7; ++k){
int col = key[k]-1;
for (int i =0; i rows; ++i){
ciphertext[index++]= toupper(matrix[i][col]);
}
}
ciphertext[index]='\0';
}
void decrypt(const char* ciphertext, int rows, const int* key, char* plaintext){
char matrix[rows][7];
int colLength = strlen(ciphertext)/ rows;
for (int k =0; k colLength; ++k){
int col = key[k]-1;
for (int i =0; i rows; ++i){
matrix[i][col]= tolower(ciphertext[i * colLength + k]);
}
}
int index =0;
for (int i =0; i rows; ++i){
for (int j =0; j colLength; ++j){
plaintext[index++]= matrix[i][j];
}
}
plaintext[index]='\0';
}
 Help, please! What's wrong with my C code? It isn't working.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!