Magazine

File Encryption and Decryption Project in C

Posted on the 13 June 2023 by Adam Stiffman

 If you guys are learning c programming then you should try this c programming project because in this project we use file handling which is quite hard for a beginner-level programmer So if you learn c programming and wants to try your knowledge then you should try this program.

File Encryption and Decryption Project in C

File Encryption and Decryption Project in C


I hope You guys know what is encryption and decryption file so let's jump to the program.


Also Check:- Voting System Program In C


File encryption and decryption project in c

This is a Mini C programming project so let's start:-

#include <stdio.h>#include <stdlib.h>voidencryptFile(const char* inputFile, const char* outputFile, int key) { FILE* inputFilePtr = fopen(inputFile, "rb"); FILE* outputFilePtr = fopen(outputFile, "wb"); if (inputFilePtr == NULL || outputFilePtr == NULL) { printf("Error opening files.\n"); return; } int ch; while ((ch = fgetc(inputFilePtr)) != EOF) { ch = ch + key; fputc(ch, outputFilePtr); } fclose(inputFilePtr); fclose(outputFilePtr); printf("File encrypted successfully.\n"); } voiddecryptFile(const char* inputFile, const char* outputFile, int key) { FILE* inputFilePtr = fopen(inputFile, "rb"); FILE* outputFilePtr = fopen(outputFile, "wb"); if (inputFilePtr == NULL || outputFilePtr == NULL) { printf("Error opening files.\n"); return; } int ch; while ((ch = fgetc(inputFilePtr)) != EOF) { ch = ch - key; fputc(ch, outputFilePtr); } fclose(inputFilePtr); fclose(outputFilePtr); printf("File decrypted successfully.\n"); } intmain() { constchar* inputFile = "input.txt"; constchar* encryptedFile = "encrypted.txt"; constchar* decryptedFile = "decrypted.txt"; int key = 5; encryptFile(inputFile, encryptedFile, key); decryptFile(encryptedFile, decryptedFile, key); return0; }

Explanation of encryption and decryption program 

Functions:- 

Function NameDescription

encryptFileEncrypts the given input file using a specified encryption key.

decryptFileDecrypts the given input file using a specified decryption key.

both functions:

ParameterDescription

inputFilegive the file path of the input file from which data will be read.

outputFileThe name of the output file to which the encrypted/decrypted data will be written.

keyThe encryption/decryption key.

Now comes to loops:-

  • Inside the while loop, the function reads each character from the input file using fgetc function until it reaches the end of the file (EOF).

  • If your file having issues opening files then it will show you the error message.

  • After running this hole code file will be closed with the function of 'fclose(inputFilePtr);'.

Main() Function :- 

  • In this function, you have to give the name of the input file and output file.

OUTPUT

Here we are having an error because we don't add the file path to inputFile variable.


file encryption and decryption project in c

file encryption and decryption project in c


Conclusion

I hope you guys understand all about the file encryption and decryption program in c.


We will come with another c programming project so keep visiting our blog. 


Back to Featured Articles on Logo Paperblog