Encrypt and decrypt text file using C++ Last Updated : 19 Oct, 2021 Comments Improve Suggest changes Like Article Like Report Encryption in cryptography is a process by which a plain text or a piece of information is converted into ciphertext or a text which can only be decoded by the receiver for whom the information was intended. The algorithm that is used for the process of encryption is known as a cipher. It helps protect consumer information, emails, and other sensitive data from unauthorized access to it and secures communication networks. Presently there are many options to choose from and find the most secure algorithm which meets our requirements. Decryption: Decryption is the process of converting a meaningless message (Ciphertext) into its original form (Plaintext). It works by applying the conversion algorithm opposite of the one that is used to encrypt the data. The same key is required to decrypt the information back to its normal form. Types of Cryptography: There are two types of cryptography: Symmetric Cryptography: It is an encryption system where the sender and receiver of a message use a single common key to encrypt and decrypt messages. Symmetric Key Systems are faster and simpler, but the sender and receiver have to somehow exchange keys securely. The most popular symmetric-key cryptography system is Data Encryption System(DES).Asymmetric Cryptography: Under this system, a pair of keys is used to encrypt and decrypt information. A public key is used for encryption and a private key is used for decryption. The public key and the private key are different. Even if the public key is known by everyone, the intended receiver can only decode it because he alone knows the private key. In this article, symmetric cryptography is used to encrypt and decrypt data. Approach: Let's discuss the approach in detail before proceeding to the implementation part: A class encdec is defined with two member functions: encrypt() and decrypt(). The name of the file to be encrypted is the member variable of the class.encrypt() function is used to handle the encryption of the input file. The file handling code is included in the encrypt() function to read the file and write to the file. A new encrypted file called encrypt.txt is generated with all the encrypted data in it. The encrypted file is encrypted using a key that is being inputted by the user.decrypt() function is used to read the encrypted file and decrypt the data and generate a new file decrypt.txt. To decrypt a file, a key is requested from the user. If the correct key is entered, then the file is successfully decrypted.The input stream fin is used to read from the file and the output stream fout is used to write to the file. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> #include <fstream> using namespace std; // encdec class with encrypt() and // decrypt() member functions class encdec { int key; // File name to be encrypt string file = "geeksforgeeks.txt"; char c; public: void encrypt(); void decrypt(); }; // Definition of encryption function void encdec::encrypt() { // Key to be used for encryption cout << "key: "; cin >> key; // Input stream fstream fin, fout; // Open input file // ios::binary- reading file // character by character fin.open(file, fstream::in); fout.open("encrypt.txt", fstream::out); // Reading original file till // end of file while (fin >> noskipws >> c) { int temp = (c + key); // Write temp as char in // output file fout << (char)temp; } // Closing both files fin.close(); fout.close(); } // Definition of decryption function void encdec::decrypt() { cout << "key: "; cin >> key; fstream fin; fstream fout; fin.open("encrypt.txt", fstream::in); fout.open("decrypt.txt", fstream::out); while (fin >> noskipws >> c) { // Remove the key from the // character int temp = (c - key); fout << (char)temp; } fin.close(); fout.close(); } // Driver Code int main() { encdec enc; char c; cout << "\n"; cout << "Enter Your Choice : -> \n"; cout << "1. encrypt \n"; cout << "2. decrypt \n"; cin >> c; cin.ignore(); switch (c) { case '1': { enc.encrypt(); break; } case '2': { enc.decrypt(); break; } } } Output: Comment More infoAdvertise with us Next Article Encrypt and decrypt text file using C++ baljeet_singh Follow Improve Article Tags : Algorithms Project C++ Programs Programming Language C++ DSA CPP-Basics +3 More Practice Tags : CPPAlgorithms Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to 5 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Like