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

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’...
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...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its...
Encrypting Text with a Caesar Cipher Write a C program caesar.c which reads characters from its input and writes the characters to its output encrypted with a Caesar cipher. A Caesar cipher shifts each letter a certain number of positions in the alphabet. The number of positions to shift will be given to your program as a command line argument. Characters other than letters should not be encrypted. Your program should stop only at the end of input. Your program...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means...
My question below Write a complete program that performs encryption/decryption of a text file. Encryption means you need to scramble the words of a file to become secure. Decryption means you need to apply some rules to an encrypted file in order to find the original file with its original content. An example of encryption is to replace each letter in a file with its consecutive letter in the alphabet. Therefore, ‘a’ is replaced by ‘b’, ‘b’ is replaced by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT