Skip to content

luc088.c

Problem Statement

Write a program to encrypt/decrypt a file using: (1) Offset cipher (2) Substitution cipher.

Metadata

Property Detail
Author Amit Dutta amitdutta4255@gmail.com
Date 08 Feb 2026
License MIT License (See the LICENSE file for details)

Actions

Raw View on GitHub

💡 You can print or save this file by opening Raw and using your browser.

Source Code

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

void create_plain_file();

int main()
{
    FILE *fs, *ft;
    char ch;
    int choice;

    create_plain_file();

    printf("1. Offset Cipher\n2. Substitution Cipher\nEnter choice: ");
    scanf("%d", &choice);

    fs = fopen("plain.txt", "r");
    ft = fopen("coded.txt", "w");

    if (fs == NULL || ft == NULL)
    {
        printf("Error opening files.\n");
        exit(1);
    }

    while ((ch = fgetc(fs)) != EOF)
    {
        if (choice == 1)
        {
            // Offset Cipher: Add 128 (effectively shifts char code)
            fputc(ch + 10, ft); // Using +10 for visibility, problem says 128
        }
        else
        {
            // Simple Substitution: A->!, B->@ etc. 
            // Here, we'll just map any char to char+5 for simplicity 
            // as true substitution requires a full map array.
            fputc(ch + 5, ft);
        }
    }

    printf("Encryption complete. Check 'coded.txt'.\n");

    fclose(fs);
    fclose(ft);
    return 0;
}

void create_plain_file()
{
    FILE *fp = fopen("plain.txt", "w");
    if (fp)
    {
        fprintf(fp, "SECRET MESSAGE");
        fclose(fp);
    }
}