diff --git a/algorithms/PlayFairCipher/cpp/PlayFairCipherDecryption.cpp b/algorithms/PlayFairCipher/cpp/PlayFairCipherDecryption.cpp new file mode 100644 index 00000000..6ce601ad --- /dev/null +++ b/algorithms/PlayFairCipher/cpp/PlayFairCipherDecryption.cpp @@ -0,0 +1,282 @@ +//Playfair Cipher +//CODE BY JATIN DHALL +//A playfair cipher has a 5X5 playfair matrix which is used for encryption as well as decryption +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +char playfairMatrix[5][5]; +string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +// Function to remove all spaces from a given string +string removeSpaces(string str) +{ + str.erase(remove(str.begin(), str.end(), ' '), str.end()); + return str; +} + +vector getVectorFromString(string str) +{ + vector res; + vector::iterator it; + for(int i=0;i> pairs) +{ + for(int i=0;i result = getVectorFromString(key); + + //Creating the 5x5 Matrix + for(int i=0;i<5;i++) + { + for(int j=0;j<5;j++) + { + if(count < result.size()) + { + playfairMatrix[i][j] = toupper(result.at(count)); + alphabets.erase(remove(alphabets.begin(), alphabets.end(), toupper(result.at(count))), alphabets.end()); + if(toupper(result.at(count)) == 'I') + { + alphabets.erase(remove(alphabets.begin(), alphabets.end(), toupper('J')), alphabets.end()); + } + else if(toupper(result.at(count)) == 'J') + { + alphabets.erase(remove(alphabets.begin(), alphabets.end(), toupper('I')), alphabets.end()); + } + cout<<"Count : "<> CipherText_To_Pairs(string text) +{ + vector> pairs; + cout<<"The length of the text is : "< IndexInMatrix(char c) +{ + pair res; + for(int i=0;i<5;i++) + { + for(int j=0;j<5;j++) + { + if(playfairMatrix[i][j] == c || c == 'I' && playfairMatrix[i][j] == 'J' || c == 'J' && playfairMatrix[i][j] == 'I') + { + res = make_pair(i,j); + return res; + } + } + } + return res; +} +pair generalCase(pair substring) //When the letters are not in the same row or column etc. +{ + pair coord1,coord2; + pair plainstring; + coord1 = IndexInMatrix(substring.first); + coord2 = IndexInMatrix(substring.second); + + plainstring = make_pair(playfairMatrix[coord1.first][coord2.second],playfairMatrix[coord2.first][coord1.second]); + + return plainstring; +} + +pair sameRow(pair substring) // When the letters are on the same row of the playFair Matrix +{ + pair coord1,coord2; + pair plainstring; + coord1 = IndexInMatrix(substring.first); + coord2 = IndexInMatrix(substring.second); + + int c1,c2,c3,c4; + c1 = coord1.first; + c2 = coord1.second-1; + c3 = coord2.first; + c4 = coord2.second-1; + + if(c2<0) + { + c2 = 4; + } + else if(c4<0) + { + c4 = 4; + } + plainstring = make_pair(playfairMatrix[c1][c2],playfairMatrix[c3][c4]); + + return plainstring; +} + +pair sameColumn(pair substring) // When the letters are on the same column of the playFair Matrix +{ + pair coord1,coord2; + pair plainstring; + coord1 = IndexInMatrix(substring.first); + coord2 = IndexInMatrix(substring.second); + + int c1,c2,c3,c4; + c1 = coord1.first-1; + c2 = coord1.second; + c3 = coord2.first-1; + c4 = coord2.second; + + if(c1<0) + { + c1 = 4; + } + else if(c3<0) + { + c3 = 4; + } + + plainstring = make_pair(playfairMatrix[c1][c2],playfairMatrix[c3][c4]); + return plainstring; +} + +string PlayFairDecode(vector> result) +{ + string decodedString = ""; + pair coord1,coord2; + pair substring,decodedPair; + + cout<<"The Encoded Pairs are :-"<> result = CipherText_To_Pairs(text); + + string decodedString = PlayFairDecode(result); + cout<<"The Decoded String is : "< +#include +#include +#include +#include +#include +#include + +using namespace std; + +char playfairMatrix[5][5]; +string alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +// Function to remove all spaces from a given string +string removeSpaces(string str) +{ + str.erase(remove(str.begin(), str.end(), ' '), str.end()); + return str; +} + +vector getVectorFromString(string str) +{ + vector res; + vector::iterator it; + for(int i=0;i> pairs) +{ + for(int i=0;i result = getVectorFromString(key); + + //Creating the 5x5 Matrix + for(int i=0;i<5;i++) + { + for(int j=0;j<5;j++) + { + if(count < result.size()) + { + playfairMatrix[i][j] = toupper(result.at(count)); + alphabets.erase(remove(alphabets.begin(), alphabets.end(), toupper(result.at(count))), alphabets.end()); + if(toupper(result.at(count)) == 'I') + { + alphabets.erase(remove(alphabets.begin(), alphabets.end(), toupper('J')), alphabets.end()); + } + else if(toupper(result.at(count)) == 'J') + { + alphabets.erase(remove(alphabets.begin(), alphabets.end(), toupper('I')), alphabets.end()); + } + cout<<"Count : "<> PlainText_To_Pairs(string text) +{ + vector> pairs; + cout<<"The length of the text is : "< IndexInMatrix(char c) +{ + pair res; + for(int i=0;i<5;i++) + { + for(int j=0;j<5;j++) + { + if(playfairMatrix[i][j] == c || c == 'I' && playfairMatrix[i][j] == 'J' || c == 'J' && playfairMatrix[i][j] == 'I') + { + res = make_pair(i,j); + return res; + } + } + } + return res; +} +pair generalCase(pair substring) //When the letters are not in the same row or column etc. +{ + pair coord1,coord2; + pair cipherstring; + coord1 = IndexInMatrix(substring.first); + coord2 = IndexInMatrix(substring.second); + + cipherstring = make_pair(playfairMatrix[coord1.first][coord2.second],playfairMatrix[coord2.first][coord1.second]); + + return cipherstring; +} + +pair sameRow(pair substring) // When the letters are on the same row of the playFair Matrix +{ + pair coord1,coord2; + pair cipherstring; + coord1 = IndexInMatrix(substring.first); + coord2 = IndexInMatrix(substring.second); + + int c1,c2,c3,c4; + c1 = coord1.first; + c2 = coord1.second+1; + c3 = coord2.first; + c4 = coord2.second+1; + + if(c2>=5) + { + c2 = c2%5; + } + else if(c4>=5) + { + c4 = c4%5; + } + cipherstring = make_pair(playfairMatrix[c1][c2],playfairMatrix[c3][c4]); + + return cipherstring; +} + +pair sameColumn(pair substring) // When the letters are on the same column of the playFair Matrix +{ + pair coord1,coord2; + pair cipherstring; + coord1 = IndexInMatrix(substring.first); + coord2 = IndexInMatrix(substring.second); + + int c1,c2,c3,c4; + c1 = coord1.first+1; + c2 = coord1.second; + c3 = coord2.first+1; + c4 = coord2.second; + + if(c1>=5) + { + c1 = c1%5; + } + else if(c3>=5) + { + c3 = c3%5; + } + + cipherstring = make_pair(playfairMatrix[c1][c2],playfairMatrix[c3][c4]); + return cipherstring; +} + +string PlayFairEncode(vector> result) +{ + string encodedString = ""; + pair coord1,coord2; + pair substring,encodedPair; + + cout<<"The Encoded Pairs are :-"<> result = PlainText_To_Pairs(text); + + string encodedString = PlayFairEncode(result); + cout<<"The Encoded String is : "<