From 4729ddec7fa128f3806cb5c309652729e8cbcb91 Mon Sep 17 00:00:00 2001 From: calebTree Date: Thu, 2 Sep 2021 07:46:20 -0500 Subject: [PATCH] fixed a bug where non alpha characters were ciphered --- caesar-cipher.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/caesar-cipher.cpp b/caesar-cipher.cpp index ab166ab..c679d80 100644 --- a/caesar-cipher.cpp +++ b/caesar-cipher.cpp @@ -25,7 +25,10 @@ void encryption(const string &plainAlpha) { cout << "\nPlaintext: " << message << endl; cout << "Plain alphabet: " << plainAlpha << "." << endl; - cout << "Cipher alphabet: " << cipherAlphabet << "; Shifted [" << key << "] place(s) to the left." << endl; + if (key == 13) + cout << "Cipher alphabet: " << cipherAlphabet << "; In ROT13." << endl; + else + cout << "Cipher alphabet: " << cipherAlphabet << "; Shifted [" << key << "] place(s) to the left." << endl; cout << "Ciphertext: " << cipherText << endl << endl; } @@ -48,7 +51,10 @@ void decryption(const string &plainAlpha) { cout << "\nCiphertext: " << cipherText << endl; cout << "Plain alphabet: " << plainAlpha << "." << endl; - cout << "Cipher alphabet: " << cipherAlpha(key, plainAlpha) << "; Shifted [" << key << "] place(s) to the left." << endl; + if (key == 13) + cout << "Cipher alphabet: " << cipherAlpha(key, plainAlpha) << "; In ROT13." << endl; + else + cout << "Cipher alphabet: " << cipherAlpha(key, plainAlpha) << "; Shifted [" << key << "] place(s) to the left." << endl; // display decrypted message cout << "Plaintext: " << decrypt(cipherText, key) << endl << endl; } @@ -64,7 +70,7 @@ string cipherAlpha(int &key, const string &plainAlpha) { string cipherMessage(const string &message, int &key) { string cipherText; for (char c : message) { - if (c >= 65 && c <= 122) + if (c >= 65 && c <= 90 || c >= 97 && c <= 122) if (isupper(c)) { cipherText += static_cast((c + key - 65) % 26 + 65); } else { @@ -79,7 +85,7 @@ string cipherMessage(const string &message, int &key) { string decrypt(const string &cipherText, int &key) { string plainText; for (char c : cipherText) { - if (c >= 65 && c <= 122) + if (c >= 65 && c <= 90 || c >= 97 && c <= 122) if (isupper(c)) { plainText += static_cast((c - key - 65 % 26 + 26) % 26 + 65); } else {