Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
C++ program to encrypt and decrypt text file by giving a pin or number password.
#include<iostream>
#include<conio.h>
#include<string>
#include<string.h>
#include<fstream>
#include<cstdlib>
using namespace std;
class passkey
{
private:
int password, key, k;
public:
passkey();
void getkey();
int checkkey(int );
friend void encrypt(passkey);
friend void decrypt(passkey);
}file1;
passkey::passkey()
{
password = 88;
}
void passkey::getkey()
{
cout << "Enter the key: ";
cin >> key;
}
int passkey::checkkey(int a)
{
if(a==key)
return 1;
else
return 0;
}
char filenaam[40];
int main()
{
int a, pw, x, k;
string c;
cout << "Enter the correct password: ";
cin>>pw;
if(pw!=88)
{
cout << "Sorry you entered the wrong password" << endl;
getch();
return 0;
}
cout << "For ENCRYPTION enter 1\nFor DECRYPTION enter 2\nTo terminate the program enter 3" << endl;
cin>> a;
if(a==1)
{
file1.getkey();
cout << "Give your file a name\neg: \"Input_Data\"" << endl;
cin >> filenaam;
strcat(filenaam,".txt");
cout << "Enter data to the file" << endl;
ofstream InputData(filenaam);
while(cin >> c)
{
InputData << c << " ";
}
InputData.close();
encrypt(file1);
getch();
return 0;
}
else if(a==2)
{
file1.getkey();
cout << "Enter name of your file\neg: Input_Data.txt" << endl;
cin >> filenaam;
strcat(filenaam,".txt");
decrypt(file1);
getch();
return 0;
}
else if (a==3)
{
return 0;
}
}
void encrypt(passkey file)
{
char encfilenaam[50] = "Encrypted_";
strcat(encfilenaam,filenaam);
int n;
srand(file.key);
ifstream OutputData(filenaam);
char ch[50] = "";
ofstream InputData(encfilenaam);
OutputData.getline(ch,49);
do
{
n= 1 + rand()%9;
for(int i=0; i<strlen(ch); i++)
{
ch[i] = ch[i] + n;
}
InputData << ch;
}
while(OutputData.getline(ch,49));
OutputData.close();
InputData.close();
cout << "File is successfully encrypted" << endl;
cout << "Encrypted data is stored in file with name: " << encfilenaam << endl;
}
void decrypt(passkey file)
{
char decfilenaam[50] = "Decrypted_";
strcat(decfilenaam,filenaam);
int m;
srand(file.key);
ifstream OutputData(filenaam);
char ch[50] = "";
ofstream InputData(decfilenaam);
OutputData.getline(ch,49);
do
{
m= 1 + rand()%9;
for(int i=0; i<strlen(ch); i++)
{
ch[i] = ch[i] - m;
}
InputData << ch;
}
while(OutputData.getline(ch,49));
OutputData.close();
InputData.close();
cout << "File successfully decrypted" << endl;
cout << "Decrypted text is stored in " << decfilenaam << endl;
}
@dishvyas

This comment has been minimized.

Copy link

dishvyas commented Apr 23, 2018

In the int main function of your code, when you choose encrypt option, there's a while loop to take input in a file after giving a filename. How to stop taking the input while running the code??

@vatz88

This comment has been minimized.

Copy link
Owner Author

vatz88 commented May 23, 2018

@dishvyas sorry for replying late. I don't remember exactly, but I used to use Windows at that time, when I wrote this code. Now tried in linux and seems pressing ctrl+d should exit the while loop.

Also, I used codeblocks to code this but only now I got to know that not all compilers will run this code. For sure #include<conio.h> will not work and should be removed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.