Bifid cipher: Difference between revisions

J: move normalization out of the encrypt/decrypt routines and show an example with a 9x9 reference "square"
(J: move normalization out of the encrypt/decrypt routines and show an example with a 9x9 reference "square")
Line 64:
Implementation:<lang J>alpha=: a.{~65+i.26
normalize=: {{ rplc&'JI'(toupper y)([-.-.)alpha }}
bifid=: {{ m{~_2 (5t&#.)\,|:5 5(t,t=.%:#m)#:m i.normalize y([-.-.)m }}
difib=: {{ m{~5t#.|:(|.@$$,)5 5(t,t=.%:#m)#:m i.normalize y([-.-.)m }}</lang>
 
This is pretty much a literal implementation of the algorithm, except that our indices range from 0..4 instead of 1..5 (or, for letter indices, they range from 0..24 instead of 1..25).
Line 75:
ref3=: 'PLAYFIREXMBCDGHKNOQSTUVWZ'
 
ref1 bifid normalize 'attack at dawn'
DQBDAXDQPDQH
ref1 difib ref1 bifid normalize 'attack at dawn'
ATTACKATDAWN
ref2 bifid normalize 'flee at once'
UAEOLWRINS
ref2 difib ref2 bifid normalize 'flee at once'
FLEEATONCE
ref2 bifid normalize 'attack at dawn'
EYFENGIWDILA
ref2 difib ref2 bifid normalize 'attack at dawn'
ATTACKATDAWN
ref3 bifid normalize 'The invasion will start on the first of January'
VRSYXSIYTMQVIRSKISLPVLDTCKRTCAIVTMATCEX
ref3 difib ref3 bifid normalize 'The invasion will start on the first of January'
THEINVASIONWILLSTARTONTHEFIRSTOFIANUARY</lang>
(_81{.123{.a.)bifid 'The invasion will start on the first of January'
 
TgqhpqpqxzpxfqzoKqhxw/O3eWH53BYw`+Be8F1
We could include both I and J by increasing the square size to 64: take all ascii characters in the range from 32 (space) to 126 (tilde). Discard the lower case letters. Discard four other characters. Use the remainder for the square.
(_81{.123{.a.)difib(_81{.123{.a.)bifid 'The invasion will start on the first of January'
TheinvasionwillstartonthefirstofJanuary</lang>
 
=={{header|Julia}}==
6,951

edits