Created
June 26, 2025 19:34
-
-
Save Artegam/337d73c07a96e138f28053b1365b5265 to your computer and use it in GitHub Desktop.
Solution c++ pour l'épreuve "À l'aise" de Hackropole
This file contains hidden or 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
#include "Crypto.h" | |
#include <cstdlib> | |
#include <cassert> | |
using namespace Crypto; | |
Cesar::Cesar () { | |
} | |
char Cesar::encode (const char c, const unsigned int offset) { | |
return compute(c, offset, true); | |
} | |
string Cesar::encode (const char * message, const unsigned int offset) { | |
compute(message, offset, true); | |
return result; | |
} | |
char Cesar::decode (const char c, const unsigned int offset) { | |
return compute(c, offset, false); | |
} | |
string Cesar::decode (const char * message, const unsigned int offset) { | |
compute(message, offset, false); | |
return result; | |
} | |
char Cesar::compute (const char c, const unsigned int offset, bool toEncode) { | |
unsigned int pos = c; | |
int offsetComputed = offset; | |
if(pos >= 'a' && pos <= 'z') {// 97 decimal | |
if (toEncode) { | |
if (offsetComputed > 'z' - c) { | |
offsetComputed -= ('z' - c); | |
offsetComputed = 'a' + offsetComputed + 1; | |
} else if(offsetComputed <= 'z' - c) { | |
offsetComputed = c + offsetComputed; | |
} | |
} else { | |
if(offsetComputed > c - 'a') { | |
offsetComputed -= (c - 'a'); | |
offsetComputed = 'z' - offsetComputed + 1; | |
} else if(offsetComputed <= c - 'a') { | |
offsetComputed = c - offsetComputed; | |
} | |
} | |
} else if(pos >= 'A' && pos <= 'Z') { | |
if (toEncode) { | |
if (offsetComputed > 'Z' - c) { | |
offsetComputed -= ('Z' - c); | |
offsetComputed = 'A' + offsetComputed + 1; | |
} else if(offsetComputed <= 'Z' - c) { | |
offsetComputed = c + offsetComputed; | |
} | |
} else { | |
if(offsetComputed > c - 'A') { | |
offsetComputed -= (c - 'A'); | |
offsetComputed = 'Z' - offsetComputed + 1; | |
} else if(offsetComputed <= c - 'A') { | |
offsetComputed = c - offsetComputed; | |
} | |
} | |
} | |
return offsetComputed; | |
} | |
void Cesar::compute (const char * message, const unsigned int offset, bool toEncode) { | |
//[ASC] Un octet pour l adresse et un octet de fin de chaine | |
unsigned int max = strlen(message); | |
result.clear(); | |
for (unsigned int i = 0; i < max; i++) | |
result.push_back(compute(message[i], offset, toEncode)); | |
} |
This file contains hidden or 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
#ifndef CRYPTO_H | |
#define CRYPTO_H | |
#include <string.h> | |
#include <string> | |
using namespace std; | |
namespace Crypto { | |
class Cesar { | |
private: | |
string result; | |
char compute (const char c, const unsigned int offset, bool toEncode); | |
void compute (const char * message, const unsigned int offset, bool toEncode); | |
public: | |
Cesar (); | |
char encode (const char c, const unsigned int offset); | |
string encode (const char * message, const unsigned int offset); | |
char decode (const char c, const unsigned int offset); | |
string decode (const char * message, const unsigned int offset); | |
}; | |
class Vigenere { | |
private: | |
string key; | |
char c; | |
unsigned int cursor; | |
string result; | |
public: | |
Vigenere (string key); | |
void reset(); | |
bool isExcluded (const char c); | |
bool isNumeric (const char c); | |
unsigned int offset(char c); | |
char encode (const char c); | |
char decode (const char c); | |
string encode (const char * message); | |
string decode (const char * message); | |
string compute (const char * message, bool toEncode); | |
}; | |
}; | |
#endif |
This file contains hidden or 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
#include "Crypto.h" | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
using namespace Crypto; | |
int main(int argc, char** argv) { | |
string key = "FCSC"; | |
string codedMessage = "Gqfltwj emgj clgfv ! Aqltj rjqhjsksg ekxuaqs, ua xtwk n'feuguvwb gkwp xwj, ujts f'npxkqvjgw nw tjuwcz ugwygjtfkf qz uw efezg sqk gspwonu. Jgsfwb-aqmu f Pspygk nj 29 cntnn hqzt dg igtwy fw xtvjg rkkunqf."; | |
Vigenere v1(key); | |
cout << "==================================" << endl; | |
cout << "Dechiffrage de la totalite du message" << endl; | |
cout << "Cle de dechiffrage : " << key << endl; | |
cout << "Message chiffre :" << endl; | |
cout << codedMessage << endl; | |
cout << "----------------------------------" << endl; | |
cout << "le resultat est : " << endl; | |
cout << v1.decode(codedMessage.c_str()) << endl; | |
cout << "==================================" << endl; | |
return 0; | |
} |
This file contains hidden or 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
CXX := g++ | |
PROJECT := crypto | |
TESTS = tests/ | |
INC = includes/ | |
SRC = src/ | |
OUT = o/ | |
BIN = bin/ | |
INSTALL_DIR = /usr/bin/ | |
TEST_DIR = $(TESTS)$(SRC) | |
INCLUDES = -I $(INC) # -I $(INC_RENDER) -I $(TESTS) | |
LIBS = | |
OPT = -Wall -g | |
OPT_THREAD = -std=c++0x -pthread | |
SRC_FILES = $(shell find src/ -type f -name '*.cpp') | |
OBJ_FILES = $(patsubst src/%.cpp, o/%.o, $(SRC_FILES)) | |
## Pour declarer des targets qui ne sont pas des fichiers | |
.PHONY: directories clean install uninstall install-tools uninstall-tools | |
.SILENT: install uninstall | |
all: crypto | |
crypto: $(OBJ_FILES) | |
g++ $(OPT) $(INCLUDES) $(OPT_THREAD) $^ -o $(BIN)$@ $(LIBS) | |
o/%.o: src/%.cpp | |
@mkdir -p "$(@D)" | |
$(CXX) $(OPT) -c $(INCLUDES) $? -o $@ | |
directories: | |
mkdir --parent src/ includes/ o/ bin/ | |
clean: | |
rm -r o/;find . -name "*~" | xargs rm -f | |
install: | |
sudo cp $(BIN)$(PROJECT) $(INSTALL_DIR) | |
if [ ! -d ~/.$(PROJECT)/ ]; then mkdir ~/.$(PROJECT)/; fi | |
chmod 755 ~/.$(PROJECT)/ | |
if [ ! -d /var/log/$(PROJECT)/ ]; then sudo mkdir /var/log/$(PROJECT)/; fi | |
echo "$(PROJECT) installed successfully !" | |
install-tools: | |
sudo apt-get install g++ vim dia dia2code doxygen | |
uninstall: | |
sudo rm -f $(INSTALL_DIR)$(PROJECT) | |
uninstall-tools: | |
sudo apt-get remove g++ vim dia dia2code doxygen | |
rm -r o/ | |
include $(DEP_FILES) |
This file contains hidden or 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
#include "Crypto.h" | |
using namespace Crypto; | |
Vigenere::Vigenere (string key) { | |
this->key = key; | |
reset(); | |
} | |
char Vigenere::encode (const char c) { | |
const char * k = key.c_str(); | |
char encoder = k[this->cursor]; | |
Cesar cesar; | |
return cesar.encode(c, offset(encoder)); | |
} | |
string Vigenere::encode (const char * message) { | |
return compute(message, true); | |
} | |
char Vigenere::decode (const char c) { | |
const char * k = key.c_str(); | |
char encoder = k[this->cursor]; | |
Cesar cesar; | |
return cesar.decode(c, offset(encoder)); | |
} | |
string Vigenere::decode (const char * message) { | |
return compute(message, false); | |
} | |
string Vigenere::compute (const char * message, bool toEncode) { | |
unsigned int max = strlen(message); | |
result.clear(); | |
for(unsigned int i = 0; i < max; i++) { | |
if(!isExcluded(message[i]) && !isNumeric(message[i])) { | |
if(toEncode) | |
result.push_back(encode(message[i])); | |
else | |
result.push_back(decode(message[i])); | |
this->cursor++; | |
this->cursor = this->cursor % key.size(); | |
} else | |
result.push_back(message[i]); | |
} | |
return result; | |
} | |
bool Vigenere::isExcluded (const char c) { | |
char excluded[] = {' ', '!', ',', '.', '\'', '-'}; | |
unsigned int max = strlen(excluded); | |
for(unsigned int i = 0; i < max; i++) { | |
if(c == excluded[i]) | |
return true; | |
} | |
return false; | |
} | |
bool Vigenere::isNumeric (const char c) { | |
if(48 <= c && c <= 57) | |
return true; | |
return false; | |
} | |
void Vigenere::reset() { | |
this->cursor = 0; | |
} | |
unsigned int Vigenere::offset(char c) { | |
return c - (int)'A'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment