Question

In: Computer Science

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

Solutions

Expert Solution

HERE IS THE CODE AS PER YOUR REQUIREMENT

RAWCODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main(void){
char password[55];
int option;
printf("1. Enter and encrypt a message\n2. View encrypted message\n3. Decrypt and view the message (NOTE: password protected)\n4. Exit\n");
// do-while loop for continuous execution
do{
   printf("\nPlease select an option => ");
   scanf("%d",&option);
   char encrypt[55];
   int p=0;
  
//     if statements for each option
//       To enter a message and encrypt it using xor;

   if(option == 1){
       printf("\nEnter a message => ");
       do{
//         get char method for each character of message
       encrypt[p]=getch();
       if(encrypt[p]!='\r'){
//           printing the "*" character instead of message
       printf("*");
       encrypt[p] = encrypt[p]^1;
       }
       p++;
       }while(encrypt[p-1]!='\r');
       encrypt[p-1]='\0';   
       }
      
//       printing encrypted message
       if(option == 2){
           printf("\n %s ",encrypt);
       }
      
//       printing decrypted message using password
       if(option == 3){
           int count=0;
          
//           continuous while loop for asking password upto 3 times
           while(1){
               int a = 0;
               printf("\nenter the password => ");
               do{
           password[a]=getch();
           if(password[a]!='\r'){
           printf("*");
           }
           a++;
           }while(password[a-1]!='\r');
           password[a-1]='\0';
          
//           password comparison, if okay, prints the decrypted message
               if(strcmp(password,"PRG255") == 0){
                   int i;
                   for(i=0; i<strlen(encrypt); i++){
                       encrypt[i] = encrypt[i]^1;
                   }
                   printf("\n %s",encrypt);
                   break;
               }
//               after 3 iterations, if password still incorrect, the while loop terminates
               else{
                   count++;
                   if(count == 3){
                       printf("\nIncorrect password entered three times! Program will terminate now!");
                       break;
                   }  
               }
           }
              
       }
   }while(option != 4);
}

SCREENSHOTS:

OUTPUT:

IF YOU HAVE ANY QUERIES FEEL FREE TO ASK AT COMMENTS

PLEASE DO LIKE :)


Related Solutions

Write a C++ program that uses all the relational operators.
Write a C++ program that uses all the relational operators.
create "bitty.c" that uses bitwise operators and return statements to implement the functions in "bitty.h". "bitty.c"...
create "bitty.c" that uses bitwise operators and return statements to implement the functions in "bitty.h". "bitty.c" that can only implement "bitty.h" _____________________________________________________________ bitty.h: // Returns 1 if v is an even number, otherwise returns 0 unsigned char isEven(unsigned char v); // Returns 1 if v is zero, otherwise returns 0 unsigned char isZero(unsigned char v); // Returns 1 if a and b have the same value, otherwise 0 unsigned char equals(unsigned int a, unsigned int b); // Returns a if...
Variable Register a $9 b $19 c $2 C Operators + add - subtract & bitwise...
Variable Register a $9 b $19 c $2 C Operators + add - subtract & bitwise and ! bitwise or ~ bitwise not Assume the variables, a, b and c are stored in the registers given above. Give a single MIPS assembly instruction from the MIPS Core Instruction Set that performs the equivalent operation for each of the following C statements. a = ~ ( b | c ); // MIPS equivalent: -------- c = b - 2; // MIPS...
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
Write a C++ program that will make changes in the list of strings by modifying its...
Write a C++ program that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and inserting...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A...
Write a program in c++ that can perform encryption/decryption. In the following let the alphabet A be A={A, a, B, b, . . ., “ ”, “.”,“’ ”}. The encoding is A→0, a→1, B→2, b→4, . . ., Z→50, z→51, “ ”→52, “.”→53 and “’”→54.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT