×
Samples Blogs Make Payment About Us Reviews 4.9/5 Order Now

C-Language Program to Implement Encryption Assignment Solution

June 11, 2024
Johanna Louis
Johanna Louis
🇦🇹 Austria
C
Johanna Louis, PhD in Computer Science from the University of Klagenfurt, Austria. With 8 years of extensive experience in C programming assignments, I specialize in providing expert solutions and guidance in this field.
Key Topics
  • Instructions
  • Requirements and Specifications
Tip of the day
Break your Ruby assignments into smaller methods for modularity and better debugging. Use testing frameworks like RSpec to validate code functionality and ensure robust solutions.
News
Programming students should explore Mojo, a fast-emerging language in 2024 that enhances AI development with speeds up to 35,000 times faster than Python and robust efficiency through MLIR.

Instructions

Objective

Write a program to implement encryption in C-language

Requirements and Specifications

Assignment Overview and Requirements

You are to write a C assignment program that performs simple encryption and decryption on strings entered by the user. For this task, you will implement an encryption technique known as the Caesar Cipher.

Caesar Cipher as encryption method

A simple way to encrypt data is attributed to Julius Caesar, the Roman Emperor, and is therefore known as Caesar Cipher.

This method takes each character in a message and replaces it with one which is a certain distance (offset) along the alphabet from it (moving right).

For example:

123456789.........
ABCDEFGHI......

+3

ABCDEFGHIJ........

If the offset is 3 then A becomes D, B becomes E, C becomes F etc. In this case the word DIG encrypts to GLJ. In order to decrypt the word/string, simply offset by the same amount moving in the opposite direction (i.e. moving left).

Instead of restricting the cipher to the alphabetic characters only, we will use all the printable ASCII characters. That is, all the characters from ASCII 32 (Space) to ASCII 126 (~).

Requirements

You are required to write a menu driven program called yourEmailId_encryptor.c that will allow the user to enter commands and process these commands until the quit command is entered.

The following commands should be allowed:

  • Encrypt string:
  • Prompt for and read (from the keyboard) a string to be encrypted. The program should then ask for the offset value (limited to a range of 1 to 94 inclusive). Display the encrypted string to the screen.

  • Decrypt string:
  • Decrypt an encrypted message by prompting for and reading (from the keyboard) a string to be decrypted. The program should then ask for the offset value (limited to the range of 1 to 94 inclusive). Display the decrypted string to the screen.

  • Brute force decryption:
  • If the offset is not known, we can implement a brute force decryption algorithm that tries all of the 94 possible Caesar offsets in order to decrypt the encrypted text. Prompt for and read the string to be decrypted and display all 94 possible decrypted strings to the screen.

  • Quit:
  • Quits the program displaying a goodbye message to the screen.

Source Code

#define _CRT_SECURE_NO_WARNINGS #include #include #define MAX_SIZE 200 void display_details(); int get_menu_choice(); int get_offset(); int main() { // Create char array to store string char string[MAX_SIZE]; // Create char array to stroe a copy of the string, used in the brute-force section char string_copy[MAX_SIZE]; // Char variable to store chars char c; // Integer variable to store the ASCII value of a char int ci; // Create char array to store encrypted string char encrypted[MAX_SIZE]; // Create char array to store decrypted string char decrypted[MAX_SIZE]; // Variable to store offset int offset; // Variable to store menu option int option; // Begin with program display_details(); int program_running = 1; // Variable to control the while loop char cc; while(program_running) { // First, clear all character arrays for(int i = 0; i < MAX_SIZE; i++) { string[i] = 0; encrypted[i] = 0; decrypted[i] = 0; } printf("String: %s\n", string); printf("Encrypted: %s\n", encrypted); printf("Decrypted: %s\n", decrypted); // Display menu and get option option = get_menu_choice(); if(option == 1) // Encrypt { // Get String to encrypt printf("Enter string to encrypt: "); //scanf("%s", string); fgets(string, MAX_SIZE, stdin); // Remove the \n character string[strcspn(string, "\n")] = '\0'; // Get offset offset = get_offset(); // Now, encrypt for(int i = 0; string[i] != '\0'; i++) { c = string[i]; ci = (int)c; // get ascii value if(ci >= 32 && ci <= 126) { // Encrypt ci = ci + offset; if(ci > 126) // out of bounds { ci -= 126; } c = (char)ci; // Add to the encrypted array encrypted[i] = c; } else { encrypted[i] = c; } } // print encrypted message printf("Encrypted string: %s\n", encrypted); } else if(option == 2) // Decrypt { // Get String to decrypt printf("Enter string to decrypt: "); //scanf("%s", string); fgets(string, MAX_SIZE, stdin); // Remove the \n character string[strcspn(string, "\n")] = '\0'; // Get offset offset = get_offset(); // Now, decrypt for(int i = 0; string[i] != '\0'; i++) { c = string[i]; ci = (int)c; // get ascii value if(ci >= 32 && ci <= 126) { // Decrypt ci = ci - offset; if(ci < 32) // out of bounds { ci += 126; } c = (char)ci; // Add to the encrypted array decrypted[i] = c; } else { decrypted[i] = c; } } // print encrypted message printf("Decrypted string: %s\n", decrypted); } else if(option == 3) // brute forde decryption { printf("Enter string to decrypt: "); fgets(string, MAX_SIZE, stdin); // Remove the \n character string[strcspn(string, "\n")] = 0; // Loop from 1 to 94 to try every offset for(int offset = 1; offset <= 94; offset++) { // Create a copy of string for(int i = 0; string[i] != '\0'; i++) { string_copy[i] = string[i]; } //strcpy(string_copy, string); // Now, decrypt for(int i = 0; string_copy[i] != '\0'; i++) { c = string_copy[i]; ci = (int)c; // get ascii value if(ci >= 32 && ci <= 126) { // Decrypt ci = ci - offset; if(ci < 32) // out of bounds { ci += 126; } c = (char)ci; // Add to the encrypted array decrypted[i] = c; } else { decrypted[i] = c; } } printf("Offset %d - decrypted string: %s\n", offset, decrypted); } } else if(option == 4) // Exit { printf("Good bye.\n"); program_running = 0; } } return 0; } void display_details() { /* Display details about the program */ printf("File:\t\tyourEmailId_encrytor.c\n"); printf("Author:\t\tBatman\n"); printf("Stud ID:\t\t0123456X\n"); printf("Email ID:\t\tyourEmailId\n"); printf("This is my own work as defined by the University's Academic Misconduct Policy.\n"); } int get_menu_choice() { /* This function will ask for an integer to user. The integer must be between 1 and 4 */ int option; while(1) { // Display menu printf("*** Menu ***\n\n"); printf("1. Encrypt string\n"); printf("2. Decrypt string\n"); printf("3. Brute force decryption\n"); printf("4. Quit\n\n"); printf("What would you like to do [1,2,3,4]? "); scanf("%d", &option); getchar(); if(option >= 1 && option <= 4) { return option; } else { printf("Invalid option. Try again.\n"); } } } int get_offset() { /* This function asks user for a positive integer between 1 and 94 */ int offset; while(1) { printf("Please enter offset value (1 to 94): "); scanf("%d", &offset); getchar(); if(offset >= 1 && offset <= 94) { return offset; } else { printf("Invalid offset.\n"); } }

Similar Samples

Explore our diverse collection of programming homework samples to understand the quality and expertise we offer. Each sample showcases our comprehensive approach to solving complex programming challenges across various languages and topics. Let these examples guide you in making an informed decision to trust us with your programming assignments.