File Handling Program To Encrypt and Decrypt in C Programming
Here, we have listed an article on File Handling to Encrypt and Decrypt in C programming language. With file handling mechanisms, it is easy to encrypt the contents of a file and change the characters into a different set of characters using some Algorithm.
Using the same encryption algorithm, we can, thus, decrypt the same file. The decoder function for decoding or decrypting text files in C language is also in the program code.
Note: We have used Caesar Cipher Technique To Encrypt and Decrypt in C Programming Language.
What is Caesar Cipher Technique?
Caesar Cipher is an old encryption methodology used for encryption of data. Also known as Caesar Shift or Caesar’s Code, it is one of the simplest and a very popular encryption technique.
It is a method in which every letter or character in the plain text is altered and shifted. This plain text character is replaced by a specific character depending upon the algorithm.
The Caesar Cipher Algorithm is one of the oldest and easiest algorithms for Encryption and Decryption Algorithm in C programming language.
This program encrypts the source file and stores the encrypted version of it in the target file.

Encryption of a File in C Programming using Caesar Cipher Technique
Encryption is the process of converting a plain text file into an encrypted code which is a random and Non-understandable text code.
Here, we have used usual file hHandling functions which are quite easy to understand. To encrypt the contents of a file, we need to use some algorithm. We have used a quite simple one which is as follows:
1 2 3 4 | { ch = ch - (8 * 5 - 3); fputc(ch, fp2); } |
C Program To Encrypt Contents of a File
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 | #include<stdio.h> int main() { FILE *fp1, *fp2; char ch; fp1 = fopen("/home/tusharsoni/Desktop/Source","r"); if(fp1 == NULL) { printf("Source File Could Not Be Found\n"); } fp2 = fopen("/home/tusharsoni/Desktop/Target","w"); if(fp2 == NULL) { printf("Target File Could Not Be Found\n"); } while(1) { ch = fgetc(fp1); if(ch == EOF) { printf("\nEnd Of File\n"); break; } else { ch = ch - (8 * 5 - 3); fputc(ch, fp2); } } fclose(fp1); fclose(fp2); printf("\n"); return 0; } |
Decryption of a File in C Programming using Caesar Cipher Technique
Decryption is the process of converting an encrypted Code which is a Random and Non-understandable text code into a plain text file which is understandable.
Here, we have used usual file handling functions which are quite easy to understand. To decrypt the contents of a file, we need to use the same algorithm that we used for encryption of that particular file, but in reverse order. We have used a quite simple one which is as follows:
1 2 3 4 | { ch = ch + (8 * 5 - 3); fputc(ch, fp2); } |
C Program To Decrypt Contents of a File
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 | #include<stdio.h> int main() { FILE *fp1, *fp2; char ch; fp1 = fopen("/home/tusharsoni/Desktop/Target","r"); if(fp1 == NULL) { printf("File 1 Not Found\n"); } fp2 = fopen("/home/tusharsoni/Desktop/etc","w"); if(fp1 == NULL) { printf("File 2 Not Found\n"); } while(1) { ch = fgetc(fp1); if(ch == EOF) { printf("\nEnd Of File\n"); break; } else { ch = ch + (8 * 5 - 3); fputc(ch, fp2); } } fclose(fp1); fclose(fp2); printf("\n"); return 0; } |
If you want to combine encryption and decryption C programs into a single source code file, you should opt for a switch case in C programming. Refer the following code.
Source Code To Encrypt and Decrypt in C Programming
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include<stdio.h> #include<stdlib.h> int encrypt(void); int decrypt(void); int encrypt_view(void); int decrypt_view(void); FILE *fp1, *fp2; char ch; int main() { int choice; while(1) { printf("Select One of the Following:\n"); printf("\n1. Encrypt\n"); printf("2. Decrypt\n"); printf("3. View The Encypted File\n"); printf("4. View The Decrypted File\n"); printf("5. Exit\n"); printf("\nEnter Your Choice:\t"); scanf("%d", &choice); switch(choice) { case 1: encrypt(); break; case 2: decrypt(); break; case 3: encrypt_view(); break; case 4: decrypt_view(); break; case 5: exit(1); } } printf("\n"); return 0; } int encrypt() { printf("\n"); fp1 = fopen("/home/tusharsoni/Desktop/Source","r"); if(fp1 == NULL) { printf("Source File Could Not Be Found\n"); } fp2 = fopen("/home/tusharsoni/Desktop/Target","w"); if(fp2 == NULL) { printf("Target File Could Not Be Found\n"); } while(1) { ch = fgetc(fp1); if(ch == EOF) { printf("\nEnd Of File\n"); break; } else { ch = ch - (8 * 5 - 3); fputc(ch, fp2); } } fclose(fp1); fclose(fp2); printf("\n"); return 0; } int decrypt() { printf("\n"); fp1 = fopen("/home/tusharsoni/Desktop/Target","r"); if(fp1 == NULL) { printf("Source File Could Not Be Found\n"); } fp2 = fopen("/home/tusharsoni/Desktop/Source","w"); if(fp2 == NULL) { printf("Target File Could Not Be Found\n"); } while(1) { ch = fgetc(fp1); if(ch == EOF) { printf("\nEnd Of File\n"); break; } else { ch = ch + (8 * 5 - 3); fputc(ch, fp2); } } fclose(fp1); fclose(fp2); printf("\n"); return 0; } int encrypt_view() { printf("\n"); fp1 = fopen("/home/tusharsoni/Desktop/Target","r"); if(fp1 == NULL) { printf("No File Found\n"); exit(1); } else { while(1) { ch = fgetc(fp1); if(ch == EOF) { break; } else { printf("%c", ch); } } printf("\n"); fclose(fp1); } printf("\n"); return 0; } int decrypt_view() { printf("\n"); fp1 = fopen("/home/tusharsoni/Desktop/Source","r"); if(fp1 == NULL) { printf("No File Found\n"); exit(1); } else { while(1) { ch = fgetc(fp1); if(ch == EOF) { break; } else { printf("%c", ch); } } printf("\n"); fclose(fp1); } return 0; printf("\n"); } |
We hope that you got your file encryption and decryption C program executed successfully. This file handling program to encrypt and decrypt in C programming using Caesar’s Cipher mechanism can be implemented in many other programming languages such as Java, C++, Python using the same approach.
To know more about Caesar Cipher technique, visit WikiPedia. In case you have any doubts or compilation errors to encrypt and decrypt in C programming, let us know about it in the comment section below.
It was a nice tutorial for beginners. Keep role with the things, If needed I have created the same in bit advanced manner with the technic similar to AES in the most simplified manner and shared on Git. You can check with the same if needed. https://github.com/shameerariff/crypt.git
Thanks Shameer.
THanks for explaining the encryption and decryption algorithm. I wanted to know about other encryption algorithms that can be used in C Programming.
Is there any real time system where Caesar Cipher Algorithm is actually implemented?
I don’t think that this caesar cipher encryption and decryption in C programming is used in realtime systems since it is comparatively easy to decode the encrypted files. However, the caesar cipher mechanism is quite good to be implemented in small college projects where there is not much data security requirement. But, if the data is something to be worried about then you should look for other password encryption algorithms.
Can you tell me what are the other algorithms used for Password Encryption and File Encryptions?
1. Triple DES (Data Encryption Standard)
2. Blowfish
3. Twofish
4. Advanced Encryption System (AES)
5. RSA Encryption
This is the best implementation of caesar cipher in c programming. It is so simple, yet amusing.
This way we can even convert text into passwords and make a good password encryption system in c programming.
Such an amazing and a simple c program for encryption and decryption of files. Perfect explanation of the source code.
Amazing code for Caesar Cipher Encryption in C language. Thanks a lot. You have simplified my assignments. 🙂
This Caesar Cipher method can be used to hide data such as passwords with either asterisks or any other masking character using ASII Values.