Created
September 2, 2016 18:19
-
-
Save vatz88/750007588adf8c26a98ad428249351ba to your computer and use it in GitHub Desktop.
C++ program to encrypt and decrypt text file by giving a pin or number password.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 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.
Hi dishvyas can you please help me on text reverse and replace space by * and encryp and decrypt the file
hello can you get me how to stop the while loop. I am using it on windows
This is a very good code .
@astroboy619 try ctrl+z
or ctrl+d
. Don't have a windows machine so can't try it
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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??