Fifty Shades of J/Chapter 40

From J Wiki
Jump to navigation Jump to search

Table of Contents ... Glossary ... Previous Chapter ... Next Chapter

The I-spy book of J part 2

Principal Topics

|. (shift), , (append) ,. (stitch) A. (anagram index), ^: (power conjunction), a. (alphabet) /: (grade up) encryption, decryption, keywords, Vigenère table

Dear J-ohn and J-anet,

Now that you know a little bit about encryption and decryption, and understand the difference between public and private keys I would like to introduce you to an even simpler code which was invented by a sixteenth century French diplomat called Vigenère. The idea of a Vigenère table is that people sharing the code share a table of alphabetic characters, and for the purposes of this demonstration we will use an alphabet of just 6 characters ‘ABCDES’. To make a Vigenère table choose an anagram of these 6 characters

   ]tkey=.(6?6){'ABCDES'        NB. your tkey value will vary 
AECSDB

(One way to create such an anagram which makes memorization easier is to choose a keyword, say ‘BED’, which leads to a tkey= 'BEDACS' in which the characters not in the keyword are appended in alphabetical order.) However it is formed, tkey forms the row and column headers for a table in which the first row is the tkey reversed, and the remaining rows are shifted progressively to the left :

   tkey=.'BEDACS'
   makevtab=:(1&|.)~i.@#
   (' ',tkey),.'|',.tkey,makevtab |.tkey
 |BEDACS
B|SCADEB
E|CADEBS
D|ADEBSC
A|DEBSCA
C|EBSCAD
S|BSCADE

This table is used for both encryption and decryption. Entering EC into the table as an example yields the letter B. Messages are converted into character pairs by the parties agreeing a message keyword. This is written by expanding it as often as necessary beneath the message, so that if the message is “Abe’s Dad’s a cad”, and the keyword is ‘SAD’ write

   ABESDADSACAD
   SADSADSADSAD

Using the columns as indices to the Vigenère table gives the coded message

   ADDEBBCABDSE

Now repeat this procedure for the coded message

   ADDEBBCABDSE
   SADSADSADSAD

… and back comes the original message

   ABESDADSACAD

Now we will go through all this in your favourite programming language

   tkey=.'AECSDB'
   ]vtab=:makevtab |.tkey
BDSCEA
DSCEAB
SCEABD
CEABDS
EABDSC
ABDSCE
   msg=.'ABESDADSACAD'
   msg,:'SAD'($~#)msg
ABESDADSACAD
SADSADSADSAD
   encrypt=:dyad :'(<"1 tkey i.y,.(#y)$x){vtab'
   'SAD' encrypt msg
CAABEEDCEABS
   'SAD' encrypt^:2 msg
ABESDADSACAD

For reassurance, try it with another keyword and another message :

   'BEADS' encrypt 'SEASBAD'
SSBDSAA
   'BEADS' encrypt^:2 'SEASBAD'
SEASBAD

Moving up to a full alphabet define alph, reassigning tkey and vtab and reexecuting (but not redefining) encrypt.

   alph=:' ',(65+i.26){a.
   tkey=.(27?27){alph
   vtab=:makevtab |.tkey=.525 A. alph
   encrypt=:dyad :'(<"1 tkey i.y,.(#y)$x){vtab'
   'VECTOR' encrypt 'QUICK A LAZY DOG'
NVNC HCYKEONDQHU
   'VECTOR' encrypt^:2 'QUICK A LAZY DOG'
QUICK A LAZY DOG  

A refinement which you might like if you are to use a full alphabetic code is to have rows and columns of the Vigenère table in alphabetical order. To illustrate how to construct such a visual table, we revert to the 6-alphabet case

   ]tkey=.(6?6){'ABCDES'    NB. your tkey value will vary
SCADEB
   order=.({~/:)&tkey
   ]ok=.order tkey     NB. but you ok value will be this
ABCDES
   (' ',tkey),.'|',.tkey,makevtab |.tkey
 |SCADEB
S|BEDACS
C|EDACSB
A|DACSBE
D|ACSBED
E|CSBEDA
B|SBEDAC
   ' ',ok),.'|',.ok,(order&|:@order) vtab
 |ABCDES
A|CEASBD
B|ECBDAS
C|ABDCSE
D|SDCBEA
E|BASEDC
S|DSEACB

from which it is easy to confirm that these two tables are equivalent.

A Vigenère cipher can be made more secure by performing a final alphabetic shift using a further key called shift key. The price of this extra security is that the encryption algorithm is no longer reciprocal. However inverting the shift is simply a matter of applying minus to the shift key.

   alph=:' ',(65+i.26){a.
   tkey=.(27?27){alph
   vtab=:makevtab |.tkey=.525 A. alph
   encrypt=:dyad :'(<"1 tkey i.y,.(#y)$x){vtab'
   shift=:dyad : '((#alph)|x+alph i.y){alph'
   skey=.3
   ]x=.skey shift 'SAD' encrypt 'QUICK A LAZY DOG'
TXPGQYI MIEDJAJC
   'SAD' encrypt (-skey)shift x
QUICK A LAZY DOG

Code Summary

makevtab=: (1&|.)~i.@#                           NB. make Vigenere table
vtab=: makevtab |.tkey                           NB. vtab is a Vig.table
alph=: ' ',(65+i.26){a.                          NB. alphabet
tkey=: (27?27){alph                              NB. anagram of alph
encrypt=: dyad :'(<"1 tkey i.y,.(#y)$x){vtab'
order=: ({~/:)&tkey                              NB. rows in alpha order
shift=: dyad : '((#alph)|x+alph i.y){alph'       NB. for extra security

Script

File:Fsojc40.ijs