diff --git a/Makefile b/Makefile index 2dfe385ce50a2b410d6dd713d70cd458eb510ec9..7fd62d17c1bfce4b77615ab9fe48a8edbf3dc760 100644 --- a/Makefile +++ b/Makefile @@ -11,17 +11,17 @@ EXEC = playunfair cypher all: $(EXEC) -playunfair: $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ $@.o +playunfair: $(OBJ) playunfair.o + $(CC) $(CFLAGS) -o $@ $^ -cypher: $(OBJ) - $(CC) $(CFLAGS) -o $@ $^ $@.o +cypher: $(OBJ) cypher.o + $(CC) $(CFLAGS) -o $@ $^ %.o: %.cpp $(DEPS) $(CC) $(CFLAGS) -c -o $@ $< $(INCL) clean: - rm -f $(OBJ) $(EXEC) + rm -f *.o $(OBJ) $(EXEC) diff --git a/cypher.cpp b/cypher.cpp index e4eb5e0b4906fe6d476a04cdcfa660d5f0aba182..0cc77ce8c78a0ae9dd2b77427ce569c31666eab7 100644 --- a/cypher.cpp +++ b/cypher.cpp @@ -24,7 +24,6 @@ int main(int argc, char *argv[]){ while(std::getline(input, aux)) text << aux; - //TODO: tirar caracteres especiais crip = text.str(); parser(crip); //std::cout << crip << std::endl; diff --git a/include/playunfair.h b/include/playunfair.h index 3b025ac39cbf8f36d3e4dbc15cb42fd73895e4e7..1365890d2dc5f79792b15087e533a2e967c37420 100644 --- a/include/playunfair.h +++ b/include/playunfair.h @@ -2,5 +2,5 @@ #define ACCEPTED 1 #define REJECTED 0 -int filter(std::string text, const std::string key); +int filter(std::string text); void parser(std::string &text); diff --git a/key.txt b/key.txt index fbcdd3e37b9c05f65ae70085ea9703b48492ae2c..c67fb1bf17ad0c1f8c9a9a18a9959ff76c591aa4 100644 --- a/key.txt +++ b/key.txt @@ -1 +1 @@ -cachorro +casa diff --git a/playunfair.cpp b/playunfair.cpp index efba99abd564d012db43e56d2168b3a288643595..36751f0b26bf9d561939c9740b35869a4c4922c7 100644 --- a/playunfair.cpp +++ b/playunfair.cpp @@ -9,7 +9,7 @@ int main(int argc, char *argv[]){ } std::fstream input, output, dict; char *inputName, *outputName, *dictName; - std::string aux="", key, crip; //a key vai vir do dicionario futuramente + std::string aux="", key, crip; std::ostringstream text, keys; inputName = argv[1]; @@ -25,11 +25,8 @@ int main(int argc, char *argv[]){ while(std::getline(input, aux)) text << aux; - //TODO: tirar caracteres especiais - std::cout<<text.str()[2] << std::endl; crip = text.str(); parser(crip); - //std::cout << crip << std::endl; dict.open(dictName, std::ifstream::in); if(!dict.good()){ @@ -37,20 +34,19 @@ int main(int argc, char *argv[]){ return 0; } - //TODO: if filter returns accepted print key and text in file while(std::getline(dict, aux)){ keys << aux; key = keys.str(); crip = playfair(crip, key, DECRYPT); - std::cout << crip << std::endl; - std::cout << key << std::endl; + if(filter(crip)==ACCEPTED){ + std::cout << crip << std::endl; + std::cout << key << std::endl; + } keys.str(""); keys.clear(); - filter(crip, key); //chamar filtro para cada key } - //crip = playfair(crip, key, DECRYPT); - //std::cout << crip << std::endl; + std::cout << crip << std::endl; return 0; } @@ -59,7 +55,7 @@ void parser(std::string &text){ std::transform(text.begin(), text.end(), text.begin(), ::tolower); } -int filter(std::string text, const std::string key){ +int filter(std::string text){ //TODO: if ( 4 primeiras letras sem vogal || // 3 char -('o') iguais seguidos || // 5 consoantes || @@ -69,6 +65,35 @@ int filter(std::string text, const std::string key){ // // ) // { elimina texto: muda pra proxima chave} + int i, j, rej = REJECTED; + for(int i=0; i<4; ++i){ + if(text[i] == 'a' || text[i] == 'e' || text[i] == 'i' || text[i] == 'o' || + text[i] == 'u'){ + rej = ACCEPTED; + break; + } + } + for(i=0; i<text.size()-3; ++i){ + char l = text[i]; + int lim=3, cont=0; + if ( l=='o') + continue; + for(j=i+1; j<i+lim; ++j){ + if(text[j]=='x'){ + ++lim; + continue; + } else if (text[j] == l){ + ++cont; + continue; + } if (text[j] != l){ + break; + } + } + if(cont == 2 ) { + rej = REJECTED; + break; + } + } - return ACCEPTED; + return rej; }