Skip to content

Commit 0ae349e

Browse files
committed
Add uppercase encryption/decryption support
Also fix a small bug which says something is unrecognized option if input was valid.
1 parent 0fd78d4 commit 0ae349e

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

src/decrypt.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using namespace std;
1010
int decrypt() {
1111
string alphabet = "abcdefghijklmnopqrstuvwxyz";
12+
string ualphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1213
string newMessage, message, type;
1314
int key;
1415
cout << "Do you want to decrypt a text file or a message" << endl;
@@ -37,7 +38,8 @@ int decrypt() {
3738
FileToDecryptContents += v;
3839
for (int i = 0; i < FileToDecryptContents.size(); i++) {
3940
size_t letterPos = alphabet.find(FileToDecryptContents[i]);
40-
if (letterPos == string::npos) {
41+
size_t letterPosU = ualphabet.find(FileToDecryptContents[i]);
42+
if (letterPos == string::npos && letterPosU == string::npos) {
4143
newMessage += FileToDecryptContents[i];
4244
}
4345
if (letterPos != string::npos) {
@@ -49,6 +51,15 @@ int decrypt() {
4951
int newCharacter = alphabet[newPosition];
5052
newMessage += newCharacter;
5153
}
54+
if (letterPosU != string::npos) {
55+
int newPosition;
56+
newPosition = letterPosU - key;
57+
if (newPosition < 0) {
58+
newPosition = newPosition + ualphabet.size();
59+
}
60+
int newCharacter = ualphabet[newPosition];
61+
newMessage += newCharacter;
62+
}
5263
}
5364
}
5465
fstream DecryptedFile;
@@ -57,13 +68,15 @@ int decrypt() {
5768
cout << "Could not create the decrypted file. Here is the decrypted text file '" + newMessage + "'. Press ENTER to exit";
5869
cin.ignore();
5970
cin.get();
71+
return 0;
6072
}
6173
else {
6274
DecryptedFile << newMessage;
6375
DecryptedFile.close();
6476
cout << "Your Decrypted File is saved to " + fileLocation + "-Decrypted. Press ENTER to exit";
6577
cin.ignore();
6678
cin.get();
79+
return 0;
6780
}
6881
}
6982
if (type == "message" || type == "Message") {
@@ -73,7 +86,8 @@ int decrypt() {
7386
cin >> key;
7487
for (int i = 0; i < message.size(); i++) {
7588
size_t letterPos = alphabet.find(message[i]);
76-
if (letterPos == string::npos) {
89+
size_t letterPosU = ualphabet.find(message[i]);
90+
if (letterPos == string::npos && letterPosU == string::npos) {
7791
newMessage += message[i];
7892
}
7993
if (letterPos != string::npos) {
@@ -85,8 +99,20 @@ int decrypt() {
8599
int newCharacter = alphabet[newPosition];
86100
newMessage += newCharacter;
87101
}
102+
if (letterPosU != string::npos) {
103+
int newPosition;
104+
newPosition = letterPosU - key;
105+
if (newPosition < 0) {
106+
newPosition = ualphabet.size() + newPosition;
107+
}
108+
int newCharacter = ualphabet[newPosition];
109+
newMessage += newCharacter;
110+
}
88111
}
89112
cout << "Your decrypted message is '" + newMessage + "'. Press ENTER to exit" << endl;
113+
cin.ignore();
114+
cin.get();
115+
return 0;
90116
}
91117
else {
92118
cout << "Unrecognized Option: '" + type + "'. ";

src/encrypt.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using namespace std;
1010
int encrypt() {
1111
string alphabet = "abcdefghijklmnopqrstuvwxyz";
12+
string ualphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1213
string newMessage, message, type;
1314
int key;
1415
cout << "Do you want to encrypt a text file or a message" << endl;
@@ -37,7 +38,8 @@ int encrypt() {
3738
FileToEncryptContents += v;
3839
for (int i = 0; i < FileToEncryptContents.size(); i++) {
3940
size_t letterPos = alphabet.find(FileToEncryptContents[i]);
40-
if (letterPos == string::npos) {
41+
size_t letterPosU = ualphabet.find(FileToEncryptContents[i]);
42+
if (letterPos == string::npos && letterPosU == string::npos) {
4143
newMessage += FileToEncryptContents[i];
4244
}
4345
if (letterPos != string::npos) {
@@ -49,6 +51,15 @@ int encrypt() {
4951
int newCharacter = alphabet[newPosition];
5052
newMessage += newCharacter;
5153
}
54+
if (letterPosU != string::npos) {
55+
int newPosition;
56+
newPosition = letterPosU + key;
57+
if (newPosition > ualphabet.size()) {
58+
newPosition = newPosition - ualphabet.size();
59+
}
60+
int newCharacter = ualphabet[newPosition];
61+
newMessage += newCharacter;
62+
}
5263
}
5364
}
5465
fstream EncryptedFile;
@@ -57,13 +68,15 @@ int encrypt() {
5768
cout << "Could not create the encrypted file. Here is the encrypted text file '" + newMessage + "'. Press ENTER to exit";
5869
cin.ignore();
5970
cin.get();
71+
return 0;
6072
}
6173
else {
6274
EncryptedFile << newMessage;
6375
EncryptedFile.close();
6476
cout << "Your Encrypted File is saved to " + fileLocation + "-Encrypted. Press ENTER to exit";
6577
cin.ignore();
6678
cin.get();
79+
return 0;
6780
}
6881
}
6982
if (type == "message" || type == "Message") {
@@ -73,7 +86,8 @@ int encrypt() {
7386
cin >> key;
7487
for (int i = 0; i < message.size(); i++) {
7588
size_t letterPos = alphabet.find(message[i]);
76-
if (letterPos == string::npos) {
89+
size_t letterPosU = ualphabet.find(message[i]);
90+
if (letterPos == string::npos && letterPosU == string::npos) {
7791
newMessage += message[i];
7892
}
7993
if (letterPos != string::npos) {
@@ -85,8 +99,20 @@ int encrypt() {
8599
int newCharacter = alphabet[newPosition];
86100
newMessage += newCharacter;
87101
}
102+
if (letterPosU != string::npos) {
103+
int newPosition;
104+
newPosition = letterPosU + key;
105+
if (newPosition > ualphabet.size()) {
106+
newPosition = newPosition - ualphabet.size();
107+
}
108+
int newCharacter = ualphabet[newPosition];
109+
newMessage += newCharacter;
110+
}
88111
}
89112
cout << "Your encrypted message is '" + newMessage + "'. Press ENTER to exit" << endl;
113+
cin.ignore();
114+
cin.get();
115+
return 0;
90116
}
91117
else {
92118
cout << "Unrecognized Option: '" + type + "'. ";

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ int main(int argc, char** argv) {
1616
cin >> crypt;
1717
if (crypt == "encrypt") {
1818
encrypt();
19+
return 0;
1920
}
2021
if (crypt == "decrypt") {
2122
decrypt();
23+
return 0;
2224
}
2325
else {
2426
cout << "Unrecognized Option: '" + crypt + "'. ";
2527
main(NULL, NULL);
2628
}
27-
cin.ignore();
28-
cin.get();
2929
}
3030

3131
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu

0 commit comments

Comments
 (0)