-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCaesar.cpp
More file actions
139 lines (114 loc) · 2.79 KB
/
Caesar.cpp
File metadata and controls
139 lines (114 loc) · 2.79 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
#include <iostream>
#include <string>
#include <cctype>
#include <limits>
#include "Caesar.hpp"
#include "Cipher.hpp"
using namespace std;
int Alpha(char alpha) {return alpha - 'a';} //changing Ascii value to 0~26
char Ascii(char alpha) {return alpha + 'a';} //changing 0~26 to Ascii Value
Caesar::Caesar()
{
//prompt the user to choose between Encrypting and Decrypting
//also calls the function to Encrypt or Decrypt
Select_Encrypt();
} // end of "Caeser"
void Caesar::Select_Encrypt()
{
//User choosing Encrypt or Decrypt
int userNum;
system("clear");
cout << "0. Encrypt Caeser Cipher" << endl << endl
<< "1. Decrypt Caeser Cipher" << endl << endl
<< "2. Go back to menu" << endl;
do{
cout << "Enter a number: ";
cin.clear();
cin.ignore(std::numeric_limits<int>::max(), '\n');
cin >> userNum;
} while ((userNum != 0) and (userNum != 1) and (userNum != 2)); //choosing between 0, 1, 2
//Encrypt with Caesar Cipher
if(userNum == 0)
{
Encrypt_Caesar();
}
//Decrypt with Caesar Cipher
else if(userNum == 1)
{
Decrypt_Caesar();
}
//Go back to menu
else
{
Cipher c1;
c1.Start();
}
} //end of "Select_Encrypt"
void Caesar::Encrypt_Caesar()
{
system("clear");
//prompt user to input the message
cout << "Caesar Cipher\nEnter a message you want to Encrypt (English): ";
cin.ignore();
getline(cin, original);
//empty result in case there are garnage value inside
result.clear();
for(int i = 0; i < static_cast<int>(original.length()); ++i)
{
//we don't encrypt spacebar
if(original[i] == ' ')
{
result += ' ';
}
else
{
//C_i = P_i +3 (converted to only lower case)
result += Ascii(Alpha(tolower(original[i]) + 3) % 26);
}
}
//display Encrypted message
cout << "\n\nResult for Encrypting " << original << " with Caeser Cipher:\n\n "
<< result << endl;
if(again())
{
Encrypt_Caesar();
}
} // end of "Encrypt_Caesar"
void Caesar::Decrypt_Caesar()
{
system("clear");
//prompt user to input the Encrypted message
cout << "Caesar Cipher\nEnter a message you want to Decrypt(English): ";
cin.ignore();
getline(cin, original);
//empty result in case there are garnage value inside
result.clear();
for(int i = 0; i < static_cast<int>(original.length()); ++i)
{
//we don't decrypt spacebar
if(original[i] == ' ')
{
result += ' ';
}
else
{
//C_i = P_i +3
result += Ascii((Alpha(original[i] - 3) + 26) % 26);
}
}
//display decrypted message
cout << "\n\nResult for Decrypting " << original << " with Caeser Cipher: \n\n"
<< result << endl << endl;
if(again())
{
Decrypt_Caesar();
}
}
bool Caesar::again()
{
//asking the user if they want to do it again
char response;
cout << "\nDo you want to do it again?(y/n): ";
cin >> response;
return (tolower(response) == 'y');
} //end of "again"