Skip to content
Snippets Groups Projects
Commit 015b867d authored by Egon Nathan Bittencourt Araujo's avatar Egon Nathan Bittencourt Araujo
Browse files

playfair pela metade

parent 32984711
No related branches found
No related tags found
No related merge requests found
*.o
playunfair
*.out
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
#include <vector> #include <vector>
#include <iterator> #include <iterator>
std::vector< std::vector<char> > createKM ( const std::string key); 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 );
...@@ -3,9 +3,10 @@ ...@@ -3,9 +3,10 @@
#define _charbit(X) (1 << (X - 'a')) #define _charbit(X) (1 << (X - 'a'))
//Base idea is to use an 32-bit int as mask to detect which char
std::vector< std::vector<char> > createKM ( const std::string key){ // is already on the matrix
std::vector< std::vector<char> > mKey(5, std::vector<char>(5)); std::vector< char > createKM ( const std::string key){
std::vector< char > mKey(25);
std::string abc(key + "abcdefghijklmnopqrstuvxwyz"); std::string abc(key + "abcdefghijklmnopqrstuvxwyz");
int mask =0; int mask =0;
//mask with 32 bits for the 25 letter alfabet //mask with 32 bits for the 25 letter alfabet
...@@ -20,21 +21,42 @@ std::vector< std::vector<char> > createKM ( const std::string key){ ...@@ -20,21 +21,42 @@ std::vector< std::vector<char> > createKM ( const std::string key){
mask = mask | (_charbit('j') | _charbit('i')); mask = mask | (_charbit('j') | _charbit('i'));
} }
mKey[i][j] = abc[si]; mKey[i*5 + j] = abc[si];
} }
} }
return mKey; return mKey;
} }
std::string playfair(std::string text, const std::string key){ std::string playfair(std::string text, const std::string key){
std::vector< std::vector <char> > mKey(createKM(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){ for (int j=0; j<5; ++j){
std::cout << mKey[i][j] << ' '; std::cout << mKey[i*5 + j] << ' ';
} }
std::cout << std::endl; std::cout << std::endl;
} }
int indexT = -1;
char fst = text[++indexT];
char snd = text[++indexT];
if (fst == snd && snd != 'x'){
snd = 'x';
--indexT;
}
int iFst, iSnd;
for (int i=0; i<25; ++i){
if(mKey[i] == fst){
iFst=i;
}
if(mKey[i] == snd){
iSnd=i;
}
}
char retfst,retsnd;
retfst = mKey[iFst/5 + iSnd%5]
//TODO:change //TODO:change
return text; return text;
......
File deleted
File deleted
File deleted
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment