Skip to content

Commit

Permalink
modulo implementation, uppercase compatibility, corrected caesar spel…
Browse files Browse the repository at this point in the history
…ling (;^_^)
  • Loading branch information
calebTree committed Aug 18, 2021
1 parent c4cdc68 commit 51bca63
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 42 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Julius Ceasar's Substitution Cipher!
# Julius Caesar's Substitution Cipher!
Enter an integer (key) to left shift the normal alphabet by that number of positions.
Then, enter a message to encrypt using your new cipher alphabet.
🔥 FYI a key of 13 is effectively ROT-13.
#### web app version
https://calebdanderson.net/ceasar-cipher.html
~~https://calebdanderson.net/ceasar-cipher.html~~
#### more info
https://en.wikipedia.org/wiki/Caesar_cipher
https://en.wikipedia.org/wiki/ROT13
60 changes: 32 additions & 28 deletions ceasar-cipher.cpp → caesar-cipher.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "ceasar-cipher.h"
#include "caesar-cipher.h"
#include <iostream>

// https://www.tutorialspoint.com/cplusplus-program-to-implement-caesar-cypher
// https://www.asciihex.com/

using namespace std;

void encryption(const string &plainAlpha, char &discard) {
Expand All @@ -24,7 +27,7 @@ void encryption(const string &plainAlpha, char &discard) {
// shift alphabet [key] letters to the left
cipherAlphabet = cipherAlpha(key, plainAlpha);
// encrypt the message using the cipher alphabet
cipherText = cipherMessage(cipherAlphabet, plainAlpha, message);
cipherText = cipherMessage(message, key);

cout << "\nPlaintext: " << message << endl;
cout << "Plain alphabet: " << plainAlpha << "." << endl;
Expand Down Expand Up @@ -53,42 +56,43 @@ void decryption(const string &plainAlpha, char &discard) {
cout << "Plain alphabet: " << plainAlpha << "." << endl;
cout << "Cipher alphabet: " << cipherAlpha(key, plainAlpha) << "; Shifted [" << key << "] place(s) to the left." << endl;
// display decrypted message
cout << "Plaintext: " << decrypt(cipherText, plainAlpha, key) << endl << endl;
cout << "Plaintext: " << decrypt(cipherText, key) << endl << endl;
}

string cipherAlpha(int &key, const string &plainAlpha) {
string cipherAlpha = plainAlpha; // cypherAlpha same size as plainAlpha
int p = 0; // wrap around variable
for (int i = 0; i < plainAlpha.size(); i++) {
if (i + key >= plainAlpha.size()) { // wrap around when i + key >= 26
cipherAlpha[i] = plainAlpha[p]; // shift cipherAlpha
p++;
} else {
cipherAlpha[i] = plainAlpha[i + key]; // shift cipherAlpha
}
string cipherAlpha;
for (char c : plainAlpha) {
cipherAlpha += static_cast<char>((c + key - 65) % 26 + 65);
}
return cipherAlpha;
}

string cipherMessage(const string &cipherAlpha, const string &plainAlpha, const string &message) {
string cipherText = message; // cipherText same size as message
for (int i = 0; i < cipherAlpha.size(); i++) { // loop over all letters in the alphabet
for (int j = 0; j < message.size(); j++) { // loop over all letters in the message
if (plainAlpha[i] == message[j])
cipherText[j] = cipherAlpha[i]; // replace each plainAlpha letter in the message with the cipherAlpha letter
}
string cipherMessage(const string &message, int &key) {
string cipherText;
for (char c : message) {
if (c >= 65 && c <= 122)
if (isupper(c)) {
cipherText += static_cast<char>((c + key - 65) % 26 + 65);
} else {
cipherText += static_cast<char>((c + key - 97) % 26 + 97);
}
else
cipherText += c;
}
return cipherText;
}

string decrypt(const string &cipherText, const string &plainAlpha, int &key) {
string plainText = cipherText; // plainText same size as cipherText
string cipherAlphabet = cipherAlpha(key, plainAlpha); // generate cipherAlphabet
for (int i = 0; i < cipherAlphabet.size(); i++) { // loop over all letters in alphabet
for (int j = 0; j < cipherText.size(); j++) { // loop over all letters in the cipherText
if (cipherAlphabet[i] == cipherText[j])
plainText[j] = plainAlpha[i]; // replace each cipherAlpha letter in the message with the plainAlpha letter
}
string decrypt(const string &cipherText, int &key) {
string plainText;
for (char c : cipherText) {
if (c >= 65 && c <= 122)
if (isupper(c)) {
plainText += static_cast<char>((c - key - 65 % 26 + 26) % 26 + 65);
} else {
plainText += static_cast<char>((c - key - 97 % 26 + 26) % 26 + 97);
}
else
plainText += c;
}
return plainText;
}
}
16 changes: 7 additions & 9 deletions ceasar-cipher.h → caesar-cipher.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CEASAR_CIPHER_CEASAR_CIPHER_H
#define CEASAR_CIPHER_CEASAR_CIPHER_H
#ifndef CAESAR_CIPHER_CAESAR_CIPHER_H
#define CAESAR_CIPHER_CAESAR_CIPHER_H

#endif //CEASAR_CIPHER_CEASAR_CIPHER_H
#endif //CAESAR_CIPHER_CAESAR_CIPHER_H

#include <iostream>
using namespace std;
Expand All @@ -20,12 +20,10 @@ string cipherAlpha(int&, const string&);
// Function to generate cipher alphabet.
// Postcondition: const string shifted left by int positions

string cipherMessage(const string&, const string&, const string&);
string cipherMessage(const string&, int&);
// Function to encrypt the message
// Postcondition: string A (plain alphabet), compared to string B (cipher alphabet)
// string C (message) enciphered
// Postcondition: string message shifted left by int positions

string decrypt(const string&, const string&, int&);
string decrypt(const string&, int&);
// Function to decrypt the message
// Postcondition: string A (ciphertext), compared to string B (plain alphabet)
// deciphered (shifted) by int positions
// Postcondition: string ciphertext shifted back to the right by int positions
7 changes: 4 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include <iostream>
#include "ceasar-cipher.h"
#include "caesar-cipher.h"

using namespace std;

int main() {
cout << "\nWelcome to Julius Ceasar's Shift Cipher!"
cout << "\nWelcome to Julius Caesar's Shift Cipher!"
"\nEnter an integer to shift the normal alphabet left by that number of positions." << endl;
cout << "Then, enter a message that will be encrypted using your cipher alphabet." << endl;
cout << "https://en.wikipedia.org/wiki/Caesar_cipher\n" << endl;

int option = -1;
string plainAlpha = "abcdefghijklmnopqrstuvwxyz";
string plainAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char discard;

while (option != 0) {
Expand Down

0 comments on commit 51bca63

Please sign in to comment.