From 44c571bec5074edf23df8144448969ec52273d9c Mon Sep 17 00:00:00 2001 From: Egon Nathan Bittencourt Araujo <enba14@inf.ufpr.br> Date: Thu, 26 Apr 2018 17:36:44 -0300 Subject: [PATCH] cypt and decrypt Signed-off-by: Egon Nathan Bittencourt Araujo <enba14@inf.ufpr.br> --- include/playfair.h | 5 ++++- playfair.cpp | 20 ++++++++------------ playunfair.cpp | 4 +++- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/playfair.h b/include/playfair.h index 63f510b..334336e 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 7a46cae..a83de78 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 72c25f1..6dd3b25 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; } -- GitLab