codescracker


c++

C++ Program to Encrypt and Decrypt Files



« Previous Program Next Program »


Encrypt and Decrypt Files in C++

To encrypt and decrypt file's content in C++ programming, you have to ask to enter the file name with extension to encrypt and decrypt the content present inside the file. Now open that file using the function open() and start reading the file's content, 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 content of the file place the content in the original file and later use the same algorithm to decrypt that file's content

C++ Programming Code to Encrypt Files

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

/* C++ Program - Encrypt File */
		
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	char fname[20], ch, choice;
	fstream fps, fpt;
	cout<<"Enter file name (with extension like file.txt) to encrypt : ";
	gets(fname);
	fps.open(fname);
	if(!fps)
	{
		cout<<"Error in opening file..!!";
		cout<<"\nPress any key to exit...";
		getch();
		exit(1);
	}
	fpt.open("temp.txt")
	if(!fpt)
	{
		cout<<"Error in creating temp.txt file..!!";
		fps.close();
		cout<<"\nPress any key to exit...";
		getch();
		exit(2);
	}
	while(fps.eof()==0)
	{
		fps>>ch;
		ch=ch+100;
		fpt<<ch;
	}
	fps.close();
	fpt.close();
	fps.open(fname);
	if(!fps)
	{
		cout<<"Error in opening source file..!!";
		cout<<"\nPress any key to exit...";
		getch();
		exit(3);
	}
	fpt.open("temp.txt");
	if(!fpt)
	{
		cout<<"Error in opening temp.txt file...!!";
		fps.close();
		cout<<"\nPress any key to exit...";
		getch();
		exit(4);
	}
	while(fpt.eof()==0)
	{
		fpt>>ch;
		fps<<ch;
	}
	cout<<"File "<<fname<<" encrypted successfully..!!";
	cout<<"\nPress any key to exit...";
	fps.close();
	fpt.close();
	getch();
}

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

C++ program to encrypt file content

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 :

C++ Programming Code to Decrypt Files

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

/* C++ Program - Decrypt File */
		
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<stdlib.h>
void main()
{
	clrscr();
	char ch, choice, fname[20];
	fstream fps, fpt;
	cout<<"Enter file name (with extension like file.txt) which you have encrypted earlier to decrypt : ";
	gets(fname);
	fps.open(fname);
	if(!fps)
	{
		cout<<"Error in opening source file..!!";
		cout<<"\nPress any key to exit...";
		getch();
		exit(7);
	}
	fpt.open("temp.txt");
	if(!fpt)
	{
		cout<<"Error in opening temp.txt file..!!";
		fps.close();
		cout<<"\nPress any key to exit...";
		getch();
		exit(9);
	}
	while(fpt.eof()==0)
	{
		fpt>>ch;
		ch=ch-100;
		fps<<ch;
	}
	cout<<"File "<<fname<<" decrypted successfully..!!";
	cout<<"\nPress any key to exit...";
	fps.close();
	fpt.close();
	getch();
}

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

C++ program to decrypt file content

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 of your own to encrypt your file's content.

Same Program in Other Language

You may like the same program in other programming language:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test