-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.cpp
More file actions
143 lines (134 loc) · 3.97 KB
/
Copy pathencryption.cpp
File metadata and controls
143 lines (134 loc) · 3.97 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <string>
#include <limits>
//=======================================================//
using namespace std;
// Function to encrypt a single character using Caesar cipher
char encryptChar(char c, int shift)
{
if (isalpha(c))
{
char base = islower(c) ? 'a' : 'A';
return ((c - base + shift) % 26 + 26) % 26 + base;
}
return c;
}
//=======================================================//
// Function to encrypt a string using Caesar cipher
string encrypt(const string &text, int shift)
{
string result;
for (char c : text)
{
result += encryptChar(c, shift);
}
return result;
}
//=======================================================//
// Function to decrypt a string (reverse of encryption)
string decrypt(const string &text, int shift)
{
return encrypt(text, -shift);
}
//=======================================================//
// Function to encrypt a file
void encryptFile(const string &inputFile, const string &outputFile, int shift)
{
ifstream inFile(inputFile);
ofstream outFile(outputFile);
if (!inFile.is_open())
{
throw runtime_error("Unable to open input file: " + inputFile);
}
if (!outFile.is_open())
{
throw runtime_error("Unable to open output file: " + outputFile);
}
string line;
while (getline(inFile, line))
{
outFile << encrypt(line, shift) << endl;
}
cout << "File encrypted successfully!" << endl;
}
//=======================================================//
// Function to decrypt a file
void decryptFile(const string &inputFile, const string &outputFile, int shift)
{
encryptFile(inputFile, outputFile, -shift); // Decryption is just encryption with negative shift
cout << "File decrypted successfully!" << endl;
}
//=======================================================//
// Function to get a valid integer input from the user
int getValidIntInput(const string &prompt)
{
int value;
while (true)
{
cout << prompt;
if (cin >> value)
{
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid integer." << endl;
}
}
//=======================================================//
// Function to get a valid file name from the user
string getValidFileName(const string &prompt)
{
string fileName;
while (true)
{
cout << prompt;
getline(cin, fileName);
if (!fileName.empty())
{
return fileName;
}
cout << "File name cannot be empty. Please try again." << endl;
}
}
//=======================================================//
int main()
{
try
{
string inputFile = getValidFileName("Enter input file name: ");
string outputFile = getValidFileName("Enter output file name: ");
int shift = getValidIntInput("Enter shift value (1-25): ");
// Ensure shift is within valid range
shift = ((shift % 26) + 26) % 26;
char choice;
while (true)
{
cout << "Encrypt (E) or Decrypt (D)?: ";
cin >> choice;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (toupper(choice) == 'E' || toupper(choice) == 'D')
{
break;
}
cout << "Invalid choice. Please enter 'E' or 'D'." << endl;
}
if (toupper(choice) == 'E')
{
encryptFile(inputFile, outputFile, shift);
}
else
{
decryptFile(inputFile, outputFile, shift);
}
}
catch (const exception &e)
{
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}