The playfair cipher uses a 5 by 5 key table generated by first filling with letters of the keyword (dropping any duplicate letters), then filling the remaining spaces with the rest of the letters of the alphabet in order (omitting Q). To encrypt a message, one would break the message into digraphs (groups of 2 letters) and map them out on the key table:
If both letters are the same (or only one letter is left), add an "X" after the first letter.
If the letters apear on the same row of you table, replace them with the letters to their immediate right respectively (wrapping around to the left side of the row ifa aletter in the original pair was on the right side of the row).
If the letters appear on the same column of your table, replace them with the letters immediately below respectively (wrapping around to the top side of the column if a letter in the original pair was on the bottom side of the column).
If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. The encrypted letters lie on the same rows as the plaintext letters.
For example, the table for the keyword "Playfair Example is
P L A Y F
I R E X M
B C D G H
J K N Q S
T U V W Z
The message "Hide the gold in the tree stump" has digraphs HI DE TH EG OL DI NT HE TR EX ES TU MP and is encrypted to "BMNDZBXDKYBEJVDMUIXMMNUVIF".
Write a MATLAB function cipertext = project7(plaintext, k) that encrypts the plaintext using the playfair cipher with k as the keyword. The alphabet is the 26 Capital letters. A sample run of the function is shown below.
>>plain = 'HIDETHEGOLDINTHETREESTUMP';
>>key = 'PLAYFAIREXAMPLE';
>>project7 (plain, key)
0 · ·