# Fun With Grille Cyphers

A Grille Cypher is a 500-year old method for encrypting a text message. It involves writing the message onto a sheet of paper through holes cut in a top sheet.

Imagine a piece of paper on which you have drawn a 10 x 10 grid of squares. Now take another piece of paper with the same grid, and cut out 10 random squares to make a grille.

Now lay the grille on top of the first sheet, and write your message through the holes. (The message must be 10 characters long).

Finally, remove the grille and fill in all the remaining squares with random letters. The result is an encrypted version of the original message.

To decode the message you simply lay the grille over the encrypted message to mask the random letters and reveal the original text.

Let's see how a grille cypher could be implemented in APL. The first thing we need is a top-secret message to send:

```      message←'top secret'
⍴message
10```

Next we need to make a random grille, which we can represent as a 10 x 10 boolean array with a '1' in each place where there's a hole cut in the grille. One way to do this is as follows:

```      ⎕io←1
grille←(⍴message)≥10 10⍴100?100
grille
0 0 1 0 1 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
1 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
+/,grille
10```

Now we need to create the encrypted message. First we create a 10 x 10 square of random letters:

```      encrypted←⎕a[?(⍴grille)⍴26]
encrypted
bpjzevgxob
mzsdvmafbk
aczlpxiaup
fkgmfjqzpg
lrfhyajmdr
zefdevbhqp
jyjmdhnpwh
yawfxgygxd
jnakihpvyy```

Next we insert the secret message using the grille positions:

```      ((,grille)/,encrypted)←message
encrypted
bptzovgxob
mzsdp afbk
sczlpxeaup
fcgmfjqzpg
lrfhyajmde
zefdevbhqp
jyjmdhnpwt
yawfxgygxd
jnakihpvyy```

We can see the secret message peeping through by using the grille to mask the message:

```      decoded←encrypted
((~,grille)/,decoded)←' '
decoded
t o
p
s     e
c
r       e

t```

Finally we can recover the message:

```      (,grille)/,encrypted
top secret```

Author: SimonMarsden

Studio/GrilleCyphers (last edited 2009-02-08 21:50:23 by SimonMarsden)