diff --git a/include/playfair.h b/include/playfair.h index 63f510b3fed3de86e148a9e486b1772bd1243fd8..334336ebdddebc19d24ff48a3d4f1cd330cace63 100644 --- a/include/playfair.h +++ b/include/playfair.h @@ -5,5 +5,8 @@ #include <sstream> #include <algorithm> +#define CRYPT 1 +#define DECRYPT -1 + std::vector< char > createKM ( const std::string key); -std::string playfair(std::string text , const std::string key ); +std::string playfair(std::string text , const std::string key, int crypt); diff --git a/playfair.cpp b/playfair.cpp index 7a46cae9d61429228a302e5e4d3787f895a90cb0..a83de78a43fdb0e34502bb7835466238b73a0e61 100644 --- a/playfair.cpp +++ b/playfair.cpp @@ -28,21 +28,21 @@ std::vector< char > createKM ( const std::string key){ return mKey; } -std::string playfair(std::string text, const std::string key){ +std::string playfair(std::string text, const std::string key, int crypt){ + crypt=crypt+5;//Avoid operator % on negative numbers 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; - }*/ + } 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'){ @@ -72,20 +72,16 @@ std::string playfair(std::string text, const std::string key){ char retfst,retsnd; if(iFst/5 == iSnd/5){ - retfst= mKey[_pos( iFst/5, (iFst+1))]; - retsnd= mKey[_pos( iSnd/5 ,(iSnd+1))]; + retfst= mKey[_pos( iFst/5, (iFst+crypt))]; + retsnd= mKey[_pos( iSnd/5 ,(iSnd+crypt))]; }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))]; + retfst= mKey[_pos( ((iFst/5)+crypt)%5 , (iFst))]; + retsnd= mKey[_pos( ((iSnd/5)+crypt)%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 ret.str(); } diff --git a/playunfair.cpp b/playunfair.cpp index 72c25f1343d47e93addc91e4e7ef450c4c885edb..6dd3b2597ed3a5fdb2b34a126396a636074c16aa 100644 --- a/playunfair.cpp +++ b/playunfair.cpp @@ -32,7 +32,9 @@ int main(int argc, char *argv[]){ crip = text.str(); parser(crip); std::cout << crip << std::endl; - crip = playfair(crip, key); + crip = playfair(crip, key, CRYPT); + std::cout << crip << std::endl; + crip = playfair(crip, key, DECRYPT); std::cout << crip << std::endl; return 0; }