diff --git a/.cproject b/.cproject new file mode 100644 index 0000000..3068143 --- /dev/null +++ b/.cproject @@ -0,0 +1,109 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4144fa1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,38 @@ +# Prerequisites +*.d + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# XCode project +*.xcodeproj +xcuserdata/ +EnigmaMachine.xcodeproj +.DS_Store diff --git a/.project b/.project new file mode 100644 index 0000000..002dab6 --- /dev/null +++ b/.project @@ -0,0 +1,27 @@ + + + Enigma-Simulator + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/.settings/language.settings.xml b/.settings/language.settings.xml new file mode 100644 index 0000000..c4cae33 --- /dev/null +++ b/.settings/language.settings.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.settings/org.eclipse.cdt.managedbuilder.core.prefs b/.settings/org.eclipse.cdt.managedbuilder.core.prefs new file mode 100644 index 0000000..55778d9 --- /dev/null +++ b/.settings/org.eclipse.cdt.managedbuilder.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/CPATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/CPATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/CPLUS_INCLUDE_PATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/CPLUS_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/C_INCLUDE_PATH/delimiter=; +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/C_INCLUDE_PATH/operation=remove +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/append=true +environment/buildEnvironmentInclude/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/appendContributed=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/LIBRARY_PATH/delimiter=; +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/LIBRARY_PATH/operation=remove +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/append=true +environment/buildEnvironmentLibrary/cdt.managedbuild.config.gnu.mingw.exe.debug.1340108827/appendContributed=true diff --git a/EnigmaMachine/EnigmaMachine/EnigmaDecrypter.cpp b/EnigmaMachine/EnigmaMachine/EnigmaDecrypter.cpp new file mode 100644 index 0000000..1e86b0e --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/EnigmaDecrypter.cpp @@ -0,0 +1,66 @@ +// +// EnigmaDecrypter.cpp +// EnigmaMachine +// +// Created by Rajat Agarwal on 12/07/20. +// Copyright © 2020 Rajat Agarwal. All rights reserved. +// + +#include "EnigmaDecrypter.hpp" +#include +#include "Utils/config.h" +#include + +EnigmaDecrypter::EnigmaDecrypter() { } + + +/*bool EnigmaDecrypter::offsetIterator(){ + for(int i = 0; i<26; i++){ + for(int j = 0; j<26; j++){ + for(int k = 0; k<26; k++){ + int arr[]={k, j, i}; + _enigmaMachine.setRotorOffset(arr); + std::string decoded = _enigmaMachine.encode(_crib); + if(decoded == _plaintxt){ + _config.offsets[0]=k; + _config.offsets[1]=j; + _config.offsets[2]=i; + return true; + } + } + } + } + return false; +} +*/ + +bool EnigmaDecrypter::offsetIterator(){ + for(int i = 0; i<26; i++){ + for(int j = 0; j<26; j++){ + for(int k = 0; k<26; k++){ + int arr[]={k, j, i}; + _enigmaMachine.setRotorOffset(arr); + std::string decoded = _enigmaMachine.encode(_crib); + if(decoded == _plaintxt){ + _config.offsets[0]=k; + _config.offsets[1]=j; + _config.offsets[2]=i; + return true; + } + } + } + } + return false; +} + +bool EnigmaDecrypter::run(std::string crib, std::string plaintxt){ + crib = custom_utils::removeSpecial(crib); + _crib = custom_utils::toUppercase(crib); + plaintxt = custom_utils::removeSpecial(plaintxt); + _plaintxt = custom_utils::toUppercase(plaintxt); + return offsetIterator(); +} + +RotorConfig EnigmaDecrypter::getRotorConfig(){ + return _config; +} diff --git a/EnigmaMachine/EnigmaMachine/EnigmaDecrypter.hpp b/EnigmaMachine/EnigmaMachine/EnigmaDecrypter.hpp new file mode 100644 index 0000000..ba51923 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/EnigmaDecrypter.hpp @@ -0,0 +1,31 @@ +// +// EnigmaDecrypter.hpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once + +#include "EnigmaMachine.hpp" +#include "RotorConfig.hpp" + +class EnigmaDecrypter { +private: + EnigmaMachine _enigmaMachine; + RotorConfig _config; + bool _keyFound; + std::string _crib; + std::string _plaintxt; + bool offsetIterator(); + + +public: + EnigmaDecrypter(); + void rotorTypeIterator(); + void reflectorIterator(); + RotorConfig getRotorConfig(); + std::string decrypt(); + bool run(std::string crib, std::string plaintxt); +}; diff --git a/EnigmaMachine/EnigmaMachine/EnigmaMachine.cpp b/EnigmaMachine/EnigmaMachine/EnigmaMachine.cpp new file mode 100644 index 0000000..1d89ac6 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/EnigmaMachine.cpp @@ -0,0 +1,35 @@ +// +// Enigma.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 10/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#include "EnigmaMachine.hpp" +#include "Utils/utils.hpp" +#include "RotorConfig.hpp" +EnigmaMachine::EnigmaMachine(RotorConfig r_config): _rotorSet(r_config) { } + +EnigmaMachine::EnigmaMachine() { } + +std::string EnigmaMachine::encode(std::string inputStr){ + std::string inpCopy = custom_utils::removeSpecial(inputStr); + + inpCopy = custom_utils::toUppercase(inpCopy); + + std::string outputStr; + for (int i = 0; i < inpCopy.size(); i++){ + char charOut = _rotorSet.parseValue( inpCopy[i] ); + outputStr.push_back(charOut); + } + + return outputStr; +} + + +void EnigmaMachine::setRotorOffset(int offsets[NO_OF_ROTORS]){ + for(int i = 0; i +#include + +#include "RotorSet.hpp" +#include "RotorConfig.hpp" +#include "Utils/config.h" + +class EnigmaMachine { + RotorSet _rotorSet; + +public: + EnigmaMachine(RotorConfig r_config); + EnigmaMachine(); + + std::string encode(std::string input); + void setRotorOffset(int[NO_OF_ROTORS]); + void setRotorType(); + void setReflectorType(); +}; diff --git a/EnigmaMachine/EnigmaMachine/Reflector.cpp b/EnigmaMachine/EnigmaMachine/Reflector.cpp new file mode 100644 index 0000000..3fd4460 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/Reflector.cpp @@ -0,0 +1,49 @@ +// +// Reflector.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#include "Reflector.hpp" +#include + +Reflector::Reflector(int type){ + _rotorNo = type; + setWiring(_rotorNo); +} + +char Reflector::runThrough(char input, bool forward = true) { + int intInp; + char output; + + forward = true; + + intInp = alphabet.find(input); + output = _wiring[intInp]; + output = nextRot -> runThrough(output, false); + + return output; +} + +void Reflector::setWiring(int type){ + _rotorNo = type; + switch(type){ + case REFLECTOR_B: + _wiring = wiring_reflectorB; + break; + case REFLECTOR_B_THIN: + _wiring = wiring_reflectorB_thin; + break; + case REFLECTOR_C: + _wiring = wiring_reflectorC; + break; + case REFLECTOR_C_THIN: + _wiring = wiring_reflectorC_thin; + break; + default: + _wiring = alphabet; + break; + } +} diff --git a/EnigmaMachine/EnigmaMachine/Reflector.hpp b/EnigmaMachine/EnigmaMachine/Reflector.hpp new file mode 100644 index 0000000..0b52111 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/Reflector.hpp @@ -0,0 +1,20 @@ +// +// Reflector.hpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once + +#include "Rotor.hpp" +#include "Utils/config.h" + +class Reflector : public Rotor { +public: + Reflector(int type); + char runThrough(char input, bool forward); + void setWiring(int type); + void rotate() {}; +}; diff --git a/EnigmaMachine/EnigmaMachine/Rotor.cpp b/EnigmaMachine/EnigmaMachine/Rotor.cpp new file mode 100644 index 0000000..4b83f75 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/Rotor.cpp @@ -0,0 +1,91 @@ +// +// Rotors.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 10/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#include "Rotor.hpp" +#include + +Rotor::Rotor(int rotorNumber, int offset) { + _rotorNo = rotorNumber; + _offset = offset; + + prevRot = nullptr; + nextRot = nullptr; + + setWiring(_rotorNo); +} + +void Rotor::setWiring(int type){ + _rotorNo = type; + + switch(type) { + case ROTOR_1: + _wiring = wiring_rotor1; + _notch = rot1_notch; + break; + case ROTOR_2: + _wiring = wiring_rotor2; + _notch = rot2_notch; + break; + case ROTOR_3: + _wiring = wiring_rotor3; + _notch = rot3_notch; + break; + case ROTOR_4: + _wiring = wiring_rotor4; + _notch = rot4_notch; + break; + case ROTOR_5: + _wiring = wiring_rotor5; + _notch = rot5_notch; + break; + default: + _wiring = alphabet; + _notch = 'A'; + break; + } +} + +char Rotor::runThrough(char input, bool forward){ + int intInp; + char output; + + if (forward) { + intInp = alphabet.find(input); + intInp = (intInp + _offset) % 26; + output = _wiring[intInp]; + output = nextRot -> runThrough(output, true); + } else { + intInp = _wiring.find(input); + intInp = (intInp - _offset)%26; + if (intInp < 0){ + intInp += 26; + } + output = alphabet[intInp]; + if(prevRot != nullptr){ + output = prevRot -> runThrough(output, false); + } + + if (alphabet[_offset] == _notch){ + nextRot -> rotate(); + } + } + + return output; +} + +void Rotor::rotate(){ + _offset = (_offset + 1) % 26; +} + +int Rotor::position(){ + return _offset; +} + +void Rotor::setOffset(int num){ + _offset = num % 26; +} diff --git a/EnigmaMachine/EnigmaMachine/Rotor.hpp b/EnigmaMachine/EnigmaMachine/Rotor.hpp new file mode 100644 index 0000000..e8328ce --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/Rotor.hpp @@ -0,0 +1,37 @@ +// +// Rotors.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 10/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once + +#include "Utils/config.h" +#include "Utils/utils.hpp" + +#include + +class Rotor { +private: + int _offset; + char _notch; + +protected: + int _rotorNo; + std::string _wiring; + +public: + Rotor* nextRot; + Rotor* prevRot; + + Rotor(){ }; + Rotor(int rotorNumber, int offset); + virtual char runThrough(char input, bool forward); + virtual void rotate(); + int position(); + + virtual void setWiring(int type); + void setOffset(int num); +}; diff --git a/EnigmaMachine/EnigmaMachine/RotorConfig.cpp b/EnigmaMachine/EnigmaMachine/RotorConfig.cpp new file mode 100644 index 0000000..2c3c81d --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/RotorConfig.cpp @@ -0,0 +1,43 @@ +// +// RotorConfig.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#include "RotorConfig.hpp" + +RotorConfig::RotorConfig(int r_offsets[NO_OF_ROTORS], int r_types[NO_OF_ROTORS], int r_reflectorType){ + for (int i = 0; i < NO_OF_ROTORS; i++){ + offsets[i] = r_offsets[i]; + types[i] = r_types[i]; + } + reflectorType = r_reflectorType; +} + +bool RotorConfig::operator == (const RotorConfig& rhs){ + for (int i = 0; i < NO_OF_ROTORS; i++){ + if (offsets[i] != rhs.offsets[i] || types[i] != rhs.types[i]){ + return false; + } + } + return reflectorType == rhs.reflectorType; +} + +RotorConfig::RotorConfig(const RotorConfig& rc){ + for (int i = 0; i < NO_OF_ROTORS; i++){ + offsets[i] = rc.offsets[i]; + types[i] = rc.types[i]; + } + reflectorType = rc.reflectorType; +} + +RotorConfig::RotorConfig(){ + for (int i = 0; i < NO_OF_ROTORS; i++){ + offsets[i] = 1; + types[i] = i; + } + reflectorType = REFLECTOR_B; +} + diff --git a/EnigmaMachine/EnigmaMachine/RotorConfig.hpp b/EnigmaMachine/EnigmaMachine/RotorConfig.hpp new file mode 100644 index 0000000..dbf5f96 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/RotorConfig.hpp @@ -0,0 +1,23 @@ +// +// RotorConfig.hpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once + +#include "Utils/config.h" + +struct RotorConfig { + int offsets[NO_OF_ROTORS]; + int types[NO_OF_ROTORS]; + int reflectorType; + RotorConfig(const RotorConfig&); + RotorConfig(); + + RotorConfig(int r_offsets[NO_OF_ROTORS], int r_types[NO_OF_ROTORS], int r_reflectorType); + + bool operator == (const RotorConfig& rhs); +}; diff --git a/EnigmaMachine/EnigmaMachine/RotorSet.cpp b/EnigmaMachine/EnigmaMachine/RotorSet.cpp new file mode 100644 index 0000000..b0c6c42 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/RotorSet.cpp @@ -0,0 +1,98 @@ +// +// RotorSet.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#include "RotorSet.hpp" +#include +RotorSet::RotorSet(RotorConfig r_config): _config(r_config) { + for (int i = 0; i < NO_OF_ROTORS; i++){ + _rotors[i] = new Rotor ( _config.types[i], _config.offsets[i] ); + } + _reflector = new Reflector ( _config.reflectorType ); + + for (int i = 0; i < NO_OF_ROTORS - 1; i++){ + _rotors[i]->nextRot = _rotors[i+1]; + _rotors[i+1]->prevRot = _rotors[i]; + } + + _rotors[NO_OF_ROTORS - 1]->nextRot = _reflector; + _reflector->nextRot = _rotors[NO_OF_ROTORS - 1]; +} + +RotorSet::~RotorSet(){ + for (int i = 0; i < NO_OF_ROTORS; i++){ + delete _rotors[i]; + } +} + +int RotorSet::parseValue(char input){ + char value = _rotors[0]->runThrough(input, true); + _rotors[0] -> rotate(); + + return value; +} + + +void RotorSet::setRotorOffset(int rotPos, int offset){ + if (rotPos >= NO_OF_ROTORS) return; + + offset %= 26; + _rotors[rotPos]->setOffset(offset); + + _config.offsets[rotPos] = offset; +} + +void RotorSet::setReflectorType(int type){ + _reflector->setWiring(type); + _config.reflectorType = type; +} + +void RotorSet::setRotorType(int rotPos, int type){ + if (rotPos >= NO_OF_ROTORS) return; + + _rotors[rotPos]->setWiring(type); + _config.types[rotPos] = type; + +} + +void RotorSet::randomConfig(){ + srand(time(0)); + int type; int offset; + std::set typesUsed; + + for (int i = 0; i < NO_OF_ROTORS; i++){ + offset = rand() % 26; + + do { + type = rand() % NO_ROTOR_TYPES; + } while (typesUsed.count(type) > 0); + + typesUsed.insert(type); + + setRotorType(i, type); + setRotorOffset(i, offset); + } + + type = rand() % NO_REFLECTOR_TYPES; + setReflectorType(type); +} + + +RotorSet::RotorSet() { + for (int i = 0; i < NO_OF_ROTORS; i++){ + _rotors[i] = new Rotor ( _config.types[i], _config.offsets[i] ); + } + _reflector = new Reflector ( _config.reflectorType ); + + for (int i = 0; i < NO_OF_ROTORS - 1; i++){ + _rotors[i]->nextRot = _rotors[i+1]; + _rotors[i+1]->prevRot = _rotors[i]; + } + + _rotors[NO_OF_ROTORS - 1]->nextRot = _reflector; + _reflector->nextRot = _rotors[NO_OF_ROTORS - 1]; +} diff --git a/EnigmaMachine/EnigmaMachine/RotorSet.hpp b/EnigmaMachine/EnigmaMachine/RotorSet.hpp new file mode 100644 index 0000000..aaf276f --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/RotorSet.hpp @@ -0,0 +1,38 @@ +// +// RotorSet.hpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once + +#include +#include +#include +#include + +#include "Rotor.hpp" +#include "Reflector.hpp" +#include "RotorConfig.hpp" +#include "Utils/config.h" + +class RotorSet { +private: + Rotor* _rotors[NO_OF_ROTORS]; + Rotor* _reflector; + + RotorConfig _config; + +public: + RotorSet(RotorConfig r_config); + ~RotorSet(); + int parseValue(char input); + RotorSet(); + void setRotorOffset(int rotPos, int offset); + void setRotorType(int rotPos, int type); + void setReflectorType(int type); + + void randomConfig(); +}; diff --git a/EnigmaMachine/EnigmaMachine/Utils/config.h b/EnigmaMachine/EnigmaMachine/Utils/config.h new file mode 100644 index 0000000..7a61b16 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/Utils/config.h @@ -0,0 +1,72 @@ +// +// config.h +// EnigmaMachine +// +// Created by Ayush Agrawal on 10/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once + +#include + +//Rotor names +#define ROTOR_1 0 +#define ROTOR_2 1 +#define ROTOR_3 2 +#define ROTOR_4 3 +#define ROTOR_5 4 + +#define REFLECTOR_B 0 +#define REFLECTOR_B_THIN 1 +#define REFLECTOR_C 2 +#define REFLECTOR_C_THIN 3 + +#define NO_ROTOR_TYPES 5 +#define NO_REFLECTOR_TYPES 4 + +#define NO_OF_ROTORS 3 + +//Rotor Wirings +//const int wiring_rotor1[][2] = { {0, 15}, {1, 4}, {2, 25}, {3, 20}, {4, 14}, {5, 7}, {6, 23}, {7, 18}, {8, 2}, +// {9, 21}, {10, 5}, {11, 12}, {12, 19}, {13, 1}, {14, 6}, {15, 11}, {16, 17}, {17, 8}, +// {18, 13}, {19, 16}, {20, 9}, {21, 22}, {22, 0}, {23, 24}, {24, 3}, {25, 10} }; +// +//const int wiring_rotor2[][2] = { {0, 25}, {1, 14}, {2, 20}, {3, 4}, {4, 18}, {5, 24}, {6, 3}, {7, 10}, {8, 5}, +// {9, 22}, {10, 15}, {11, 2}, {12, 8}, {13, 16}, {14, 23}, {15, 7}, {16, 12}, {17, 21}, +// {18, 1}, {19, 11}, {20, 6}, {21, 13}, {22, 9}, {23, 17}, {24, 0}, {25, 19} }; +// +//const int wiring_rotor3[][2] = { {0, 4}, {1, 7}, {2, 17}, {3, 21}, {4, 23}, {5, 6}, {6, 0}, {7, 14}, {8, 1}, +// {9, 16}, {10, 20}, {11, 18}, {12, 8}, {13, 12}, {14, 25}, {15, 5}, {16, 11}, {17, 24}, +// {18, 13}, {19, 22}, {20, 10}, {21, 19}, {22, 15}, {23, 3}, {24, 9}, {25, 2} }; +// +//const int wiring_rotor4[][2] = { {0, 8}, {1, 12}, {2, 4}, {3, 19}, {4, 2}, {5, 6}, {6, 5}, {7, 17}, {8, 0}, +// {9, 24}, {10, 18}, {11, 16}, {12, 1}, {13, 25}, {14, 23}, {15, 22}, {16, 11}, {17, 7}, +// {18, 10}, {19, 3}, {20, 21}, {21, 20}, {22, 15}, {23, 14}, {24, 9}, {25, 13} }; +// +//const int wiring_rotor5[][2] = { {0, 16}, {1, 22}, {2, 4}, {3, 17}, {4, 19}, {5, 25}, {6, 20}, {7, 8}, {8, 14}, +// {9, 0}, {10, 18}, {11, 3}, {12, 5}, {13, 6}, {14, 7}, {15, 9}, {16, 10}, {17, 15}, +// {18, 24}, {19, 23}, {20, 2}, {21, 21}, {22, 1}, {23, 13}, {24, 12}, {25, 11} }; +// +//const int wiring_reflector[][2] = { {0, 21}, {1, 10}, {2, 22}, {3, 17}, {4, 6}, {5, 8}, {6, 4}, {7, 19}, {8, 5}, +// {9, 25}, {10, 1}, {11, 20}, {12, 18}, {13, 15}, {14, 16}, {15, 13}, {16, 14}, {17, 3}, +// {18, 12}, {19, 7}, {20, 11}, {21, 0}, {22, 2}, {23, 24}, {24, 23}, {25, 9} }; + + +const std::string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +const std::string wiring_rotor1 = "EKMFLGDQVZNTOWYHXUSPAIBRCJ"; +const std::string wiring_rotor2 = "AJDKSIRUXBLHWTMCQGZNPYFVOE"; +const std::string wiring_rotor3 = "BDFHJLCPRTXVZNYEIWGAKMUSQO"; +const std::string wiring_rotor4 = "ESOVPZJAYQUIRHXLNFTGKDCMWB"; +const std::string wiring_rotor5 = "VZBRGITYUPSDNHLXAWMJQOFECK"; +const std::string wiring_reflectorB = "EJMZALYXVBWFCRQUONTSPIKHGD"; +const std::string wiring_reflectorC = "FVPJIAOYEDRZXWGCTKUQSBNMHL"; +const std::string wiring_reflectorB_thin = "ENKQAUYWJICOPBLMDXZVFTHRGS"; +const std::string wiring_reflectorC_thin = "RDOBJNTKVEHMLFCWZAXGYIPSUQ"; + +const char rot1_notch = 'Q'; +const char rot2_notch = 'E'; +const char rot3_notch = 'V'; +const char rot4_notch = 'J'; +const char rot5_notch = 'Z'; diff --git a/EnigmaMachine/EnigmaMachine/Utils/utils.hpp b/EnigmaMachine/EnigmaMachine/Utils/utils.hpp new file mode 100644 index 0000000..25940a2 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/Utils/utils.hpp @@ -0,0 +1,34 @@ +// +// ArrayFunctions.hpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 11/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#pragma once +#include +#include +namespace custom_utils { + inline void copyArr2D(int arTo[26][2], const int arFrom[26][2], int size = 26){ + for (int i = 0; i < size; i++){ + for (int j = 0; j < 2; j++){ + arTo[i][j] = arFrom[i][j]; + } + } + } + + inline std::string removeSpecial(std::string str){ + str.erase(remove_if(str.begin(), str.end(), isspace),str.end()); + str.erase(remove_if(str.begin(), str.end(), isdigit),str.end()); + str.erase(remove_if(str.begin(), str.end(), ispunct),str.end()); + + return str; + } + + inline std::string toUppercase(std::string str){ + std::transform(str.begin(), str.end(),str.begin(), ::toupper); + return str; + } + +} diff --git a/EnigmaMachine/EnigmaMachine/main.cpp b/EnigmaMachine/EnigmaMachine/main.cpp new file mode 100644 index 0000000..7bd9f52 --- /dev/null +++ b/EnigmaMachine/EnigmaMachine/main.cpp @@ -0,0 +1,51 @@ +// +// main.cpp +// EnigmaMachine +// +// Created by Ayush Agrawal on 10/07/20. +// Copyright © 2020 Ayush Agrawal. All rights reserved. +// + +#include +#include +#include +#include + +#include "EnigmaMachine.hpp" +#include "Utils/config.h" +#include "RotorConfig.hpp" +#include "EnigmaDecrypter.hpp" + +int main(int argc, const char * argv[]) { + int rotorNumbers[NO_OF_ROTORS] = {ROTOR_1, ROTOR_2, ROTOR_3}; + int offsets[NO_OF_ROTORS]; + + + srand(time(0)); + + for (int i = 0; i < NO_OF_ROTORS; i++){ + offsets[i] = rand() % 26; + std::cout << offsets[i] << " "; + } + + RotorConfig rConfig(offsets, rotorNumbers, REFLECTOR_B); + + EnigmaMachine enigmaMachine1(rConfig); + EnigmaMachine enigmaMachine2(rConfig); + + std::string message; + + getline(std::cin, message); + + std::string encoded = enigmaMachine1.encode(message); + + std::cout << encoded << "\n"; + std::cout << enigmaMachine2.encode(encoded) << "\n"; + EnigmaDecrypter enigmaDecrypter; + enigmaDecrypter.run(encoded, message); + RotorConfig dConfig=enigmaDecrypter.getRotorConfig(); + for(int i = 0;i