diff options
| author | Fuwn <[email protected]> | 2023-05-08 00:39:03 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-05-08 00:39:03 -0700 |
| commit | 178423c8bf6268ebcb89241f3755c18ad1bfab67 (patch) | |
| tree | dbf567599d0d97ff70b72425f71e6349383928b2 | |
| parent | build: assignment macro (diff) | |
| download | cst120-178423c8bf6268ebcb89241f3755c18ad1bfab67.tar.xz cst120-178423c8bf6268ebcb89241f3755c18ad1bfab67.zip | |
feat(week_4): initial homework 4
| -rw-r--r-- | cst120/week_4/BUILD | 1 | ||||
| -rw-r--r-- | cst120/week_4/homework_4.c | 115 |
2 files changed, 116 insertions, 0 deletions
diff --git a/cst120/week_4/BUILD b/cst120/week_4/BUILD index 574513f..c60ffcc 100644 --- a/cst120/week_4/BUILD +++ b/cst120/week_4/BUILD @@ -1,3 +1,4 @@ load("//:rules.bzl", "assignment") assignment("quiz_4_program_part") +assignment("homework_4") diff --git a/cst120/week_4/homework_4.c b/cst120/week_4/homework_4.c new file mode 100644 index 0000000..be274d8 --- /dev/null +++ b/cst120/week_4/homework_4.c @@ -0,0 +1,115 @@ +/* Author: Zoltan Szabatin + * Professor: Professor Phong N. + * Project: Homework 4 + * Course: CST 120 */ + +/* Disable security warnings when using MSVC */ +/* #ifdef _MSC_VER +#define _CRT_SECURE_NO_WARNINGS +#endif */ + +#include <ctype.h> /* islower */ +#include <linux/limits.h> +#include <stdio.h> /* printf, scanf */ +#include <stdlib.h> /* malloc, realloc */ +#include <string.h> /* strlen */ + +/* Caesar cipher encryption */ +char *caesar_cipher_encrypt(const char *, const int *); + +/* Rail Fence cipher encryption */ +char *rail_fence_encrypt(const char *); + +int main(void) { + /* Mutable variables for user input */ + int user_key = 0; + char user_go_again = 'z'; + char user_message[PATH_MAX] = {0}; + + /* Keep running while the user requests so */ + do { + char *caesar_cipher_encrypted_message = NULL; + char *rail_fence_encrypted_message = NULL; + + /* Prompt and accept cipher message */ + printf("Enter a message: "); + scanf(" %s", user_message); + + /* Prompt and accept cipher key */ + printf("Enter a key: "); + scanf(" %d", &user_key); + + /* Encrypt user's message by the Caesar cipher, then the Rail Fence cipher + */ + caesar_cipher_encrypted_message = + caesar_cipher_encrypt(user_message, &user_key); + rail_fence_encrypted_message = + rail_fence_encrypt(caesar_cipher_encrypted_message); + + /* Output final double-cipher-ed result */ + printf("%s\n", caesar_cipher_encrypted_message); + + /* Prompt user to repeat */ + printf("Would you like to go again? (y/n) "); + scanf(" %c", &user_go_again); + + /* Free the encrypted messages */ + free((void *)caesar_cipher_encrypted_message); + free((void *)rail_fence_encrypted_message); + } while (user_go_again == 'y' || user_go_again == 'Y'); + + return 0; +} + +char *caesar_cipher_encrypt(const char *message, const int *key) { + size_t message_index = 0; + size_t message_length = (size_t)strlen(message); + char *encrypted_message = malloc((message_length + 1) * sizeof(char)); + + /* **Not** optimized Caesar cipher encryption implementation */ + for (message_index = 0; message_index < message_length; ++message_index) { + if (isupper(message[message_index])) { + encrypted_message[message_index] = + (char)((message[message_index] + *key - 'A') % 26 + 'A'); + } else { + encrypted_message[message_index] = + (char)((message[message_index] + *key - 'a') % 26 + 'a'); + } + } + + encrypted_message[message_length] = '\0'; + + return encrypted_message; +} + +char *rail_fence_encrypt(const char *message) { + size_t message_index = 0, encrypted_message_index = 0; + size_t message_length = (size_t)strlen(message); + char *encrypted_message = malloc((message_length + 1) * sizeof(char)); + + /* Obtain and append message's first rail to the encrypted message */ + for (message_index = 0; message_index < message_length; ++message_index) { + if (message_index % 2 == 0) { + encrypted_message[encrypted_message_index++] = message[message_index]; + } + } + + /* Obtain and append message's second rail to the encrypted message */ + for (message_index = 0; message_index < message_length; ++message_index) { + if (message_index % 2 != 0) { + encrypted_message[encrypted_message_index++] = message[message_index]; + } + } + + /* If the message parity is uneven, append an 'x' to the end. */ + if (message_length % 2 != 0) { + encrypted_message = + realloc(encrypted_message, (message_length + 2) * sizeof(char)); + encrypted_message[message_length] = 'x'; + } + + /* Null terminator to signify C-string status */ + encrypted_message[message_length + 1] = '\0'; + + return encrypted_message; +} |