File Encryption using Caesar Cipher

By | August 10, 2015
  The term Encryption is used to refer to the conversion of normal, human-readable data into an unreadable and meaningless collection of bytes. This is done for preventing unauthorised access to confidential data.The process used to extract the original data from the encrypted data is known as Decryption. There are several algorithms available for encrypting data, and each one has its own pros and cons.

File Encryption using Caesar Cipher

Here, we shall discuss File Encryption using Caesar Cipher (also known as Caesar Shift), a technique which was used by Julius Caesar in his private and military communications. This technique uses the method of substitution, i.e. replacing each letter in the text with another letter which is located after a fixed number of positions in the alphabet.


For Example,
  If you substitute each letter with another letter which comes three positions later, then the text
This website is Windows Code Bits
Wklv zhevlwh lv Zlqgrzv Frgh Elwv
As you can see in the above example, the text has been converted to a form which has no meaning and the original message has been thus concealed. Now, if we use a number larger than three, say 20 or 50, then the result would be more complicated and difficult to guess.


Given below is a C++ program to encrypt your data using this method.

File Encryption program

#include <Windows.h>
#include <iostream>

#define BUF_SIZE 256

BOOL encrypt(LPCTSTR fIn, LPCTSTR fOut, DWORD shift)
    HANDLE hIn, hOut;
    DWORD nIn, nOut, iCopy;
    CHAR aBuffer[BUF_SIZE], ccBuffer[BUF_SIZE];
    BOOL success = TRUE;


        return FALSE;


        return FALSE;

    while ((ReadFile(hIn, aBuffer, BUF_SIZE, &nIn, NULL) && nIn > 0 && success))
        for (iCopy = 0; iCopy < nIn; iCopy++)
            ccBuffer[iCopy] = (aBuffer[iCopy] + shift) % 256;
        success = WriteFile(hOut, ccBuffer, nIn, &nOut, NULL);

    return success;

int main()
    LPCTSTR fIn, fOut;
    UINT shift;
    BOOL chk;
    fIn = L"a.txt";
    fOut = L"b.dat";
    shift = -1978;
    chk = encrypt((LPCTSTR)fIn, (LPCTSTR)fOut, (DWORD)shift);

    if (chk == FALSE)
        std::cout << "Encryption wasn't successful !!! Please check whether "a.txt" exists and try again...";
        std::cout << "Encryption succesful !!!";
    return 0;

To use this program, follow these steps:

1. Build the above program as a Win32 Console Application project using Visual Studio or download Encrypt.exe.
2. Place the executable i.e. Encrypt.exe in the same folder as the file you want to encrypt. Make sure you change the name of that file to a.txt.
3. Run the program.
4. If encryption is successful, the program will display the following message in the Console :

Encryption successful !!!

If the program fails to perform its task, it will show the following message :

Encryption wasn’t successful !!! Please check whether “a.txt” exists and try again…


5. If encryption is successful, a new file named b.dat is created in the same folder.
Open b.dat to view its contents. Obviously, you won’t be able to read its contents, because there will be lots of unreadable characters. Now, if you want to decrypt the file’s contents, you have the following two options :
a) Locate this statement : shift = -1978; in the above program and  remove the – sign and re-build the program. Now place the .exe file in the same folder as the b.dat file which you want to decrypt. Run the .exe. The original file with the original text will be created with the name a.txt.

b) Download Decrypt.exe. Place it in the same folder as b.dat and run it. The original file with the original text will be created with the name a.txt.

The following video shows how Caesar Cipher works :

Please note that this method is one of the simplest ways to encrypt data. The data encrypted using this algorithm can be easily decrypted using the decryption programs available. No matter how large is the shift number, the data cannot be secured properly using this algorithm. 
However, you can use it for securing your personal data from your friends and colleagues (who might not know anything about encryption).
Click here to download the Encrypt project (with source code).
Click here to download the Decrypt project (with source code).
(Visited 951 times, 1 visits today)

9 thoughts on “File Encryption using Caesar Cipher

  1. cheap sunglasses

    Of course, what a magnificent website and instructive posts, I surely will bookmark your site.All the Best!

  2. count draco cpp

    I like your website very much. I’m gonna share it with my frnds. I’m sure they’d love it too. This particular article is one of my favourites. Now, I can communicate in secret language with my frnds without others getting a clue about what we’re up to 😀

    1. Mohit Raj Post author

      Caesar Cipher is great for basic data encryption. Like you said, you can use it for communicating with your friends. 😀

  3. Thomas

    Very informative article! I saw one of my friends retweets on twitter which linked here. Really, this is a cool way to encrypt information. Thumbs up 🙂

    1. Mohit Raj Post author

      Just don’t rely upon the Caesar Cipher too much. It’s not suitable for sensitive data. Apart from that, it’s great 🙂

  4. Codolean Codaparte

    Wonderful tutorial. I was watching your youtube video on file encryption. I liked the way you transformed normal english text to some mumbo jumbo. So, I came to view this tutorial and found it even more awesome. I’m currently following your feed

  5. Chen

    I tried your programs- encrypt.exe and decrypt.exe and WOW … encrypt.exe converted my text file to a completely unreadable mess !! And decrypt.exe converted it back to plain text. that’s COOL 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *