Question

In: Computer Science

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 just need more information and a better theoritical explanation about that line. Thank you! :)

#include

#include

using namespace std;

//Encryption function

void encryption(char msg[100], int key) {

int i;

char ch;

  

for (i = 0; msg[i]; ++i) {

  

ch = msg[i];

  

if (ch >= 'a' && ch <= 'z') {

ch = ch + key;

if(ch > 'z'){

ch = ch - 'z' + 'a' - 1;

}

  

msg[i] = ch;

} else if (ch >= 'A' && ch <= 'Z') {

ch = ch + key;

if(ch > 'Z'){

ch = ch - 'Z' + 'A' - 1;

}

  

  

msg[i] = ch;

}

}

}

//Decryption function

void decryption(char msg[100], int key) {

int i;

char ch;

  

for (i = 0; msg[i]; ++i) {

  

ch = msg[i];

  

if (ch >= 'a' && ch <= 'z') {

ch = ch - key;

if(ch < 'a'){

ch = ch + 'z' - 'a' + 1;

}

  

msg[i] = ch;

} else if (ch >= 'A' && ch <= 'Z') {

ch = ch - key;

if(ch < 'A'){

ch = ch + 'Z' - 'A' + 1;

}

  

msg[i] = ch;

}

}

  

}

int main() {

  

int x;

int key;

char msg[100];

  

cout << "Enter a message: ";

cin.getline(msg, 100);

cout << "Enter key: ";

cin >> key;

  

cout << "Please select the following:\n" << endl;

cout << "1) Encryption" << endl;

cout << "2) Decryption" << endl;

cin >> x;

  

switch (x) {

case 1:

  

encryption(msg, key);

cout << "\nEncrypted message: \n" << msg << endl;

  

break;

  

case 2:

  

decryption(msg, key);

cout << "\nDecrypted message: \n" << msg << endl;

  

break;

  

}

  

return 0;

}

Solutions

Expert Solution

Your assumption is correct these lines are dealing with asci values, char variables store decimal value value of characters, numbers and symbols according to ASCII table. Google any asci table with decimal values and you will be able to map. For example character ‘b’ is stored in char variable named as ch as 98 (decimal value), whereas capital ‘B’ is stored as 66. Small letters decimal range in ascii is 97-122 corresponding to a-z . Note: 97 and 122 both are inclusive in that range. A char variable can store a maximum decimal value of 126.

if ( ch > ‘z’ )         /* This line is checking if decimal value in ch (a char variable) is greater than 122. And if it is greater,

it means it is going out of small letters range. So in that case it will change the value stored. */

{              // This is start of block, this block will execute if condition in ‘if’ block above it is true.

                ch = ch – ‘z’ + ‘a’ + 1;       /* This line is subtracting ‘z’ (122) from ch (which is greter than 122 lets say 124), then adding ‘a’ (97) then adding 1, so the total on right hand side becomes 100 (124 – 122 + 97 + 1). Now 100 is stored in left hand side of ‘=’ , that is ch variable itself. That means after executing this line ch will be having 100 value in it, instead of 126. This is done so that ch variable finally holds a value in range of a-z (97-122). Alphabet ‘d’ has 100 value in ascii decimal form.*/

}              // This is end of if block

If (ch < ‘a’)           /* This line is checking if decimal value stored in ch is smaller than value of alphabet ‘a’ (97). If it is less than 97 that means it is out of a-z range of alphabets */

{              // Start of if block

                ch = ch + ‘z’ – ‘a’ + 1;       /* Here let’s assume value stored in ch is 95 then the right hand side of statement becomes 121 ( 95 + 122 – 97 +1). This value is then assigned to ch, or we can also say value in ch is overwritten to 121 from 95. Now is stores value for alphabet ‘y’ , which is in range of a-z (97-122) */              

}              // End of if block

Hope this clears your doubt. Kindly provide this answer with a thumbs up if you are satisfied with the answer.


Related Solutions

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