Task

Implement functions to encrypt and decrypt a message using the straddling checkerboard method. The checkerboard should take a 28 character alphabet (A-Z plus a full stop and an escape character) and two different numbers representing the blanks in the first row. The output will be a series of decimal digits.

Numbers should be encrypted by inserting the escape character before each digit, then including the digit unencrypted. This should be reversed for decryption.




V t = [[String(‘79’), ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’],
       [String(‘’),   ‘H’, ‘O’, ‘L’, ‘’,  ‘M’, ‘E’, ‘S’, ‘’,  ‘R’, ‘T’],
       [String(‘3’),  ‘A’, ‘B’, ‘C’, ‘D’, ‘F’, ‘G’, ‘I’, ‘J’, ‘K’, ‘N’],
       [String(‘7’),  ‘P’, ‘Q’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’, ‘.’, ‘/’]]
 
F straddle(s)
   R multiloop_filtered(Array(s.uppercase()), :t, (c, l) -> c C l, (c, l) -> l[0]‘’:t[0][l.index(c)]).join(‘’)
 
F unstraddle(s)
   [String] r
   V si = 0
   F next()
      R @s[@si++]
   L
      I si == s.len
         L.break
      V c = s[si++]
      I c C (:t[2][0], :t[3][0])
         V i = [:t[2][0], :t[3][0]].index(c)
         V n = :t[2 + i][:t[0].index(next())]
         r [+]= I n == ‘/’ {next()} E n
      E
         r [+]= :t[1][:t[0].index(c)]
   R r
 
V O = ‘One night-it was on the twentieth of March, 1888-I was returning’
print(‘Encoded: ’straddle(O))
print(‘Decoded: ’unstraddle(straddle(O)).join(‘’))