Question

In: Computer Science

Write a program that encrypts and decrypts the user input. Note – Your input should be...

Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user

For Encryption Process:

Take the string and reverse the string.

Encrypt the reverse string with each character replaced with distance value (x) given by the user.

For Decryption:

Take the string and reverse the string.

Decrypt the reverse string with each character replaced with distance value (x) given by the user.

Sample

String input - udc

Encryption process – udc -> xgf (encrypted)

The program should ask the user for input to encrypt, and then display the resulting encrypted output. Next your program should ask the user for input to decrypt, and then display the resulting decrypted output.

Example

Enter phrase to Encrypt (lowercase, no spaces): udc

Enter distance value: 3

Result: xgf

Enter pharse to Decrypt: (lowercase, no spaces): xgf

Enter distance value: 3

Result: udc

Solutions

Expert Solution

Code(C++):-

#include <iostream>
using namespace std;
string reverse(string s)
{
   int n=s.length();
   for(int i=0;i<n/2;i++)
   {
       char c=s[i];
       s[i]=s[n-i-1];
       s[n-i-1]=c;
   }
   return s;
}
string Encrypt(string s,int d)
{
   s=reverse(s); //Reverse String.
   int n=s.length();
   for(int i=0;i<n;i++)
   {
       s[i]=s[i]-97;
       s[i]=(s[i]+d)%26;
       s[i]=s[i]+97;
   }
   return reverse(s);
}
string Decrypt(string s,int d)
{
   s=reverse(s); //Reverse String.
   int n=s.length();
   for(int i=0;i<n;i++)
   {
       s[i]=s[i]-97; //As Lower Case letters start from Ascii 97
       s[i]=(s[i]-d)%26;
       s[i]=s[i]+97;
   }
   return reverse(s);
}
int main()
{
   string s;
   cout<<"Enter phrase to Encrypt (lowercase, no spaces):";
   cin>>s;
   int d;
   cout<<"Enter Distance:";
   cin>>d;
   d=d%26;       //One Complete Cycle of 26 characters in lower alphabet.
  
   string s1=Encrypt(s,d); //Function for Encryption.
  
   cout<<"Result:"<<s1<<endl;
  
   cout<<"Enter phrase to Decrypt (lowercase, no spaces):";
   cin>>s;
   cout<<"Enter Distance:";
   cin>>d;
   d=d%26;       //One Complete Cycle of 26 characters in lower alphabet.
  
   s1=Decrypt(s,d); //Function for Decryption.
  
   cout<<"Result:"<<s1<<endl;

}



Related Solutions

PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or...
Desc: Encrypts or decrypts a file. Input: The user supplies the character '1' to encrypt, or '2' to decrypt via the keyboard.   The user also supplies the name of the source file via the keyboard.   Output: If the user wants to encrypt, the text in input file is encrypted and the encrypted text is stored in "encrypted.txt". The original file is not changed. If the user wants to decrypt, the text in input file is decrypted and the decrypted text...
JAVA Take in a user input. if user input is "Leap Year" your program should run...
JAVA Take in a user input. if user input is "Leap Year" your program should run exercise 1 if user input is "word count" your program should run exercise 2 Both exercises should run in separate methods Exercise 1: write a java method to check whether a year(integer) entered by the user is a leap year or not. Exercise 2: Write a java method to count all words in a string.
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using...
Programing Language: Java The Problem: You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your program will use two constant strings. One will represent the code for encryption: going from the original message (called the plaintext) to the encrypted version of the message. The other will be “abcdefghijklmnopqrstuvwxyz” (the lowercase alphabet. Your program will ask the user whether they want to 1) encrypt a message, 2) decrypt a message, or 3) quit. If they...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
User the Scanner class for your input Write a java program to calculate the area of...
User the Scanner class for your input Write a java program to calculate the area of a rectangle. Rectangle Area is calculated by multiplying the length by the width   display the output as follow: Length =   Width = Area = Load: 1. Design (Pseudocode ) 2. Source file (Java file, make sure to include comments) 3. Output file (word or pdf or jpig file)
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Your Java program should perform the following things:Take the input from the user about the...
Your Java program should perform the following things:Take the input from the user about the patient name, weight, birthdate, and height.Calculate Body Mass Index.Display person name and BMI Category.If the BMI Score is less than 18.5, then underweight.If the BMI Score is between 18.5-24.9, then Normal.If the BMI score is between 25 to 29.9, then Overweight.If the BMI score is greater than 29.9, then Obesity.Calculate Insurance Payment Category based on BMI Category.If underweight, then insurance payment category is low.If Normal...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and...
(CODE IN PYTHON) Program Input: Your program will display a welcome message to the user and a menu of options for the user to choose from. Welcome to the Email Analyzer program. Please choose from the following options: Upload text data Find by Receiver Download statistics Exit the program Program Options Option 1: Upload Text Data If the user chooses this option, the program will Prompt the user for the file that contains the data. Read in the records in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT