Example Program
#include <stdio.h> #include <ctype.h> #define MAXSIZE 1024 void encrypt(char*); void decrypt(char*); int menu(); int main(void) { char c, choice[2], s[MAXSIZE]; while(1) { menu(); gets(choice); if((choice[0]=='e')||(choice[0]=='E')) { puts("Input text to encrypt->"); gets(s); encrypt(s); } else if((choice[0]=='d')||(choice[0]=='D')) { puts("Input text to decrypt->"); gets(s); decrypt(s); } else break; } return 0; } void encrypt(char*str) { int n=0; char *p=str, q[MAXSIZE]; while(*p) { if(islower(*p)) { if((*p>='a')&&(*p<'x')) q[n]=toupper(*p + (char)3); else if(*p=='x') q[n]='A'; else if(*p=='y') q[n]='B'; else q[n]='C'; } else { q[n]=*p; } n++; p++; } q[n++]='\0'; puts(q); } void decrypt(char*str) { int n=0; char *p=str, q[MAXSIZE]; while(*p) { if(isupper(*p)) { if((*p>='D')&&(*p<='Z')) q[n]=tolower(*p - (char)3); else if(*p=='A') q[n]='x'; else if(*p=='B') q[n]='y'; else q[n]='z'; } else { q[n]=*p; } n++; p++; } q[n++]='\0'; puts(q); } int menu() { puts("To encrypt, input e or E\n"); puts("To decrypt, input d or D\n"); puts("To exit, input any other letter\n"); puts("Your choice:->\n"); return 0; }
Code Analysis
The main function does the following:- First we include the stdio.h and ctype.h
- Then we create a macro for maximum sentence size. In this example, it is 1024.
- There are a few declarations to reserve place for things that we use in our code.
- While loop will repeat until user inputs proper letter to stop the program.
- In the while loop, we call the function menu(), which will display the menu to the user.
- When you input the letter, function gets() reads your choice. According to the user input appropriate function would be called.
- One function encrypts the text, and the other function decrypts it.
- First function gets one string into it, and modifies it. After that, we are changing each letter according to the rule we need to apply.
- The pointer q is a helper to read the original string, and the q is used to store the output.
- tolower() will transform the letter into lower case. toupper() will transform the letter into upper case.
- Function gets() is used to read the input string from user.
- To encrypt, this code will move letters to a different offset by 3 spaces in ASCII table. Also, at the end of alphabet you wrap around and replace: x, y and z, with: a, b and c.
- Instead of char type, use wcahr_t symbols that could be good for languages other than English. There are usually similar functions that will work with two byte letters. Sometimes it is enough to use one additional w.
When we talk about breaking Caesars cipher, first algorithm that could be applied is statistical decryption. For each language, there are usual frequencies of each letter and they could be used to figure out the encrypted text without getting the key. On a related subject, you should also explore how Vigener’s cipher works.
Again, it is very easy to break the encrypted text generated by this example. The above code is given only for learning purpose to understand how this works.