- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- C++ Find Simple Interest
- C++ Fahrenheit to Celsius
- C++ Celsius to Fahrenheit
- C++ Print Prime Numbers
- C++ Reverse a Number
- C++ Swap Two Numbers
- C++ Print Multiplication Table
- C++ Find Factorial of Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- C++ Sum of Squares of Digits
- C++ Interchange Digits of Number
- C++ if else Programs
- C++ Check Even or Odd
- C++ Check Prime or Not
- C++ Check Alphabet or Not
- C++ Check Vowel or Not
- C++ Check Leap Year or Not
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- C++ Find Discounted Price
- C++ Find Shipping Charge
- C++ Find Telephone Bills
- C++ Calculate Student Grade
- C++ Largest of Two Numbers
- C++ Largest of Three Numbers
- C++ Number Conversion
- C++ Decimal to Binary
- C++ Decimal to Octal
- C++ Decimal to Hexadecimal
- C++ Binary to Decimal
- C++ Binary to Octal
- C++ Binary to Hexadecimal
- C++ Octal to Decimal
- C++ Octal to Binary
- C++ Octal to Hexadecimal
- C++ Hexadecimal to Decimal
- C++ Hexadecimal to Binary
- C++ Hexadecimal to Octal
- C++ Pattern Programs
- C++ Pattern Programs
- C++ Print Diamond Pattern
- C++ Print Floyd's Triangle
- C++ Print Pascal's Triangle
- C++ Array Programs
- C++ 1D Array Program
- C++ Linear Search
- C++ Binary Search
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- C++ Merge two Arrays
- C++ Bubble Sort
- C++ Selection Sort
- C++ Insertion Sort
- C++ Common Elements
- C++ 2D Array Programs
- C++ Add Two Matrices
- C++ Subtract Two Matrices
- C++ Transpose Matrix
- C++ Multiply Two Matrices
- C++ 3D Array Programs
- C++ String Programs
- C++ Print String
- C++ Find Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Program to Encrypt and Decrypt a File
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:
And here is the snapshot of the opened file, codescracker.txt:
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:
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:
Now if you open the file codescracker, its data (content) gets encrypted. Here is the snapshot of the opened file, codescracker.txt:
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:
- Asks from user enter the name of file say codescracker.txt
- Opens the file (in reading mode) entered by user
- Creates a file named tmp.txt
- Reads content of file, codescracker.txt in character-by-character manner
- While reading the character, adds 100 and writes to tmp.txt file
- That is, in tmp.txt file, we have the same content as of codescracker.txt. Instead each character is of ASCII value, 100 more than original one
- Closes both the file stream
- Opens the file, codescracker.txt in writing mode
- Opens the file, tmp.txt in reading mode
- Copies the content of tmp.txt (encrypted content of codescracker.txt file) to codescracker.txt file
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:
Now enter the name of file say codescracker.txt to decrypt its content as shown in the snapshot given below:
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:
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
« Previous Program Next Program »