Skip to content

Commit

Permalink
data hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
calebTree committed May 6, 2021
1 parent 747a69d commit 203ecda
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 95 deletions.
96 changes: 96 additions & 0 deletions ceasar-cipher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Created by caleb on 5/5/2021.
//
#include "ceasar-cipher.h"
#include <iostream>
using namespace std;

void encryption(const string &plainAlpha, char &discard) {
int key;
string message, cipherAlphabet, cipherText;

// input number of places to shift left
cout << "Enter your chosen cipher key. (integer, 1-25): ";
cin >> key;
// input validation
if (key > 25 || key < 1) {
while (key > 25 || key < 1) {
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
}
cin.get(discard); // discard \n
cout << "Enter the message to encrypt. (lowercase, roman alpha): "; // input message to encrypt
getline(cin, message);

// shift alphabet [key] letters to the left
cipherAlphabet = cipherAlpha(key, plainAlpha);
// encrypt the message using the cipher alphabet
cipherText = cipherMessage(cipherAlphabet, plainAlpha, message);

cout << "\nPlaintext: " << message << endl;
cout << "Plain alphabet: " << plainAlpha << "." << endl;
cout << "Cipher alphabet: " << cipherAlphabet << "; Shifted [" << key << "] place(s) to the left." << endl;
cout << "Ciphertext: " << cipherText << endl << endl;
}

void decryption(const string &plainAlpha, char &discard) {
int key;
string cipherText;

cout << "Enter the known cipher key. (integer, 1-25): ";
cin >> key;
// input validation
if (key > 25 || key < 1) {
while (key > 25 || key < 1) {
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
}
cin.get(discard); // discard \n
cout << "Enter the message to decrypt. (lowercase, roman alpha): "; // input message to decrypt
getline(cin, cipherText);

cout << "\nCiphertext: " << cipherText << endl;
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;
}

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
}
}
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
}
}
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
}
}
return plainText;
}
17 changes: 17 additions & 0 deletions ceasar-cipher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by caleb on 5/5/2021.
//

#ifndef CEASAR_CIPHER_CEASAR_CIPHER_H
#define CEASAR_CIPHER_CEASAR_CIPHER_H

#endif //CEASAR_CIPHER_CEASAR_CIPHER_H

#include <iostream>
using namespace std;

void encryption(const string&, char&); // encryption option
void decryption(const string&, char&); // decryption option
string cipherAlpha(int&, const string&); // generate cipher alphabet
string cipherMessage(const string&, const string&, const string&); // encrypt the message
string decrypt(const string&, const string&, int&); // decrypt the message
96 changes: 1 addition & 95 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
#include <iostream>
#include "ceasar-cipher.h"
using namespace std;

void encryption(const string&, char&); // encryption option
void decryption(const string&, char&); // decryption option
string cipherAlpha(int&, const string&); // generate cipher alphabet
string cipherMessage(const string&, const string&, const string&); // encrypt the message
string decrypt(const string&, const string&, int&); // decrypt the message

int main() {
cout << "\nWelcome to Julius Ceasar's Shift Cipher!"
"\nEnter an integer to shift the normal alphabet left by that number of positions." << endl;
Expand Down Expand Up @@ -35,92 +30,3 @@ int main() {
}
return 0;
}
void encryption(const string &plainAlpha, char &discard) {
int key;
string message, cipherAlphabet, cipherText;

// input number of places to shift left
cout << "Enter your chosen cipher key. (integer, 1-25): ";
cin >> key;
// input validation
if (key > 25 || key < 1) {
while (key > 25 || key < 1) {
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
}
cin.get(discard); // discard \n
cout << "Enter the message to encrypt. (lowercase, roman alpha): "; // input message to encrypt
getline(cin, message);

// shift alphabet [key] letters to the left
cipherAlphabet = cipherAlpha(key, plainAlpha);
// encrypt the message using the cipher alphabet
cipherText = cipherMessage(cipherAlphabet, plainAlpha, message);

cout << "\nPlaintext: " << message << endl;
cout << "Plain alphabet: " << plainAlpha << "." << endl;
cout << "Cipher alphabet: " << cipherAlphabet << "; Shifted [" << key << "] place(s) to the left." << endl;
cout << "Ciphertext: " << cipherText << endl << endl;
}

void decryption(const string &plainAlpha, char &discard) {
int key;
string cipherText;

cout << "Enter the known cipher key. (integer, 1-25): ";
cin >> key;
// input validation
if (key > 25 || key < 1) {
while (key > 25 || key < 1) {
cout << "Error! Enter an integer between 1 and 25: ";
cin >> key;
}
}
cin.get(discard); // discard \n
cout << "Enter the message to decrypt. (lowercase, roman alpha): "; // input message to decrypt
getline(cin, cipherText);

cout << "\nCiphertext: " << cipherText << endl;
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;
}

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
}
}
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
}
}
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
}
}
return plainText;
}

0 comments on commit 203ecda

Please sign in to comment.