Write a program to encrypt a text file using the Caeser shift cipher.

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 >= 'a' ) && ( rc < 'v'))
                    fputc(toupper(rc + 5), tmp);
                else if(rc == 'v')
                    fputc(65, tmp);  // 'A'
                else if(rc == 'w')
                    fputc(66, tmp);  // 'B'
                else if(rc == 'x')
                    fputc(67, tmp);  // 'C'
                else if(rc == 'y')
                    fputc(68, tmp);  // 'D'
                    fputc(69, tmp);  // 'E'
            } 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 encryption

enemy falling back. breakthrough imminent. lucius.

File content after encryption


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:

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 plaintext and read each character at a time. If the character is not space, punctuation, digit, uppercase etc encrypt 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 encrypted message can now be sent by Julius Caesar to his charioteers.

Add comment

Security code