17
Background
One-hundred and thirteen years ago, amateur cryptographer Félix Delastelle published his work on the four-square cipher, a digraphic substitution cipher that accepts two permutations of a 25-letter alphabet as keys and encodes messages consisting of those letters.
Like most pen-and-paper ciphers, the four-square cipher does not have any cryptographic value today, but at the time of its invention, it had a significant advantage over its monographic counterparts.
One-hundred and thirteen years later, Alice Delastelle decides to improve the four-square cipher by increasing the alphabet's size and the number of keys.[citation needed]
Key setup
The alphabet contains the following characters (starting with space):
!"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ
Given a pass phrase, we construct a permutation of this alphabet as follows:
Keep only the first occurrence of each character.
Append the unused characters from the alphabet, in their natural order.
After turning four pass phrases into four keys, we split each key into a square of side length 7 and arrange the four resulting squares so that they form one big square.
For example, if the pass phrases were
PROGRAMMING PUZZLES & CODE GOLF
POPULARITY CONTESTS & CODE BOWLING
CORRECT HORSE BATTERY STAPLE
DON'T TELL EVE!!!
the keys would be constructed and arranged like this:
PROGAMI POULARI
N UZLES TY CNES
&CDF!"# &DBWG!"
$%'()*+ #$%'()*
,-./:;< +,-./:;
=>?@BHJ <=>?@FH
KQTVWXY JKMQVXZ
CORET H DON'T E
SBAYPL! LV!"#$%
"#$%&'( &()*+,-
)*+,-./ ./:;<=>
:;<=>?@ ?@ABCFG
DFGIJKM HIJKMPQ
NQUVWXZ RSUWXYZ
Encryption
Given a plaintext message such as
ALICE LOVES BOB.
we append 0 or 1 spaces to make its length even and split it into character pairs:
["AL" "IC" "E " "LO" "VE" "S " "BO" "B."]
For each character pair, we locate the the first character in the first square (reading order) and the second in the fourth.
Then, we choose the characters in the remaining squares such that the four selected characters form a rectangle whose sides are parallel to the sides of the squares.
Finally, we replace the character pair by the selected characters of the second and third square.
For our example string, this yields
["PP" "A@" "E " "YT" "ZE" "EH" "=T" "<-"]
resulting in the following ciphertext:
PPA@E YTZEEH=T<-
Task
Write a program or function that accepts four pass phrases and a plaintext, uses the above variant of the four-square cipher to encrypt it and returns the resulting ciphertext.
Details:
The five input strings will consist solely of characters of the mentioned alphabet.
The five input strings can be read in any order, as such, a single string delimited by newlines or as an array of five strings.
You may assume that none of the strings is empty.
The output must be a single string.
If you choose to print the output to STDOUT, you may print only the characters of the ciphertext and (optionally) a trailing newline.
Standard code-golf rules apply.
Test cases
In all test cases, the first four strings correspond to the key squares in reading order and the last input string to the plaintext.
Input
PROGRAMMING PUZZLES & CODE GOLF
POPULARITY CONTESTS & CODE BOWLING
CORRECT HORSE BATTERY STAPLE
DON'T TELL EVE!!!
ALICE LOVES BOB.
Output
PPA@E YTZEEH=T<-
Input
!"#$%&'()*+,-./:;<=>
!"#$%&'()*+,-./:;<=>?@ABCDE
!"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKL
!"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRS
HELLO, WORLD!
Output
LALLR)#TROKE !
Input
,'K AB-Q=?@("W$>XM).C#<I:G!OLP*+;SZJTU%NED/&VFHRY
:,-D$C<SU=IYHFVA;! JG/M&L+WT%#.Q@(N*R")EZOBKX?'>P
L()JX,BND?Z<>P*FU%=O@&KIC+A!Y:;$SWHR.EMG'/T"QV #-
<->CI")AP Q:+U;O/F(KH&ER!MW?X'VJLZ#.$,BSGN@TD%*Y=
SPIN THE PPCG WHEEL OF BLAME!
Output
#>TE,VK+,ZQ(&<F@RA.NL@DM%NAC&>
Input
& %(,-#)$@?/=>'*"<:;!.+
=/$- ?;',@#!(&<")%.:>+*
&%-/<?.;$'#:=!( ,>"+*)@
=,<-*?#.'@/;>%!+:(& ")$
HNRA#JX$H %JAV+"GTS,
Output
GOOD LUCK, HAVE FUN.
Can I steal your input order idea? – Maltysen – 2015-08-01T04:15:52.827
@Maltysen Sure. – orlp – 2015-08-01T04:19:10.277