Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Impliments the classical cipher "Playfair"
- // Usage: cat key.txt message.txt | playfair message -[e/d]
- // The decrypted message will be missing spaces and have an x
- // in place of every double letter.
- //////////////////////////////////////////////////////////////*/
- #include<iostream>
- #include<vector>
- #include<string.h>
- using namespace std;
- char mat[5][5];
- void createMatrix(char key[]);
- void prepareText(char origmessage[], char message[]);
- void encipher(char message[]);
- void decipher(char message[]);
- int main(int argc, char** argv)
- {
- char message[1000];
- char origmessage[1000];
- char key[25];
- if (argc != 2)
- {
- cout << "Incorrect number of command line arguments, " << endl
- << "Usage: cat key.txt message.txt | playfair message -[e/d] " << endl;
- return false;
- }
- cin >> key;
- cin >> origmessage;
- // function creates the matrix
- createMatrix(key);
- // encipher/decipher flag
- switch (argv[1][1]) {
- case 'e':
- // removes spaces and punctuation, checks for pairs of double letters
- prepareText(origmessage, message);
- // turns plaintext into ciphertext
- encipher(message);
- case 'd':
- // turns ciphertext into plaintext
- decipher(message);
- }
- return 0;
- }
- void createMatrix(char key[])
- {
- int i = 0, j = 0, k = 0;
- int letter = 'a';
- bool ok = true;
- char used[25];
- // keep going until the key runs out
- while(key[k] != '0')
- {
- // we throw out the letter j
- if(key[k] == 'j')
- key[k] = 'i';
- // end of row
- if (j > 4)
- {
- j = 0;
- i++;
- }
- // check to see if the letter is a duplicate
- for(int check = 0; check < k; check++)
- {
- if(used[check]==key[k])
- ok = false;
- }
- // If not a duplicate add it to the matrix
- if(ok)
- {
- cout << mat[i][j];
- mat[i][j] = key[k];
- }
- j++;
- k++;
- }
- // Add remaining letters of the alphabet to the matrix
- for(int remain = k; remain < 25; remain ++)
- {
- // we throw out the letter j
- if(letter == 'j')
- letter++;
- // end of row in matrix
- if (j > 4)
- {
- j = 0;
- i++;
- }
- // check to see if the letter has been used
- for(int check = 0; check < k; check ++)
- {
- if(used[check] == letter)
- ok = false;
- }
- // if letter hasn't been used add it to the matrix
- if(ok)
- {
- mat[i][j] = letter;
- letter++;
- }
- }
- // print out the matrix to test it
- for(int m=0; m < 5; m++)
- for(int n = 0; n < 5; n++)
- {
- cout << mat[m][n] << " ";
- }
- }
- // remove spaces and punctuation, checks for pairs of double letters
- void prepareText(char origmessage[], char message[])
- {
- int stepO = 0;
- int stepM = 0;
- //transform(origmessage.begin(), origmessage.end(), origmessage.begin(), ::tolower);
- while(!origmessage[stepO])
- {
- if(origmessage[stepO] >= 'a' && origmessage[stepO] <= 'z')
- {
- if(stepM%2 == 1)
- {
- if(origmessage[stepO] == origmessage[stepO-1])
- {
- message[stepM] = 'x';
- }
- else
- {
- message[stepM] = origmessage[stepO];
- stepO++;
- }
- }
- else
- {
- message[stepM] = origmessage[stepO];
- stepO++;
- }
- }
- stepM++;
- }
- }
- // this turns our plaintext into cipher text
- void encipher(char message[])
- {
- int x1, x2, y1, y2, j, k;
- // the first 50 characters of the plaintext
- for(int i =0; i < 50; i++)
- {
- j = 0;
- k = 0;
- // find the 1st character from the message in the matrix
- while(message[i] != mat[j][k] && k < 5)
- {
- j++;
- if(j>4)
- {
- j=0;
- k++;
- }
- }
- x1 = j; // row number
- y1 = k; // column number
- j=0;
- k=0;
- // find the 2nd character from the message in the matrix
- while(message[i+1] == mat[j][k])
- {
- j++;
- if(j>4)
- {
- j=0;
- k++;
- }
- }
- x2 = j; // row number
- y2 = k; // column number
- // if they are not in the same row or column
- if(x1!=x2 && y1!=y2)
- {
- message[i] = mat[x1][y2];
- message[i+1] = mat[x2][y1];
- }
- // if they in the same row
- else if(y1 != y2)
- {
- if(x1 == 4)
- message[i] = mat[0][y1];
- else
- message[i] = mat[x1+1][y1];
- if(x2 == 4)
- message[i+1] = mat[0][y2];
- else
- message[i+1] = mat[x2+1][y2];
- }
- // if they are in the same column
- else
- {
- if(y1 == 4)
- message[i] = mat[x1][0];
- else
- message[i] = mat[x1][y1+1];
- if(y2 == 4)
- message[i+1] = mat[x2][0];
- else
- message[i+1] = mat[x2][y2+1];
- }
- i++;
- }
- }
- void decipher(char message[])
- {
- int x1, x2, y1, y2, j, k;
- // the first 50 characters of the plaintext
- for(int i =0; i < 50; i++)
- {
- j = 0;
- k = 0;
- // find the 1st character from the message in the matrix
- while(message[i] != mat[j][k] && k < 5)
- {
- j++;
- if(j>4)
- {
- j=0;
- k++;
- }
- }
- x1 = j; // row number
- y1 = k; // column number
- j=0;
- k=0;
- // find the 2nd character from the message in the matrix
- while(message[i+1] == mat[j][k])
- {
- j++;
- if(j>4)
- {
- j=0;
- k++;
- }
- }
- x2 = j; // row number
- y2 = k; // column number
- // if they are not in the same row or column
- if(x1!=x2 && y1!=y2)
- {
- message[i] = mat[x1][y2];
- message[i+1] = mat[x2][y1];
- }
- // if they in the same row
- else if(y1 != y2)
- {
- if(x1 == 0)
- message[i] = mat[4][y1];
- else
- message[i] = mat[x1-1][y1];
- if(x2 == 0)
- message[i+1] = mat[4][y2];
- else
- message[i+1] = mat[x2-1][y2];
- }
- // if they are in the same column
- else
- {
- if(y1 == 0)
- message[i] = mat[x1][4];
- else
- message[i] = mat[x1][y1-1];
- if(y2 == 0)
- message[i+1] = mat[x2][4];
- else
- message[i+1] = mat[x2][y2-1];
- }
- i++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement