-1

I found this Caesar cipher encryption code on the web and I'm trying to understand how it works

#include<stdio.h>

int main()
{
char message[100], ch;
int i, key;

printf("Enter a message to encrypt: ");
gets(message);
printf("Enter key: ");
scanf("%d", &key);

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;
    }
}

printf("Encrypted message: %s", message);

return 0;
}
  • Meaning of if(ch >= 'a' && ch <= 'z') Like does c include the alphabet as an array or something or how does it know that the letter is b or others ?

  • Adding an int to a char in ch = ch + key;

  • this math thing ch = ch - 'Z' + 'A' - 1;

And thanks very very much

user8408288
  • 64
  • 1
  • 7
  • Everything can be pretty much explained if you look at the ASCII values of the characters you're mentioning. – AntonH Dec 29 '17 at 19:44
  • "Does C…know that the letter is…?" Nope, it is the character encoding that you have your compiler using for the "[execution charset](https://www.google.com/search?q="execution+charset")" that defines the values that characters are encoded with. The C language has no such requirement because it is designed to be implemented on different systems. (Since your program uses character literals [encoded per the execution charset], you would expect/make sure that the console/terminal's locale/chcp line up with it and with the C locale that scanf uses. Your C runtime probably detects the console's) – Tom Blodget Dec 30 '17 at 02:16

1 Answers1

0

The codes adds a certain value (key) to each inputed character and "rotates" the character in the range a-z if small caps and A-Z if big caps.

In C each single character has an implicit ascii/int value and the compare operator is used to decide if the inputted character is in the set of characters a-z (which are aligned after each other) or if it is in the set of A-Z which also follow behind each other.

The rest of the code deals with the wraparound if the inputted character plus the key "overshoots" f.e. z or Z and loops it around by substracting the value of 'z' and adding the value of 'a' -1 so that the resulting char is again one in the selected range of a-z or A-Z

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Can you explain the process of subtracting 1 from a or ch z from ch – user8408288 Dec 29 '17 at 19:50
  • 1
    @user8408288 Every single character of message is in turn checked. A character has an implicit integer value (see [ascii table](https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html) ) `a` is 97 , `z` is 122 - if your key is `50` and if your message is `abcd` it is "encrypted" to (`'a'` + `50` = `97 + 50` = `147` which is bigger then the 122 for a `'z'` so you do - `147 - 122 +97 -1` = `25 +97 -1` = `121` = `'y'`) for a etc for `bcd`. – Patrick Artner Dec 29 '17 at 19:56
  • Sorry for bothering you but why a - 1 can't we just say 25 + 97 ? – user8408288 Dec 29 '17 at 20:04
  • 1
    @user8408288 because you would have hardcode values into your sourcecode that are not nesseccary - C understands `'a' + 50 - 'z' + 'a' -1` without any problems. hardcoding these values might seem simpler but then you loose the connection behind this. What is `'a'+50-122+97-1` and why are you doing this - kindof hard to remember in 6months time... – Patrick Artner Dec 29 '17 at 20:08
  • Oh thanks for your help – user8408288 Dec 29 '17 at 20:10
  • 1
    @user8408288 also consider that 'a' is 97 in ASCII, but there's no guarantee the underlying character set will be ASCII. In the event, however unlikely, that somebody invents a new character set where, say, 'a' is now 5891, the code 'a' + 50 will still work, whereas 97 + 50 will fail. – UncaAlby Dec 29 '17 at 21:22