Write a program to decrypt a text file encrypted as in Exercise 6.6

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char ** argv) {

    FILE *in, *tmp;
    const char * fn = "text_message";
    const char * temp = "text_message.bin";
    const static int maxString = 1024;	// read buffer size
    int rc;

    if((in = fopen(fn,"rb+")) == NULL){ 
        printf("Cannot open input file.\n");

    if((tmp = fopen(temp,"wb+")) == NULL){ 
        printf("Cannot open input temp file.\n");

    while ((rc = fgetc(in)) != EOF) {
        if (isspace(rc) || ispunct(rc) || isdigit(rc))
            fputc(rc, tmp);
        else {
                if((rc >= 'F' ) && ( rc <= 'Z'))
                    fputc(tolower(rc - 5), tmp);
                else if(rc == 'E')
                    fputc('z', tmp);
                else if(rc == 'D')
                    fputc('y', tmp);
                else if(rc == 'C')
                    fputc('x', tmp);
                else if(rc == 'B')
                    fputc('w', tmp);
                    fputc('v', tmp);
            } else {
                fputc(rc, tmp);
    char buf[maxString];
    // Copy the content of the temp file back to the original file overriding existing content
    while((fgets(buf, maxString, tmp) )) {  // Reads a line and put in a buffer
		fputs(buf, in);

    return 0;

When you compile and execute the above program it produces the following result on Linux:

File content before decryption


File content after decryption

enemy falling back. breakthrough imminent. lucius.

Brief Explanation

  • A cryptographic algorithm is an algorithm that is used for encryption and decryption.
  • Cryptography refers to the methodology of concealing the content of messages.
  • The program above make use of the Caesar cipher. This method of cryptography was employed by Julius Caesar and hence the name Caesar cipher is used in his honor.
  • The program makes use of a cipher wheel with an offset of five letters as shown below (assume the encrypted message used a cipher wheel with an offset of five letters):

cipher wheel

  • To decrypt a letter, simply find it on the inner wheel and read the corresponding plaintext letter on the outer wheel. To encrypt, reverse this process: find the plaintext letter on the outer wheel and read off the ciphertext letter from the inner wheel.
  • The program above opens a text file called text_message, containing the ciphertext and read each character at a time. If the character is not space, punctuation, digit, lowercase etc decrypt it and write the result to a temp file text_message.bin (binary file). The program does that using the while loop until the end-of-file is reached.
  • The file pointer is set to the beginning of each file using the function rewind.
  • Using the function fgets, the content of the temp file is copied back to the original file overriding existing content.
  • The decrypted message can now be read by Julius Caesar charioteers.

Add comment

Security code