Question

Data Encryption (Strings and Bitwise Operators) Write a C program that uses bitwise operators (e.g. bitwise...

Data Encryption (Strings and Bitwise Operators)
Write a C program that uses bitwise operators (e.g. bitwise XOR) to encrypt/decrypt a message.
The program will prompt the user to select one of the following menu options:
1. Enter and encrypt a message
2. View encrypted message
3. Decrypt and view the message (NOTE: password protected)
4. Exit
If the user selects option 1, he/she will be prompted to enter a message (a string up to 50 characters long). The program will next encrypt/modify each character of this message before it will be stored into the memory.
If selecting option 2, the user will be able to view on the screen the decrypted message that is stored in the memory.
If the user selects option 3, he/she will then be prompted to enter a password in order to view the original message. (Use “PRG255” as a password.) The user should be limited to three attempts to enter the password. If the user enters the correct password, the program will decrypt the encrypted message and display the original message. If an incorrect password is entered three times the program should show an appropriate message (e.g. “Incorrect password entered three times! Program will terminate now!”) and terminate.
This process will continue until the user chooses the Exit option 4 to terminate the program
NOTE: One of the methods of data encryption uses the XOR bitwise operator with a mask value (an integer number) to encrypt every character of the message and the same operation should be used to decrypt the message.
It is required to submit your source code file, i.e. Lab4.c file as well as a file with your program's run screen captures.

BONUS MARKS:
When message and password are entered by the user the program should NOT display these strings on the screen when they are entered, instead the program will print on the screen a single asterisk (*) representing each character entered. Please note that the actual characters entered by the user will be properly stored in the memory.
A sample run of the program with bonus marks could be as follows:
1. Enter and encrypt a message
2. View encrypted message
3. Decrypt and view the message (NOTE: password protected)
4. Exit
Please select an option => 1
Enter a message => ***********
Please select an option => 3
Enter Password => ******
Message entered: LAB4 PRG255

Homework Answers

Answer #1

C Program:

#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include<conio.h>

int main(void){
  char password[10]="PRG255";
  int at=0;
  char msg[50];
  printf("1.Enter and encrypt a message\n");
  printf("2.View encrypted message\n");
  printf("3.Decrypt and view the message\n");
  printf("4.Exit\n");
  int f=1;
  while(f==1)
  {
    int ch;
    printf("Please select an option=>:");
    scanf("%d",&ch);
    while(getchar()!='\n');
    switch(ch)
    {
      case 1:
        printf("Enter a message=>");
        int i=0;
        do
        {
          msg[i]=getch()^5;
          if(msg[i]^5!='\r')
          {
              printf("*");
          }
          i++;
        }while((msg[i-1]^5)!='\r');
        printf("\n");
        break;
      case 2:
        printf("Decrypted message: %s\n",msg);
        break;
      case 3:
        printf("Enter Password=>");
        i=0;
        char p[50];
        do
        {
          p[i]=getch();
          if(p[i]!='\r')
          {
              printf("*");
          }
          i++;
        }while(p[i-1]!='\r');
        p[i-1]='\0';
        printf("\n");
        if(strcmp(p,password)!=0)
        {
          printf("Wrong Password\n");
          at++;
          if(at>2)
          {
            printf("Incorrect Password entered three times!Program will terminate now!\n");
            f=0;
          }
          break;
        }
        printf("Message entered:");
        int pi=0;
        while((msg[pi]^5)!='\r')
        {
          printf("%c",msg[pi]^5);
          pi++;
        }
        printf("\n");
        break;
      case 4:
       f=0;
       break;
      default:
        printf("Wrong option!Enter between 1 to 4\n");
    }
  }
  printf("Program Terminated\n");
  return 0;
}

Sample Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...
MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Write a C program that prompts the user to enter a line of text on the...
Write a C program that prompts the user to enter a line of text on the keyboard then echoes the entire line. The program should continue echoing each line until the user responds to the prompt by not entering any text and hitting the return key. Your program should have two functions, writeStr and readLn, in addition to the main function. The text string itself should be stored in a char array in main. Both functions should operate on NUL-terminated...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such...
Write a Python program named lastNameVolumes that finds the volumes of different 3 D objects such as a cube, sphere, cylinder and cone. In this file there should be four methods defined. Write a method named cubeVolFirstName, which accepts the side of a cube in inches as an argument into the function. The method should calculate the volume of a cube in cubic inches and return the volume. The formula for calculating the volume of a cube is given below....
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
--USING C# ONLY-- You will create your own on-line shopping store in the console application. You...
--USING C# ONLY-- You will create your own on-line shopping store in the console application. You can choose different products to sell in your store (at least 8 products). You will create an product inventory text file. The program will display two options at the beginning of the program, Customer and Manager. Customer: If this is a new customer, the program will allow the customer to register with their information and assign a unique ID number to the new customer....
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). Extend the ItemToPurchase class per the following specifications              Private fields string itemDescription - Initialized in default constructor to "none" Parameterized constructor to assign item name, item description, item price, and itemquantity (default values of 0).             Public instance member methods setDescription() mutator & getDescription() accessor (2 pts) printItemCost() - Outputs the item name followed by the quantity, price, and subtotal printItemDescription() - Outputs the...