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...
You are writing a program that encrypts or decrypts messages using a simple substitution cipher. Your...
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 choose encrypt or decrypt, you...
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 Java program that reads an input graph data from a user. Then, it should...
Write a Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number of vertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2 0...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
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...
Write a program that prompts the user to input a string. The program then uses the...
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 of your program and...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT