C++ Implementation of Substitution Cipher

This simple C++ implementation of a substitution cipher—specifically the Caesar cipher—demonstrates how basic cryptographic techniques can be used with file handling.

The Caesar cipher is one of the oldest and simplest substitution cipher techniques. It works by shifting the letters in a message by a fixed number of positions in the alphabet. Unlike transposition ciphers, which rearrange character positions, substitution ciphers replace characters with others based on a defined scheme.

This blog post demonstrates a C++ implementation that reads a message from a file, performs Caesar cipher encryption or decryption, and writes the result to another file.


How It Works

  1. Input and Output Files:
    • Input is read from a file named anip.txt.
    • Output is written to a file named anop.txt.
  2. User Choices:
    • You can choose between encryption and decryption.
    • Provide a key (shift amount), e.g., 2.
  3. Caesar Cipher Logic:
    • For encryption, each letter is shifted forward in the alphabet by the key.
    • For decryption, each letter is shifted backward by the key.
    • Non-alphabet characters are left unchanged.

C++ Implementation

#include<iostream>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
using namespace std;

int main()
{
    FILE *in, *op;          // File pointers for input and output
    int key, t1, ope;        // Key for shifting, temp variable, operation choice
    char c;                  // Character variable for processing

    in = fopen("anip.txt", "r");    // Open input file in read mode
    op = fopen("anop.txt", "w");    // Open output file in write mode

    // Ask user for operation
    cout << "\n What do you want to perform:";
    cout << "\n 1. Encryption";
    cout << "\n 2. Decryption";
    cin >> ope;

    // Ask for the Caesar cipher key
    cout << "\n Enter Key:";
    cin >> key;

    if (ope == 1) {
        // --- Encryption Logic ---
        while (!feof(in)) {
            c = fgetc(in);            // Read character from file
            if (c == -1) break;       // Break on EOF

            if (isupper(c)) {
                // Shift uppercase characters
                t1 = (int)c - (int)'A';
                t1 = (t1 + key) % 26;
                t1 = t1 + (int)'A';
                fputc((char)t1, op);
            } else if (islower(c)) {
                // Shift lowercase characters
                t1 = (int)c - (int)'a';
                t1 = (t1 + key) % 26;
                t1 = t1 + (int)'a';
                fputc((char)t1, op);
            } else {
                // Leave non-alphabet characters unchanged
                fputc(c, op);
            }
        }
    } else {
        // --- Decryption Logic ---
        while (!feof(in)) {
            c = fgetc(in);            // Read character from file
            if (c == -1) break;       // Break on EOF

            if (isupper(c)) {
                // Reverse shift for uppercase characters
                t1 = (int)c - (int)'A';
                t1 = (t1 - key + 26) % 26;
                t1 = t1 + (int)'A';
                fputc((char)t1, op);
            } else if (islower(c)) {
                // Reverse shift for lowercase characters
                t1 = (int)c - (int)'a';
                t1 = (t1 - key + 26) % 26;
                t1 = t1 + (int)'a';
                fputc((char)t1, op);
            } else {
                // Leave non-alphabet characters unchanged
                fputc(c, op);
            }
        }
    }

    fclose(in);   // Close input file
    fclose(op);   // Close output file
    cout << "\n Done"; // Notify completion
    getch();      // Wait for key press
    return 0;
}

Sample Example

Input File (<strong>anip.txt</strong>):

This Is Example File Which Contains YZ

User Input:

Choose: 1 (Encryption)
Enter Key: 2

Encrypted Output File (<strong>anop.txt</strong>):

Vjku Ku Gzcorng Hknn Ykije Eqpvckpu AB

Explanation:

  • ‘T’ → ‘V’ (shifted by 2)
  • ‘h’ → ‘j’
  • ‘i’ → ‘k’
  • ‘s’ → ‘u’
  • Spaces remain unchanged
  • ‘Y’ → ‘A’ (cyclic shift past ‘Z’)
  • ‘Z’ → ‘B’

Now for decryption the user chooses:

Choose: 2 (Decryption)
Enter Key: 2

and provides the encrypted file as input

Then the output will be:

This Is Example File Which Contains YZ

One thought on “C++ Implementation of Substitution Cipher”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.