codescracker


c

C Program to Encrypt and Decrypt Files



« Previous Program Next Program »


Encrypt and Decrypt Files in C

To encrypt and decrypt files in C programming, you have to open that file and start reading the file character by character, at the time of reading make some algorithm to encrypt the content of the file and place the content in the temporary file then after encrypting all the content of the file place the content in the original file and later use the same algorithm to decrypt the file

C Programming Code to Encrypt and Decrypt Files

Here are one by one program to encrypt and decrypt files in C programming. Let's first start, how to encrypt files in C programming.

Encrypt Files in C

Following C program ask to the user to enter the file name to encrypt its content:

C Programming Code to Encrypt Files

/* C Program - Encrypt Files */
		
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	char fname[20], ch, choice;
	FILE *fps, *fpt;
	printf("Enter file name (with extension like file.txt) to encrypt : ");
	gets(fname);
	fps=fopen(fname, "r");
	if(fps==NULL)
	{
		printf("Error in opening file..!!");
		printf("\nPress any key to exit...");
		getch();
		exit(1);
	}
	fpt=fopen("temp.txt", "w");
	if(fpt==NULL)
	{
		printf("Error in creating temp.txt file..!!");
		fclose(fps);
		printf("\nPress any key to exit...");
		getch();
		exit(2);
	}
	while(1)
	{
		ch=fgetc(fps);
		if(ch==EOF)
		{
			break;
		}
		else
		{
			ch=ch+100;
			fputc(ch, fpt);
		}
	}
	fclose(fps);
	fclose(fpt);
	fps=fopen(fname, "w");
	if(fps==NULL)
	{
		printf("Error in opening source file..!!");
		printf("\nPress any key to exit...");
		getch();
		exit(3);
	}
	fpt=fopen("temp.txt", "r");
	if(fpt==NULL)
	{
		printf("Error in opening temp.txt file...!!");
		fclose(fps);
		printf("\nPress any key to exit...");
		getch();
		exit(4);
	}
	while(1)
	{
		ch=fgetc(fpt);
		if(ch==EOF)
		{
			break;
		}
		else
		{
			fputc(ch, fps);
		}
	}
	printf("File %s encrypted successfully..!!", fname);
	printf("\nPress any key to exit...");
	fclose(fps);
	fclose(fpt);
	getch();
}

When the above c program is compile and executed, it will produce the following output:

c program to encrypt files

After running the above program, you can check your file that you have entered. You will see that your file's content will be encrypted. Now go though the following C program to know how to decrypt this file :

Decrypt Files in C

Following C program ask to the user to enter the file name (enter that file name which you have encrypted earlier) to decrypt that file:

C Programming Code to Decrypt Files

/* C Program - Decrypt Files */
		
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	char ch, choice, fname[20];
	FILE *fps, *fpt;
	printf("Enter file name (with extension like file.txt) which you have encrypted earlier to decrypt : ");
	gets(fname);
	fps=fopen(fname, "w");
	if(fps==NULL)
	{
		printf("Error in opening source file..!!");
		printf("\nPress any key to exit...");
		getch();
		exit(7);
	}
	fpt=fopen("temp.txt", "r");
	if(fpt==NULL)
	{
		printf("Error in opening temp.txt file..!!");
		fclose(fps);
		printf("\nPress any key to exit...");
		getch();
		exit(9);
	}
	while(1)
	{
		ch=fgetc(fpt);
		if(ch==EOF)
		{
			break;
		}
		else
		{
			ch=ch-100;
			fputc(ch, fps);
		}
	}
	printf("File %s decrypted successfully..!!",fname);
	printf("\nPress any key to exit...");
	fclose(fps);
	fclose(fpt);
	getch();
}

When the above c program is compile and executed, it will produce the following result:

c program to decrypt files

Now you can watch your file's content, you will see that your file's content will be decrypted i.e., you will watch your original content.

Note - You can use a lot of algorithm and technique of your own to encrypt and decrypt your file's content.

Same Program in Other Programming Language

You may also like same program in other programming language:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test