TechTricks
Technical answers from the trenches 
 
 
 
 

     
   
ObjectPAL: Implementing Playfair Encryption
 
   
 Posted: 21 August 2000
 
   
 
 Applies to: Paradox 9.0x
 
   
 
Audience: Intermediate
 
       
   

Can you read this?

+Lif,gmdkvUnW[epwh!Wxn?qbcmfq,smjbg(feXwzoq?dkW[UnXwzoq?ojou!q2se
h?qif.qvkio8qg9rjg8bvU4kVhs?ArvXuh8!Aif(GkbzeYkq.At4koh.)ps(Gkbze
Ykq.BrUn[.ucq,kdnfW[Vkojq.Rxhms2g.wYjfjxs2q?dkW[MbvhkYnfjxXukYg[K
jq.Bzxe(GkbzeYks.!Wxn,glxfeojW[U59qe12sYmtu.AkXvhio&gifZutuxeh8?W
!Aif)WxUg8ucq,vtnU8gXue5,gg[UqvhW[Xbq.JYH,[Gvkio9gps?qif)WxUg8dkW
[dk8gudfwmf!Woh,gg[rjg8jebmxanU?qnoq?gjmf,.!Aijq,epwh,gq,tushZwe5
9gps9gxk?.eromols.?qif8gmdo1quesi_UnXwzosjxe.qpvsjef)gU4(Wg8pe,qw
eg8jeojohtu.)jg[gpu!qoi[,\V

To read the above message:

  1. Save the message to a text file. For best results, remove all line breaks before saving the text file.

  2. Download and unzip the PlayFair Sample Code.

  3. Use the instructions found the sample file's README file to open the demo form in Paradox.

  4. Open your saved message file in a text editor (e.g. Notepad), remove the carriage returns and then copy the results to the Clipboard.

  5. Paste the message into the Cipher Text field.

  6. Enter BLACKSTONE as the key and then choose the Encrypt/Decrypt button.

The files included in the downlad use a slightly modified version of the "Playfair Square rules" to encrypt and decrypt messages.

Credit: All work done by Liz A. Woodhouse. Feel free to send comments, suggestions, bug reports to Liz at: law@aros.net. Please note that specific terms, conditions, and disclaimers apply to this sample; these can be found at the end of the README file.

Note: This is strictly for fun. It was done to see if I could do it. However, there may be something useful in the code. I'm also aware that the error trapping code is spotty/inconsistent, and the encryption/decryption routine isn't fully explained, but that's because I have a hard time understanding it myself... Also, no aliases are used, so the tables and form must be in the same directory (feel free to alter the form to use an alias).

Instructions: Enter a key in the Key field (up to 25 characters), enter the text to encrypt in the Plain Text field OR enter the text to decrypt in the Cipher Text field, press the Re-Prep Square button after changing the value in the Key field or moving to a new record in that table. Press the Encode/Decode button to encode plain text or decode cipher text. Do NOT edit the square.

Explanation: The Playfair Square (or Playfair Code) was named after English scientist and Parliamentarian Sir Lyon Playfair, and invented by physicist Charles Wheatstone. The code was used early in World War II. This form uses only a slightly different approach, as follows:

1. The square is a 10x10 grid. It is prepped by placing the characters from the key in the grid starting with the first field in the first row, and skipping additional occurrences of any letter. Once the key is in place, the remainder of the alphabet (case sensitive in my version), the numbers 0 through 9, all other standard US keyboard characters and some extended characters are added to the grid. No character may appear in the grid twice, and the characters must go into the grid in the same order to decrypt the message.

2. The plain text is broken into pairs of letters, and if any pair consists of the same character twice, the pair is split by placing a Z between the two characters in the plain text, if the last character doesn't have a partner, a Z is added to the end of the plain text (you can choose a character other than Z if you wish - if the characters being separated are Zs or the character without a partner is a Z, X is used instead).

3. The rules of encryption are as follows (decryption is done by performing the exact opposite of these steps): For each character in the plain text (including the added Zs), the 'encrypted' version is the character in its own row and its partner's column. If the characters are in the same row, the 'encrypted' version is the character to the right (if the plain text character is the last character in the row, the 'encrypted' version would be the first character in the same row). If the characters are in the same column, the 'encrypted' version is the character below (if the plain text character is the last character in the column, the 'encrypted' version would be the first character in the same column).

The following example (using the original Playfair Square layout of a 5x5 grid, only A-Z allowed, I and J coupled in the grid) is to demonstrate these rules. Our key in this example is BLACKSTAFF and our message is LESTER DUCK LOVES YOU TOO OSCAR (spaces are omitted in the original version).

1. Set up the grid using the key and omitting repeat letters:

     B  L  A  C  K
     S  T  F  D  E
     G  H  IJ M  N
     O  P  Q  R  U
     V  W  X  Y  Z

2. Our message is broken into pairs (note that the pair which would have been OO (using the last O from TOO and the first O from OSCAR is separated with a Z, and that the lone R at the end of the message is paired with a Z):

     LE ST ER DU CK LO VE SY OU TO OZ OS CA RZ

3. Now we will encrypt each pair according to the rules above:

     KT TF DU ER KB BP ZS DV PO SP UV VG KC UY

4. In the original rules, the output is put in groups of five (with additional random characters added at the end to fill out the last group), so we'd end up with:

     KTTFD UERKB BPZSD VPOSP UVVG KCUYZ

5. However, in my version, the output is not grouped at all, replacements are simply put out character by character.

Reminder: PLAYFAIR.ZIP, this information in this article, and all related source code are subject to the specific disclaimers, terms, and conditions outlined in the README.TXT file found in the download. Play Fair!

Related Information

For more information, see Britannica's discussion of cryptography, which mentions Playfair about halfway through the discussion.

 

       

Top

Feedback About Paradox Delphi Assorted Web Stuff
 
 
Copyright © 2000-2004, techtricks.com; All Rights Reserved.
Acknowledgements, Disclaimers, Terms and Conditions.
Article last updated on 31 May 2003

 

Other Sites: Paradox, Delphi, Perl, Web Stuff, and More


 

[- End -]