Last active
December 3, 2018 15:35
-
-
Save almeida1492/e490bdadab95e8428338001c6fb02029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// enigma | |
// | |
// Created by Henrique de Almeida on 27/10/2018. | |
// Copyright © 2018 Henrique de Almeida. All rights reserved. | |
// | |
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <vector> | |
#include <algorithm> | |
unsigned int rotorIPos = 0; | |
unsigned int rotorIIPos = 0; | |
unsigned int rotorIIIPos = 0; | |
const unsigned int GOING = 1; | |
const unsigned int COMING = 2; | |
const unsigned int READING = 3; | |
const unsigned int WRITING = 4; | |
const unsigned int ALPHABET_LENGTH = 26; | |
char pins[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'W', 'Y', 'Z'}; | |
char rotorIPlates[] = {'T', 'Y', 'K', 'Q', 'W', 'E', 'P', 'V', 'X', 'U', 'R', 'Z', 'N', 'H', 'F', 'M', 'J', 'S', 'D', 'O', 'C', 'G', 'L', 'A', 'I', 'B'}; | |
char rotorIIPlates[] = {'P', 'Z', 'I', 'K', 'L', 'E', 'G', 'F', 'J', 'Q', 'S', 'T', 'V', 'A', 'N', 'D', 'B', 'X', 'W', 'M', 'R', 'Y', 'H', 'C', 'U', 'O'}; | |
char rotorIIIPlates[] = {'V', 'K', 'R', 'D', 'X', 'H', 'L', 'M', 'U', 'J', 'Q', 'N', 'E', 'B', 'Z', 'Y', 'G', 'C', 'S', 'O', 'A', 'W', 'T', 'P', 'I', 'F'}; | |
//char reflector[] = {'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'W', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E'}; | |
static char rotor(unsigned int flow, char *plates, unsigned int pos, char input); | |
static void refreshPositions(unsigned int flow); | |
static char encript(char input); | |
static char decript(char input); | |
static char reflect(char input, char *plates); | |
int main(int argc, const char * argv[]) { | |
std::string plain = "OIISABELA"; | |
std::string encripted; | |
for (int i = 0; i < plain.size(); i++) { | |
char temp = encript(plain[i]); | |
encripted.append(&temp); | |
//std::cout << rotorIIIPos << " " << rotorIIPos << " " << rotorIPos << std::endl; | |
} | |
std::cout << std::endl << " Writing" << std::endl; | |
std::cout << "" << std::endl; | |
std::cout << "Mensagem original: " << plain << std::endl; | |
std::cout << "Mensagem encriptada: "; | |
for (int i = 0; i < encripted.length(); i++) { | |
if (encripted[i] >= 'A' && encripted[i] <= 'Z') { | |
std::cout << encripted[i]; | |
} | |
} | |
std::cout << "" << std::endl << std::endl; | |
//---------------------------------------------------------------------------------------------------- | |
rotorIPos = 0; | |
rotorIIPos = 0; | |
rotorIIIPos = 0; | |
std::string encripted2 = "MUEHLEICO"; | |
std::string plain2; | |
for (int i = 0; i < encripted2.size(); i++) { | |
char temp = decript(encripted2[i]); | |
plain2.append(&temp); | |
//std::cout << rotorIIIPos << " " << rotorIIPos << " " << rotorIPos << std::endl; | |
} | |
std::cout << " Reading" << std::endl; | |
std::cout << "" << std::endl; | |
std::cout << "Mensagem encriptada: " << encripted2 << std::endl; | |
std::cout << "Mensagem original: " << plain << std::endl << std::endl; | |
return 0; | |
} | |
static char rotor(unsigned int flow, char *plates, unsigned int pos, char input){ | |
unsigned int index; | |
if (flow == GOING) { | |
for (unsigned int i = 0; i < ALPHABET_LENGTH; i++){ | |
index = (i + pos) % 26; | |
if (input == pins[i]) { | |
return plates[index]; | |
} | |
} | |
} else if (flow == COMING){ | |
for (unsigned int i = 0; i < ALPHABET_LENGTH; i++){ | |
index = (i + pos) % 26; | |
if (input == plates[i]) { | |
return pins[index]; | |
} | |
} | |
} | |
return NULL; | |
} | |
static char rotor2(unsigned int flow, char *plates, unsigned int pos, char input){ | |
unsigned int index; | |
if (flow == GOING) { | |
for (unsigned int i = 0; i < ALPHABET_LENGTH; i++){ | |
index = (i - pos + 26) % 26; | |
if (input == pins[i]) { | |
return plates[index]; | |
} | |
} | |
} else if (flow == COMING){ | |
for (unsigned int i = 0; i < ALPHABET_LENGTH; i++){ | |
index = (i - pos + 26) % 26; | |
if (input == plates[i]) { | |
return pins[index]; | |
} | |
} | |
} | |
return NULL; | |
} | |
static char reflect(int mode, char input, char *plates){ | |
if (mode == WRITING) { | |
for (int i = 0; i < ALPHABET_LENGTH; i++) { | |
if (plates[i] == input) { | |
return rotorIIIPlates[(i - 4 + 26)%26]; | |
} | |
} | |
} | |
if (mode == READING){ | |
for (int i = 0; i < ALPHABET_LENGTH; i++) { | |
if (plates[i] == input) { | |
return rotorIIIPlates[(i + 4)%26]; | |
} | |
} | |
} | |
return NULL; | |
} | |
static void refreshPositions(unsigned int flow){ | |
if (flow == WRITING) { | |
if (rotorIPos == ALPHABET_LENGTH - 1) { | |
rotorIPos = 0; | |
if (rotorIIPos == ALPHABET_LENGTH - 1) { | |
rotorIIPos = 0; | |
if (rotorIIIPos == ALPHABET_LENGTH - 1) { | |
rotorIIIPos = 0; | |
} else { | |
rotorIIIPos++; | |
} | |
} else { | |
rotorIIPos++; | |
} | |
} else { | |
rotorIPos++; | |
} | |
} else if (flow == READING) { | |
if (rotorIPos == 0) { | |
rotorIPos = ALPHABET_LENGTH - 1; | |
if (rotorIIPos == 0) { | |
rotorIIPos = ALPHABET_LENGTH - 1; | |
if (rotorIIIPos == 0) { | |
rotorIIIPos = ALPHABET_LENGTH - 1; | |
} else { | |
rotorIIIPos--; | |
} | |
} else { | |
rotorIIPos--; | |
} | |
} else { | |
rotorIPos--; | |
} | |
} | |
} | |
static char encript(char input){ | |
char currentCharacter = input; | |
currentCharacter = rotor(GOING, rotorIPlates, rotorIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor(GOING, rotorIIPlates, rotorIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor(GOING, rotorIIIPlates, rotorIIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = reflect(WRITING, currentCharacter, rotorIIIPlates);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor(COMING, rotorIIIPlates, rotorIIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor(COMING, rotorIIPlates, rotorIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor(COMING, rotorIPlates, rotorIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
refreshPositions(WRITING); | |
return currentCharacter; | |
} | |
static char decript(char input){ | |
char currentCharacter = input; | |
currentCharacter = rotor2(GOING, rotorIPlates, rotorIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor2(GOING, rotorIIPlates, rotorIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor2(GOING, rotorIIIPlates, rotorIIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = reflect(READING, currentCharacter, rotorIIIPlates); | |
currentCharacter = rotor2(COMING, rotorIIIPlates, rotorIIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor2(COMING, rotorIIPlates, rotorIIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
currentCharacter = rotor2(COMING, rotorIPlates, rotorIPos, currentCharacter);//std::cout << currentCharacter << std::endl; | |
refreshPositions(WRITING); | |
return currentCharacter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment