C Program to Encrypt and Decrypt Files

In this article, you will learn and get code about how to encrypt or decrypt a file using the C programming language. But before going through the program of encryption and decryption, let's first understand what it actually means when encrypting or decrypting a file.

What exactly are encryption and decryption?

Data encryption means converting the original data into a form or code that cannot be read or understood by anyone (the public). Because encrypted data can only be accessed by authorized people, Here "authorized person" means a man who knows its decryption key (or formula or password). A decryption key is a password or formula that is used to convert the ciphertext to plaintext or the original text.

Note: Encrypted data is known as "cyphertext," whereas unencrypted data is known as "plaintext."

Encrypt or Decrypt a File in C

Encrypting a file means, we will convert the plaintext (original content of file) to cyphertext, so that our credential information stored in a file gets converted into ciphertext, whereas decrypting a file means getting our content back to its original form.

Things to Do Before the Program

Here are the things that must have to do, before going through the program given below. Because, we have created a program to encrypt a file and then created another program to decrypt the same file. Then we must have to create a file in the same directory where program's source code is going to be saved.

For example, create a folder named "c programs" to the parent directory named "Documents". And create a file named "fresherearth.txt" with following content:

my usernamed is fresherearth
my password is 1234@cc

Here is a snapshot of the "c programs" folder present in the parent directory of "Documents":

file encryption program in c

As you can see from the above snapshot, there is a file named fresherearth.txt in the folder "c programs." Now let's move on to the program to encrypt the content of this file using the C program given given below.

Program to Encrypt a File in C

To encrypt a file in C programming, you have to open that file and start reading the file character by character. At the time of reading, create some algorithm to encrypt the content of the file, and place the content in a temporary file, character by character. Finally, copy the content of the temporary file to the original file as shown in the program given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char fname[20], ch;
    FILE *fps, *fpt;
    printf("Enter Filename: ");
    gets(fname);
    fps = fopen(fname, "r");
    if(fps == NULL)
        return 0;
    fpt = fopen("temp.txt", "w");
    if(fpt == NULL)
        return 0;
    ch = fgetc(fps);
    while(ch != EOF)
    {
        ch = ch+100;
        fputc(ch, fpt);
        ch = fgetc(fps);
    }
    fclose(fps);
    fclose(fpt);
    fps = fopen(fname, "w");
    if(fps == NULL)
        return 0;
    fpt = fopen("temp.txt", "r");
    if(fpt == NULL)
        return 0;
    ch = fgetc(fpt);
    while(ch != EOF)
    {
        ch = fputc(ch, fps);
        ch = fgetc(fpt);
    }
    fclose(fps);
    fclose(fpt);
    printf("\nFile %s Encrypted Successfully!", fname);
    getch();
    return 0;
}

This program was written in the Code::Blocks IDE. Before running the preceding program, save it by going to the File->Save file as... menu (in Code::Blocks).Save the file in the same directory where fresherearth.txt was saved earlier in this article, namely the "c programs" folder.

Now build and run the above program after saving it. Because we have used "w" as the file opening mode (for the file temp.txt), that is used to write. And if the file does not exist, then a new one gets created with the same name, which in this case is temp.txt. Now here is the sample run of the above program:

c program to encrypt files

Enter the filename fresherearth.txt and press the ENTER key to encrypt it. Here is the second snapshot of the same sample run:

c program encrypt text file

Now your file, fresherearth.txt, gets encrypted. Here is a screenshot of the "c programs" folder. You will see that a new file, text.txt, automatically gets created inside the same folder:

encrypt text file in c

The content of the file, fresherearth.txt, before encryption is:

c program encrypt file

And the following is the content of the same file, fresherearth.txt, after encryption:

c file encryption program

Algorithm used to encrypt files in previous programs

This is the simple encryption key that was used in the above program to encrypt the content of the file fresherearth.txt:

ch = ch+100;

It means:

Now let's go through the program that decrypts the same file that was encrypted using the above program.

Program to Decrypt a File in C

The question is: write a program in C that decrypts the file encrypted using the previous program. The answer to this question is given below:

#include<stdio.h>
#include<conio.h>
int main()
{
    char fname[20], ch;
    FILE *fps, *fpt;
    printf("Enter Filename: ");
    gets(fname);
    fps = fopen(fname, "w");
    if(fps == NULL)
        return 0;
    fpt = fopen("temp.txt", "r");
    if(fpt == NULL)
        return 0;
    ch = fgetc(fpt);
    while(ch != EOF)
    {
        ch = ch-100;
        fputc(ch, fps);
        ch = fgetc(fpt);
    }
    fclose(fps);
    fclose(fpt);
    printf("\nFile %s Decrypted Successfully!", fname);
    getch();
    return 0;
}

Here is the sample run of the previous program (just above this sentence):

c program to decrypt files

If you look at the file fresherearth.txt, you will see its original content, which was:

my usernamed is fresherearth
my password is 1234@cc

That is, if you see your file's content, then you will find that your file's content will be decrypted, i.e., you will get back your original content.

Note: You can use a lot of algorithms and techniques (including your own) to encrypt and decrypt your file's content.

The same program in different languages

C Quiz


« Previous Program Next Program »