-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhack1.cpp
More file actions
96 lines (80 loc) · 1.47 KB
/
hack1.cpp
File metadata and controls
96 lines (80 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 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;
}
}
}