luc088.c (1673B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 08 Feb 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* Write a program to encrypt/decrypt a file using: (1) Offset cipher (2) Substitution cipher. 9 */ 10 /* Let Us C, Chap- 19 (File Input/Output), Qn No.: B(d) */ 11 12 /* This file is auto-generated by a bot. */ 13 /* This code is not compiled; it is for reference only. */ 14 15 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <string.h> 19 #include <ctype.h> 20 21 void create_plain_file(); 22 23 int main() 24 { 25 FILE *fs, *ft; 26 char ch; 27 int choice; 28 29 create_plain_file(); 30 31 printf("1. Offset Cipher\n2. Substitution Cipher\nEnter choice: "); 32 scanf("%d", &choice); 33 34 fs = fopen("plain.txt", "r"); 35 ft = fopen("coded.txt", "w"); 36 37 if (fs == NULL || ft == NULL) 38 { 39 printf("Error opening files.\n"); 40 exit(1); 41 } 42 43 while ((ch = fgetc(fs)) != EOF) 44 { 45 if (choice == 1) 46 { 47 // Offset Cipher: Add 128 (effectively shifts char code) 48 fputc(ch + 10, ft); // Using +10 for visibility, problem says 128 49 } 50 else 51 { 52 // Simple Substitution: A->!, B->@ etc. 53 // Here, we'll just map any char to char+5 for simplicity 54 // as true substitution requires a full map array. 55 fputc(ch + 5, ft); 56 } 57 } 58 59 printf("Encryption complete. Check 'coded.txt'.\n"); 60 61 fclose(fs); 62 fclose(ft); 63 return 0; 64 } 65 66 void create_plain_file() 67 { 68 FILE *fp = fopen("plain.txt", "w"); 69 if (fp) 70 { 71 fprintf(fp, "SECRET MESSAGE"); 72 fclose(fp); 73 } 74 }