mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-06 18:33:58 -05:00
Created class for Atbash cipher
This commit is contained in:
31
Headers/Atbash.hpp
Normal file
31
Headers/Atbash.hpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
//Ciphers/Headers/Atbash.hpp
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 4-30-18
|
||||||
|
//Modified: 4-30-18
|
||||||
|
//This file contains the declaration of the Atbash class
|
||||||
|
//This class is used to encode and decode an Atbash cipher
|
||||||
|
|
||||||
|
#ifndef ATBASH_HPP
|
||||||
|
#define ATBASH_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
class Atbash{
|
||||||
|
private:
|
||||||
|
std::string inputString;
|
||||||
|
std::string outputString;
|
||||||
|
public:
|
||||||
|
Atbash();
|
||||||
|
~Atbash();
|
||||||
|
void setInputString(std::string input);
|
||||||
|
std::string getInputString() const;
|
||||||
|
std::string getOutputString() const;
|
||||||
|
std::string encode();
|
||||||
|
std::string encode(std::string input);
|
||||||
|
std::string decode();
|
||||||
|
std::string decode(std::string input);
|
||||||
|
void reset();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif //ATBASH_HPP
|
||||||
74
SourceFiles/Atbash.cpp
Normal file
74
SourceFiles/Atbash.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
//Ciphers/SourceFiles/Atbash.cpp
|
||||||
|
//Matthew Ellison
|
||||||
|
// Created: 4-30-18
|
||||||
|
//Modified: 4-30-18
|
||||||
|
//This file contains the implementation of the Atbash class
|
||||||
|
|
||||||
|
|
||||||
|
#include "../Headers/Atbash.hpp"
|
||||||
|
#include <cctype>
|
||||||
|
|
||||||
|
Atbash::Atbash(){
|
||||||
|
}
|
||||||
|
|
||||||
|
Atbash::~Atbash(){
|
||||||
|
}
|
||||||
|
|
||||||
|
void Atbash::setInputString(std::string input){
|
||||||
|
//Make sure inputString is empty
|
||||||
|
reset();
|
||||||
|
//Strip all punctuation and whitespace from input and make all letters capital
|
||||||
|
for(int cnt = 0;cnt < input.size();++cnt){
|
||||||
|
char letter = input[cnt];
|
||||||
|
//If it is upper case add it to the inputString
|
||||||
|
if(isupper(letter)){
|
||||||
|
inputString += letter;
|
||||||
|
}
|
||||||
|
//If it is lower case make it upper case and add it to the inputString
|
||||||
|
else if(islower(letter)){
|
||||||
|
inputString += toupper(letter);
|
||||||
|
}
|
||||||
|
//If it is anything else ignore it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Atbash::getInputString() const{
|
||||||
|
return inputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Atbash::getOutputString() const{
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Atbash::encode(){
|
||||||
|
//Step through every element in the inputString and shift it the correct amount
|
||||||
|
for(int cnt = 0;cnt < inputString.size();++cnt){
|
||||||
|
outputString += (inputString[cnt] + 25 - (2 * (inputString[cnt] - 'A')));
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Atbash::encode(std::string input){
|
||||||
|
setInputString(input);
|
||||||
|
return encode();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Atbash::decode(){
|
||||||
|
//Make sure outputString is empty
|
||||||
|
outputString = "";
|
||||||
|
for(int cnt = 0;cnt < inputString.size();++cnt){
|
||||||
|
outputString += (inputString[cnt] + 25 - (2 * (inputString[cnt] - 'A')));
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string Atbash::decode(std::string input){
|
||||||
|
setInputString(input);
|
||||||
|
return decode();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Atbash::reset(){
|
||||||
|
inputString = outputString = "";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user