Added the Caesar Cipher to the program

This commit is contained in:
2018-04-25 18:58:05 -04:00
parent 422d38ed6f
commit 3503a42e77
5 changed files with 380 additions and 0 deletions

35
Headers/Caesar.hpp Normal file
View File

@@ -0,0 +1,35 @@
//Ciphers/Headers/Caesar.hpp
//Matthew Ellison
// Created: 4-25-18
//Modified: 4-25-18
//This file contains the declaration of the Caesar class
//This class implements the Caesar Cipher and is inteded to be turned into a library
#ifndef CAESAR_HPP
#define CAESAR_HPP
#include <string>
class Caesar{
private:
std::string input;
std::string output;
int shift;
public:
Caesar();
~Caesar();
void setInput(std::string inputString);
std::string getInput() const;
void setShift(int shiftAmount);
int getShift() const;
std::string getOutput() const;
std::string encode();
std::string encode(int shiftAmount, std::string inputString);
std::string decode();
std::string decode(int shiftAmount, std::string inputString);
void reset();
};
#endif //CAESAR_HPP

127
SourceFiles/Caesar.cpp Normal file
View File

@@ -0,0 +1,127 @@
//Ciphers/SourceFiles/Caesar.hpp
//Matthew Ellison
// Created: 4-25-18
//Modified: 4-25-18
//This file contains the implementation of the Caesar class
//This class implements the Caesar Cipher and is inteded to be turned into a library
#include "../Headers/Caesar.hpp"
#include <string>
#include <cctype>
//std::string input;
//std::string output;
//int shift;
Caesar::Caesar(){
shift = 0;
}
Caesar::~Caesar(){
}
void Caesar::setInput(std::string inputString){
input = inputString;
}
std::string Caesar::getInput() const{
return input;
}
void Caesar::setShift(int shiftAmount){
//If you shift more than 26 you will just be wrapping back around again
shift = shiftAmount % 26;
}
int Caesar::getShift() const{
return shift;
}
std::string Caesar::getOutput() const{
return output;
}
std::string Caesar::encode(){
output = "";
char temp; //A temperary holder for the current working character
for(int cnt = 0;cnt < input.size();++cnt){
temp = input.at(cnt);
//If it is a upper case letter shift it and wrap if necessary
if(isupper(temp)){
temp += shift;
//Wrap around if the letter is now out of bounds
if(temp < 'A'){
temp += 26;
}
else if(temp > 'Z'){
temp -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(islower(temp)){
temp += shift;
//Wrap around if the letter is now out of bounds
if(temp < 'a'){
temp += 26;
}
else if(temp > 'z'){
temp -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
output += temp;
}
return output;
}
std::string Caesar::encode(int shiftAmount, std::string inputString){
setShift(shiftAmount);
setInput(inputString);
return encode();
}
std::string Caesar::decode(){
output = "";
char temp;
for(int cnt = 0;cnt < input.size();++cnt){
temp = input.at(cnt);
//If it is an upper case letter shift it and wrap if necessary
if(isupper(temp)){
temp -= shift;
if(temp < 'A'){
temp += 26;
}
else if(temp > 'Z'){
temp -= 26;
}
}
//If it is a lower case letter shift it and wrap if necessary
else if(islower(temp)){
temp -= shift;
if(temp < 'a'){
temp += 26;
}
else if(temp > 'z'){
temp -= 26;
}
}
//If it is whitespace, number, or punctuation just let it pass through
//Add it to the output string
output += temp;
}
return output;
}
std::string Caesar::decode(int shiftAmount, std::string inputString){
setShift(shiftAmount);
setInput(inputString);
return decode();
}
void Caesar::reset(){
input = output = "";
shift = 0;
}

BIN
libCaesar.lib Normal file

Binary file not shown.

194
main.cpp Normal file
View File

@@ -0,0 +1,194 @@
//Ciphers/main.cpp
//Matthew Ellison
// Created: 4-25-18
//Modified: 4-25-18
//This file contains the driver function and the test functions for the Cipher program
//This program will use some simple ciphers that are no longer widely used but still fun to play with
///The header files
#include "Headers/Caesar.hpp"
#include "Headers/Playfair.hpp"
#include <iostream>
#include <string>
///Test definitions
#define TEST_VERSION
#ifdef TEST_VERSION
#define CAESAR_TEST
//#define PLAYFAIR_TEST
enum codingState { ENCODE, DECODE };
std::string testingError(codingState type, const std::string& input, const std::string& output, const std::string& cipher);
#endif //TEST_VERSION definition
#ifdef CAESAR_TEST
bool caesarTest(std::string& errorString);
#endif //CAESAR_TEST
#ifdef PLAYFAIR_TEST
bool playfairTest(std::string& errorString);
#endif //PLAYFAIR_TEST
//Main functions
#ifdef TEST_VERSION
int main(int argc, char** argv){
bool testResult = false;
std::string resultString = "";
std::string errorString = "";
//Testing the Caesar Cipher
#ifdef CAESAR_TEST
testResult = caesarTest(errorString);
//If the test was successful
if(testResult){
resultString += "Caesar Cipher completed successfully\n";
}
else{
resultString += "Caesar Cipher error:\n" + errorString;
}
//Reset the variables
errorString = "";
testResult = false;
#endif //CAESAR_TEST
//Testing the playfair cipher
#ifdef PLAYFAIR_TEST
testResult = playfairTest(errorString);
if(testResult){
resultString += "Playfair Cipher completed successfully\n";
}
else{
resultString += "Playfair Cipher error: " + errorString;
}
errorString = "";
testResult = false;
#endif //PLAYFAIR_TEST
std::cout << "Results:\n" << resultString << std::endl;
std::cin.get();
return 0;
}
std::string testingError(codingState type, const std::string& input, const std::string& output, const std::string& cipher){
std::string errorMessage;
//Decide if it was encoding or decoding
if(type == ENCODE){
errorMessage = "Encoding:";
}
else if(type == DECODE){
errorMessage = "Decoding:";
}
else{
errorMessage = "We gots problems";
}
errorMessage += "\nError in: " + input + "\nExpected: " + output + "\nActual: " + cipher + '\n';
return errorMessage;
}
#ifdef CAESAR_TEST
bool caesarTest(std::string& errorString){
///Add something in error message about shift amount
Caesar cipher;
bool passed = true;
std::string inputString = ""; //Holds the string to be input into the cipher
std::string outputString = ""; //Holds the string that the cipher is expected to output
std::string cipherString = ""; //Holds the string that the cipher actually output
///All lowercase
inputString = "abcdefghijklmnopqrstuvwxyz"; //Shift = 3
outputString = "defghijklmnopqrstuvwxyzabc"; //Shift = 3
//Try it forwards
cipherString = cipher.encode(3, inputString);
if(cipherString != outputString){
//errorString += "Encoding:\nError in: " + inputString + "\nExpected: " + outputString + "\nActual: " + cipherString + '\n';
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(3, outputString);
if(cipherString != inputString){
//errorString += "Decoding:\nError in: " + outputString + "\nExpected: " + inputString + "\nActual: " + cipherString + '\n';
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
///All upercase
inputString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
outputString = "XYZABCDEFGHIJKLMNOPQRSTUVW";
//Try it forwards
cipherString = cipher.encode(-3, inputString);
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(-3, outputString);
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
///Both cases
inputString = "AbCdEfGhIjKlMnOpQrStUvWxYz";
outputString = "DeFgHiJkLmNoPqRsTuVwXyZaBc";
//Try it forwards
cipherString = cipher.encode(3, inputString);
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(3, outputString);
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
///Punctuation
inputString = "abc,def. rst; wxyz";
outputString = "def,ghi. uvw; zabc";
//Try it forwards
cipherString = cipher.encode(3, inputString);
if(cipherString != outputString){
errorString += testingError(ENCODE, inputString, outputString, cipherString);
passed = false;
}
//Try it backwards
cipherString = cipher.decode(3, outputString);
if(cipherString != inputString){
errorString += testingError(DECODE, outputString, inputString, cipherString);
passed = false;
}
return passed;
}
#endif //CAESAR_TEST
#ifdef PLAYFAIR_TEST
bool playfairTest(std::string& errorString){
}
#endif //PLAYFAIR_TEST
#else //TEST_VERSION main function
enum CipherFlagLocation { CAESAR, PLAYFAIR };
void getFlags(bool flags[]);
int main(int argc, char** argv){
}
void getFlags(bool flags[]){
}
#endif //TEST_VERSION

24
makefile Normal file
View File

@@ -0,0 +1,24 @@
#For Linux
All: libCaesar.a libPlayfair.a Ciphers
#For Windows
WindowsAll: libCaesar.lib libPlayfair.lib Ciphers.exe
libCaesar.a: SourceFiles/Caesar.cpp
(CXX) -shared -std=c++11 -O3 -fPIC -o $@ $<
libPlayfair.a: SourceFiles/Playfair.cpp
(CXX) -shared -std=c++11 -O3 -fPIC -o $@ $<
Ciphers: main.cpp
(CXX) -O3 -std=c++11 -o $@ $< -lCaesar -lPlayfair
libCaesar.lib: SourceFiles/Caesar.cpp
g++ -shared -std=c++11 -O3 -fPIC -o $@ $<
libPlayfair.lib: SourceFiles/Caesar.cpp
g++ -shared -std=c++11 -O3 -fPIC -o $@ $<
Ciphers.exe: main.cpp
g++ -std=c++11 -O3 -o $@ $< -L./ -llibCaesar -llibPlayfair