-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcypher.cpp
More file actions
87 lines (77 loc) · 2.2 KB
/
Copy pathcypher.cpp
File metadata and controls
87 lines (77 loc) · 2.2 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
#include <iostream>
#include <string>
using namespace std;
// Function to perform encryption
string encryption(string s, int key)
{
string output = ""; // Reset output string
char a;
for (int i = 0; i < s.length(); i++)
{
a = s[i] + key;
// Ensure that the character remains within the range of lowercase letters or uppercase letters
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
{
if ((a > 'z' && s[i] <= 'z') || (a > 'Z' && s[i] <= 'Z'))
{
a -= 26; // Wrap around to the beginning of the alphabet
}
}
output = output + a;
}
return output;
}
// Function to perform decryption
string decryption(string s, int key)
{
string output = ""; // Reset output string
char a;
for (int i = 0; i < s.length(); i++)
{
a = s[i] - key;
// Ensure that the character remains within the range of lowercase letters or uppercase letters
if ((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z'))
{
if ((a < 'a' && s[i] >= 'a') || (a < 'A' && s[i] >= 'A'))
{
a += 26; // Wrap around to the end of the alphabet
}
}
output = output + a;
}
return output;
}
int main()
{
string input;
int user_choice, key;
char choice;
do
{
cout << "1) Encryption\n2) Decryption\nEnter your choice: ";
cin >> user_choice;
cout << endl;
cout << "Enter text: ";
cin.ignore(); // Ignore the newline character in the buffer
getline(cin, input); // Allowing spaces in input
cout << "Enter key: ";
cin >> key;
cout << endl;
switch (user_choice)
{
case 1:
cout << "Encrypted text: " << encryption(input, key);
break;
case 2:
cout << "Decrypted text: " << decryption(input, key);
break;
default:
cout << "Invalid Input!";
}
cout << endl
<< endl;
cout << "Do you want to encrypt or decrypt again? (Y/N): ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
return 0;
}