Encryption is a process that is used to convert a piece of information or simple text to decode it into a new piece of information that can only be understood by the user for whom the data is intended. In C++, an algorithm called a cipher is used to encrypt and decode data. It converts sensitive data into a decoded form to refrain unauthorized readers from fetching this data for misuse.
How to Implement Basic Encryption in C++
There are different algorithms in C++ to execute the encryption of meaningful and sensitive data. The encrypted information is called “cyphertext” and the decrypted or original information called as, “plaintext”. This program performs basic encryption and decryption on the text of the password.
using namespace std;
void encryptArray( char [50 ] );
void decryptArray( char * eStr );
int main( )
{
char string [50];
cout<< "Enter your password here : ";
cin << string;
encryptArray( string );
// function encrypt Array( )
cout << "Encrypted password is : " << string<<endl;
decryptArray( string );
//function decrypt Array( )
cout << "Decrypted password is : " << string<<endl;
cin.get();
return 0;
}// end of main
//-----------------------------------------------------
//encrypt Array data
void encryptArray (char e[] )
{
for( int i=0; e[i] != '\0'; ++i ) ++e[i];
} // encrypt Array
//-----------------------------------------------------
//decrypt Array data
void decryptArray( char * eStr ) {
for( ; * eStr != '\0'; ++ eStr ) --(* eStr);
}
This program is written to encrypt and decrypt the text of the password, which is saved in the form of a string of characters. To apply encryption, the encrypt() function uses a for loop which iterates each character in the array e until it reaches a blank space, which is taken as the terminator of the string. Inside the loop, each ASCII character is iterated to the next character, for example, c becomes d, and so on. In the decrypt() function, the for loop iterates the array until it reaches the blank space and terminates. Inside the loop, each character is decremented by 1 according to its ASCII character, and the result is returned as a decrypted password again.
The user enters the password, how23%, which is encrypted by replacing each character with the next ASCII character, and the encrypted password is displayed. Later, this password is decrypted into its original form.
How to Encrypt a File in C++
A file can also be encrypted in C++. To encrypt a file, create a text file, insert data in it, and save this file in the directory where the files of the C++ compiler are being saved.
For instance, I have saved the username and the password in this text file. The C++ files are saved in the Document folder, so this file is saved in the same directory so that it can be readable to the compiler. This is a program to encrypt this saved file.
#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;
}
The program asks the user to enter the name of the file to encrypt and if the file is not found in the readable directory, it will return an error, otherwise, it will open and read the file. The read file will be encrypted.
The user inputs the code.txt file, which was found in the same directory and encrypted.
The code.txt file is encrypted and saved in the same directory.
Conclusion
Encryption is a process that is used to convert a piece of information or simple text to decode it into a new piece of information that can only be understood by the user for whom the data is intended. A cipher is an algorithm that is used to encrypt and decrypt data in C++ as it converts sensitive data into a decoded form to refrain unauthorized readers from fetching this data for misuse. In C++ the user input data as well as file can be encrypted using encrypt and decrypt functions.