Write a program that takes a shift value between +/-26 and a plaintext message (
ID: 3784335 • Letter: W
Question
Write a program that takes a shift value between +/-26 and a plaintext message (no spaces) and returns the corresponding ciphertext. The program should also implement a decryption routine that reconstructs the original plaintext from the ciphertext. Example usage $ caesar Enter shift +/- 26: -3 Enter plaintext message (A-Z only, no spaces): THE ciphertext: QEB plaintext: THE Or $ Caesar Enter shift +/- 26: 1 Enter plaintext message (A-Z only, no spaces): ZZZ ciphertext: AAA plaintext: ZZZ assume that the user message only consists of uppercase English alphabet (A-Z).Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
void text_encryption (char cipher_text[26], int sh, int numer)
{
for (int i=0; i < numer; i++)
{
if (cipher_text[i] >= 'A' && cipher_text[i] <= 'Z') //checking the character between A-Z.
{
Cipher_text[i] = char(((cipher_text[i] + sh - 'A' + 26) % 26) + 'A'); //Applying logic
}
else if {
cout<<” Please enter only Uppercase Letters”;
}
}
void text_decryption(char cipher_text[26], int sh, int number)
{
for (int i=0; i < number; i++)
{
if (cipher_text[i] >= 'A' && cipher_text[i] <= 'Z')
{
Cipher_text[i] = char(((cipher_text[i] - sh - 'A' + 26) % 26) + 'A');
}
else if{
cout<<” Result cant be processed as you have entered lower case letters”; } }
int main ()
{
char value [10];
static const char encryption[] = "encryption";
static const char decryption[] = "decryption";
int sh;
char cipher_text[26];
int res1, res2, number;
cout << " Enter Ur Choice: encryption or decryption" << endl;
cin >> value
cout << "Enter shift key value by which u want to shift" << endl;
cin >> sh;
cout << "Enter text u want to encrypt/decrypt" << endl;
cin >> cipher_text;
number = strlen (cipher_text); //counting number of characters
res1 = strcmp (value, encryption);
res2 = strcmp (value, decryption);
if(res1 == 0)
{
value_decryption(cipher_text, sh, number);
}
if(res2 == 0)
{
value_encryption(cipher_text, sh, number); }
cout << "Final Result:" << endl <<cipher_text<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.