diff --git a/.gitignore b/.gitignore index 838aa8eded147d0efa9f6f65ec5b2386b63a6534..4d90da4e5050c8556186f981186d08439ed83fec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ *.o +*.swp +*.swo playunfair *.out diff --git a/playfair.cpp b/playfair.cpp index 87e18fabbe6127c5630349f7b7acd8e42002ac78..7a46cae9d61429228a302e5e4d3787f895a90cb0 100644 --- a/playfair.cpp +++ b/playfair.cpp @@ -2,12 +2,13 @@ #define _charbit(X) (1 << (X - 'a')) +#define _pos(I,J) ((I)*5 + (J)%5) //Base idea is to use an 32-bit int as mask to detect which char // is already on the matrix std::vector< char > createKM ( const std::string key){ std::vector< char > mKey(25); - std::string abc(key + "abcdefghijklmnopqrstuvxwyz"); + std::string abc(key + "abcdefghijklmnopqrstuvwxyz"); int mask =0; //mask with 32 bits for the 25 letter alfabet int si=0; @@ -30,22 +31,61 @@ std::vector< char > createKM ( const std::string key){ std::string playfair(std::string text, const std::string key){ std::ostringstream ret; std::vector< char > mKey(createKM(key)); - for (int i=0; i<5; ++i){ + /*for (int i=0; i<5; ++i){ for (int j=0; j<5; ++j){ std::cout << mKey[i*5 + j] << ' '; } std::cout << std::endl; - } - int indexT = -1; - char fst = text[++indexT]; - char snd = text[++indexT]; - if (fst == snd && snd != 'x'){ - snd = 'x'; - --indexT; - } + }*/ + unsigned int indexT = 0; + int repetitions=0; + while(indexT <text.size() ){ + char fst = text[indexT]; + char snd = (++indexT< text.size())?text[indexT]:'x'; + std::cout << fst << " " << snd << std::endl; + ++indexT; + if (fst == snd){ + if(snd!='x'){ + snd = 'x'; + }else{ + snd='q'; + } + --indexT; + ++repetitions; + } - int iFst, iSnd; + int iFst, iSnd; + for (int i=0; i<25; ++i){ + if(mKey[i] == fst || + ((fst == 'i' || fst=='j') && + (mKey[i] =='i' || mKey[i]=='j'))){ + iFst=i; + } + if(mKey[i] == snd || + ((snd == 'i' || snd =='j') && + (mKey[i] =='i' || mKey[i]=='j'))){ + iSnd=i; + } + } + + char retfst,retsnd; + + if(iFst/5 == iSnd/5){ + retfst= mKey[_pos( iFst/5, (iFst+1))]; + retsnd= mKey[_pos( iSnd/5 ,(iSnd+1))]; + }else if(iFst%5 == iSnd%5){ + std::cout << iFst << " " << iFst/5 << " " << ((iFst/5)+1)%5 << std::endl; + retfst= mKey[_pos( ((iFst/5)+1)%5 , (iFst))]; + std::cout << retfst << " " << _pos( ((iFst/5)+1)%5 , (iFst)) << std::endl; + retsnd= mKey[_pos( ((iSnd/5)+1)%5 , (iSnd))]; + }else{ + retfst = mKey[ _pos( iFst/5, iSnd)]; + retsnd = mKey[_pos ( iSnd/5, iFst)]; + } + ret << retfst << retsnd; + } + std::cout << ret.str() << std::endl; //TODO:change - return text; + return ret.str(); } diff --git a/playunfair.cpp b/playunfair.cpp index c68172769b35b637f5b2d43550eb882a797a22b3..b318631ef2f37d7d0e420831a2ffb011bd18be01 100644 --- a/playunfair.cpp +++ b/playunfair.cpp @@ -13,11 +13,13 @@ int main(int argc, char *argv[]){ } std::fstream input, output; char *inputName, *outputName; - std::string aux, key = "cachorro", crip; //a key vai vir do dicionario futuramente + std::string aux="", key = "cachorro", crip; //a key vai vir do dicionario futuramente std::ostringstream text; inputName = argv[1]; outputName = argv[2]; + + //TODO: ELE TA LENDO CAGADO JA, olha o fim desse comment input.open(inputName, std::ifstream::in); if(!input.good()){ std::cout << "Nao foi possivel abrir o arquivo de entrada" << std::endl; @@ -27,9 +29,13 @@ int main(int argc, char *argv[]){ while(std::getline(input, aux)) text << aux; + std::cout<<text.str()[0] << std::endl; + //TODO: ERRO ACABA AQUI, veja o resultado disso aqui^ com o teste3.in + return 0; crip = text.str(); parser(crip); std::cout << crip << std::endl; crip = playfair(crip, key); + std::cout << crip << std::endl; return 0; } diff --git a/teste3.in b/teste3.in new file mode 100644 index 0000000000000000000000000000000000000000..9a75b6816eb3cf02889f99a588cd418c9fdb5923 --- /dev/null +++ b/teste3.in @@ -0,0 +1 @@ +youshallnot pas