codescracker
c++

C++ Program to Encrypt and Decrypt a File



« Previous Program Next Program »

In this article, you will learn and get code on file encryption and decryption. That is, code to encrypt the data (content) of a textual file. And another code to decrypt the same data of a textual file.

What an Encryption and Decryption Means ?

Encryption of a data means converting the data from original form into a coded form. The coded form of original data can not be read by an unauthorized person.

Decryption of a data means, converting data from coded form to its original form.

Encrypted data can only be accessed by an authorized person.

Note - Authorized person knows the decryption key. Decryption key is a password or formula that is used to convert cyphertext to plaintext

Note - Encrypted data is known as cyphertext, whereas decrypted data (original data) is known as plaintext.

Therefore in simple language, converting data from plaintext to cyphertext is known as data encryption. Whereas converting data from cyphertext to plaintext is known as data decryption.

Things to Do before Program

Because the program given below encrypts the data of a text file and then further decrypt the same data with another program. We must have to create a textual file say, codescracker.txt in current directory.

The current directory means, the directory where the C++ souce code is saved. Therefore because I'm going to save the program of encrypt and decrypt a file in cpp programs folder. Then in the same folder, creates a file named codescracker.txt with following content inside it:

username = codescrackerUser
password = codescrackerPassword

Here is the snapshot of the folder, cpp programs that contains file, codescracker.txt:

file encryption c++

And here is the snapshot of the opened file, codescracker.txt:

file decryption c++

Now let's move on to the program given below, to encrypt the data of this newly created file and then further decrypt the same data.

Encrypt a File in C++

To encrypt file's content in C++ programming, you have to ask from user to enter the name of file (with extension). And then encrypt the content present inside the file as shown in the program given below.

#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
    char fileName[30], ch;
    fstream fps, fpt;
    cout<<"Enter the Name of File: ";
    gets(fileName);
    fps.open(fileName, fstream::in);
    if(!fps)
    {
        cout<<"\nError Occurred, Opening the Source File (to Read)!";
        return 0;
    }
    fpt.open("tmp.txt", fstream::out);
    if(!fpt)
    {
        cout<<"\nError Occurred, Opening/Creating the tmp File!";
        return 0;
    }
    while(fps>>noskipws>>ch)
    {
        ch = ch+100;
        fpt<<ch;
    }
    fps.close();
    fpt.close();
    fps.open(fileName, fstream::out);
    if(!fps)
    {
        cout<<"\nError Occurred, Opening the Source File (to write)!";
        return 0;
    }
    fpt.open("tmp.txt", fstream::in);
    if(!fpt)
    {
        cout<<"\nError Occurred, Opening the tmp File!";
        return 0;
    }
    while(fpt>>noskipws>>ch)
        fps<<ch;
    fps.close();
    fpt.close();
    cout<<"\nFile '"<<fileName<<"' Encrypted Successfully!";
    cout<<endl;
    return 0;
}

This program was build and run under Code::Blocks IDE. Here is its sample run:

C++ program to encrypt file content

Now enter the name of file say codescracker.txt and press ENTER key to encrypt its data. Here is the final snapshot of sample run:

encrypt data in file c++

Now if you open the file codescracker, its data (content) gets encrypted. Here is the snapshot of the opened file, codescracker.txt:

data encrypted c++

As you can see from the above snapshot, the data of the file, codescracker.txt can not be read from any unknown person, that is the person who does not knows the decryption key.

The decryption key is the formula used while encrypting the file, that is, adding 100 to each and every character. So to decrypt, we have to subtract 100 from each and every character. The file, tmp.txt holds cyphertext.

The fstream library allows to work with files. It is defined in fstream header file.

The open() function receives one or two argument. The first argument is required, that is the name of file. The second argument is file opening mode. So the statement given below:

fps.open(fileName, fstream::in);

Opens the file in reading mode only. The name of file is entered by user and stored in fileName variable. The file opening mode, fstream::in opens a file in reading mode.

And the file opening mode, fstream::out opens a file in writing mode. In case, if file doesn't exist, then a new file gets created.

The statement,

fps>>noskipws>>ch

reads data from file in character-by-character manner, without skipping white spaces.

The main logic in above program is:

Decrypt a File in C++

Now let's use cyphertext available in tmp.txt file with decryption formula (subtracting 100 from each character), to decrypt the data of a file, codescracker.txt with the help of following program:

#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
    char fileName[30], ch;
    fstream fps, fpt;
    cout<<"Enter the Name of File: ";
    gets(fileName);
    fps.open(fileName, fstream::out);
    if(!fps)
    {
        cout<<"\nError Occurred while Opening the Source File!";
        return 0;
    }
    fpt.open("tmp.txt", fstream::in);
    if(!fpt)
    {
        cout<<"\nError Occurred while Opening/Creating tmp File!";
        return 0;
    }
    while(fpt>>noskipws>>ch)
    {
        ch = ch-100;
        fps<<ch;
    }
    fps.close();
    fpt.close();
    cout<<"\nFile '"<<fileName<<"' Decrypted Successfully!";
    cout<<endl;
    return 0;
}

Here is the initial snapshot of sample run:

c++ program to decrypt file content

Now enter the name of file say codescracker.txt to decrypt its content as shown in the snapshot given below:

decrypt file c++

After executing the above program, when you opens the same file, that is the file named codescracker.txt. Then your data will be in original form, or your data gets decrypted. Here is the snapshot of the opened file:

file decryption c++

Note - You can create and use your own algorithm for encryption and decryption. It is up to you. The program given above provides you an idea about the topic.

Short Message on Encrypt/Decrypt File

To encrypt a file entered by user, first open the file using the function open(). And read the content of file in character by character manner. At the time of reading, create some algorithm to encrypt the content of the file. While encrypting, place the content (in character-by-character manner) in a temporary file say tmp.txt.

After putting all the encrypted content in a tmp.txt file, copy its content to the original file. And later, use the file, tmp.txt, to decrypt the content of codescracker.txt file.

Same Program in Other Language

C++ Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker