luc088.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Write a program to encrypt/decrypt a file using: (1) Offset cipher (2) Substitution cipher.
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#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);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59