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

write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
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):
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...
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.
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’...
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...
Write a program in java that can perform encryption/decryption. In the following let the alphabet A...
Write a program in java 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.
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should...
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players". in python
please solve the following in python: write a program for the decryption of 2-rail fence cipher....
please solve the following in python: write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT