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