Question

In: Computer Science

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 :)

Solutions

Expert Solution

#include<iostream>

using namespace std;

//function to encrypt
void encrypt(char message[],int key)
{
   int i;
   char ch;
   for(i = 0; message[i] != '\0'; ++i){
       ch = message[i];
      
       if(ch >= 'a' && ch <= 'z'){
           ch = ch + key;
          
           if(ch > 'z'){
               ch = ch - 'z' + 'a' - 1;
           }
          
           message[i] = ch;
       }
       else if(ch >= 'A' && ch <= 'Z'){
           ch = ch + key;
          
           if(ch > 'Z'){
               ch = ch - 'Z' + 'A' - 1;
           }
          
           message[i] = ch;
       }
   }
}
//method to decrypt
void decrypt(char message[],int key)
{
   int i;
   char ch;
       for(i = 0; message[i] != '\0'; ++i){
       ch = message[i];
      
       if(ch >= 'a' && ch <= 'z'){
           ch = ch - key;
          
           if(ch < 'a'){
               ch = ch + 'z' - 'a' + 1;
           }
          
           message[i] = ch;
       }
       else if(ch >= 'A' && ch <= 'Z'){
           ch = ch - key;
          
           if(ch > 'a'){
               ch = ch + 'Z' - 'A' + 1;
           }
          
           message[i] = ch;
       }
   }
  
  
}
int main()
{
   char message[100], ch;
   int i, key;
  
   cout << "Enter a message to encrypt: ";
   cin.getline(message, 100);
   cout << "Enter key: ";
   cin >> key;
  
   encrypt(message,key);//calling method to encrypt
   cout << "Encrypted message: " << message<<endl;
  
  
   decrypt(message,key);//calling method to decrypt
   cout << "Decrypted message: " << message<<endl;
   return 0;
}

output:

Enter a message to encrypt: Hello
Enter key: 3
Encrypted message: Khoor
Decrypted message: Hello


Process exited normally.
Press any key to continue . . .


Related Solutions

use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide...
use C++ You will implement the following encryption and decryption functions/programs for the Caesar cipher. Provide the following inputs and outputs for each function/program: EncryptCaesar Two inputs: A string of the plaintext to encrypt A key (a number) ▪ For the Caesar cipher: This will indicate how many characters to shift (e.g. for a key=3, A=>D, B=>E, ..., X=>A, Y=>B, Z=>C). Note that the shift is circular. One output: ◦ A string of the ciphertext or codeword DecryptCaesar Two inputs:...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed...
The mathematical expression of the encryption and decryption process of a Caesar cipher algorithm is expressed respectively as: c=Ep, k=p+k%26                                                                                                                         (1) p=Dc,k=c-k%26                                                                                                                         (2) Please do the following: Write at least two paragraphs to explain the principle of operation of the algorithm. For a full credit, your explanation must show the architectural diagram of the encryption and decryption process. Write a program to implement the Caesar algorithm Code must have two functions; encryption and decryption Test your codes with p as...
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 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.
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch >...
In the caeser cipher encryption and decryption program below, what do the two lines if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } mean??? I understand that it has something to do with ASCII characters and makes sure that if the encryption/decryption character is more than "z", then it would loop back to "a" instead of outputting a charcter like "{" . I...
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of...
Problem 2: Caesar Cipher Decryption] Write a python method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Reverse the operations performed by the encryption method to obtain the plaintext message. The method’s header is as follows: def casesardecryption(s, key):
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and...
Caesar Cipher Encryption] Write a method that takes two parameters: A parameter of type str and a parameter of type int. The first parameter is the plaintext message, and the second parameter is the encryption key. The method strictly does the following tasks: a. Convert the string into a list (let us refer to it as lista). An element in the generated list is the position of the corresponding letter in the parameter string in the English alphabet. Example: ‘C’...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption given a dictionary 3. Perform a caesar decryption given a dictionary 4. Create a random caesar cipher dictionary If user prints: -r : Perform rot13 substitution -g : generate a random caesar cipher dictionary. -e: Encrypt using the caesar cipher -d : Decrypt using the caesar cipher The format for the caesar cipher dictionary is a file with 26 pairs of letters, one per...
In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this...
In cryptography, Caesar cipher is one of the simplest encryption techniques. The key idea of this method is to replace each plaintext letter with one fixed number of places down the alphabet. Below is an example with a shift of three: Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz Cipher: DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABC To cipher a string, ‘A’ is replaced by ‘D’, ‘B’ is substituted by ‘E’, and so on. To decode a string, ‘A’ is replaced by ‘x’, etc. By using python with vs code: Write a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT