mirror of
https://bitbucket.org/Mattrixwv/cipherstream.git
synced 2025-12-06 18:33:58 -05:00
Created the Vigenere class for Vigenere Ciphers
This commit is contained in:
37
Headers/Vigenere.hpp
Normal file
37
Headers/Vigenere.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
//Ciphers/SourceFiles/Vigenere.hpp
|
||||
//Matthew Ellison
|
||||
// Created: 4-29-18
|
||||
//Modified; 4-29-18
|
||||
//This file contains the declaration of the Vigenere class
|
||||
|
||||
#ifndef VIGENERE_HPP
|
||||
#define VIGENERE_HPP
|
||||
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Vigenere{
|
||||
private:
|
||||
std::string inputString; //This is the string that you want to encode or decode
|
||||
std::string outputString; //This is the string that is output from encoding or decoding
|
||||
std::string keyword; //This is the keyword that is the resposible for determining the offsets that you change each character by
|
||||
std::vector<unsigned int> offset; //This holds the offsets computed from each character in the keyword
|
||||
void setOffset();
|
||||
public:
|
||||
Vigenere();
|
||||
~Vigenere();
|
||||
void setInputString(std::string input);
|
||||
std::string getInputString() const;
|
||||
std::string getOutputString() const;
|
||||
void setKeyword(std::string key);
|
||||
std::string getKeyword() const;
|
||||
std::vector<unsigned int> getOffsets() const;
|
||||
std::string encode();
|
||||
std::string encode(std::string key, std::string input);
|
||||
std::string decode();
|
||||
std::string decode(std::string key, std::string input);
|
||||
void reset();
|
||||
};
|
||||
|
||||
#endif //VIGENERE_HPP
|
||||
139
SourceFiles/Vigenere.cpp
Normal file
139
SourceFiles/Vigenere.cpp
Normal file
@@ -0,0 +1,139 @@
|
||||
//Ciphers/SourceFiles/Vigenere.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 4-29-18
|
||||
//Modified; 4-29-18
|
||||
//This file contains the implementation of the Vigenere class
|
||||
|
||||
#include "../Headers/Vigenere.hpp"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <cctype>
|
||||
|
||||
|
||||
Vigenere::Vigenere(){
|
||||
}
|
||||
|
||||
Vigenere::~Vigenere(){
|
||||
}
|
||||
|
||||
void Vigenere::setInputString(std::string input){
|
||||
//Make sure inputString is clear
|
||||
inputString = "";
|
||||
|
||||
//Loop through every character in input. Remove all whitespace and punctuation and make sure all letters are capital and add it to inputString
|
||||
for(int cnt = 0;cnt < input.size();++cnt){
|
||||
char letter = input[cnt];
|
||||
if(isupper(letter)){
|
||||
inputString += letter;
|
||||
}
|
||||
else if(islower(letter)){
|
||||
inputString += toupper(letter);
|
||||
}
|
||||
//If the letter is whitespace or punctuation do nothing
|
||||
}
|
||||
}
|
||||
|
||||
std::string Vigenere::getInputString() const{
|
||||
return inputString;
|
||||
}
|
||||
|
||||
std::string Vigenere::getOutputString() const{
|
||||
return outputString;
|
||||
}
|
||||
|
||||
void Vigenere::setKeyword(std::string key){
|
||||
//Make sure the keyword is blank
|
||||
keyword = "";
|
||||
//Loop through every letter in the key and make sure all of them are uppercase letters
|
||||
for(int cnt = 0;cnt < key.size();++cnt){
|
||||
char letter = key[cnt];
|
||||
if(isupper(letter)){
|
||||
keyword += letter;
|
||||
}
|
||||
else if(islower(letter)){
|
||||
keyword += toupper(letter);
|
||||
}
|
||||
//If it is not a letter ignore it
|
||||
}
|
||||
setOffset();
|
||||
}
|
||||
|
||||
std::string Vigenere::getKeyword() const{
|
||||
return keyword;
|
||||
}
|
||||
|
||||
void Vigenere::setOffset(){
|
||||
//Make sure offset is empty
|
||||
offset.clear();
|
||||
//Reserve the correct size to increase speed later
|
||||
offset.reserve(keyword.size());
|
||||
|
||||
//Loop through every letter in keyword and get the offset from A
|
||||
for(char letter : keyword){
|
||||
offset.push_back((letter - 'A') % 26);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<unsigned int> Vigenere::getOffsets() const{
|
||||
return offset;
|
||||
}
|
||||
|
||||
std::string Vigenere::encode(){
|
||||
//Clear the outputString
|
||||
outputString = "";
|
||||
//Reserve the correct size for the output string to increase speed for longer messages
|
||||
outputString.reserve(inputString.size());
|
||||
|
||||
//Step through every charater in the inputString and advance it the correct amount, according to offset
|
||||
for(int cnt = 0;cnt < inputString.size();++cnt){
|
||||
char letter = (inputString[cnt] + offset[cnt % offset.size()]); //By using % you allow it to loop without having a separate counter
|
||||
//Make sure the character is still a letter, if not, wrap around
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
outputString += letter;
|
||||
}
|
||||
|
||||
return outputString;
|
||||
}
|
||||
|
||||
std::string Vigenere::encode(std::string key, std::string input){
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
return encode();
|
||||
}
|
||||
|
||||
std::string Vigenere::decode(){
|
||||
//Clear the outputString
|
||||
outputString = "";
|
||||
//Reserve the correct size for the output string to increase speed for longer messages
|
||||
outputString.reserve(inputString.size());
|
||||
|
||||
//Step through every charater in the inputString and reduce it the correct amount, according to offset
|
||||
for(int cnt = 0;cnt < inputString.size();++cnt){
|
||||
char letter = (inputString[cnt] - offset[cnt % offset.size()]); //By using % you allow it to loop without having a separate counter
|
||||
if(letter < 'A'){
|
||||
letter += 26;
|
||||
}
|
||||
else if(letter > 'Z'){
|
||||
letter -= 26;
|
||||
}
|
||||
outputString += letter;
|
||||
}
|
||||
|
||||
return outputString;
|
||||
}
|
||||
|
||||
std::string Vigenere::decode(std::string key, std::string input){
|
||||
setKeyword(key);
|
||||
setInputString(input);
|
||||
return decode();
|
||||
}
|
||||
|
||||
void Vigenere::reset(){
|
||||
inputString = outputString = keyword = "";
|
||||
offset.clear();
|
||||
}
|
||||
Reference in New Issue
Block a user