In: Computer Science
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;
}
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.