2023-02-09      Columnar transposition

 A columnar transposition is simple transposition cypher which can be
 en-/decrypted by  hand and  was widely used  during wartime.  If the
 cyphertext is long (~200 characters) und the cypher is applied twice
 with different keys this encryption method is rather hard to break.

 Quoting Wikipedia[1]:  In a  columnar transposition, the  message is
 written  out in  rows of  a fixed  length, and  then read  out again
 column  by column,  and the  columns  are chosen  in some  scrambled
 order. Both the width of the rows and the permutation of the columns
 are usually defined by a  keyword. For example, the keyword UNCLETED
 is of length 8 (so the rows are of length 8), and the permutation is
 defined by the alphabetical order of  the letters in the keyword. In
 this case, the order would be "8, 6, 1, 5, 3, 7, 4, 2".

 And  now my  simple elisp  implementation using  the thread-operator
 macro[2] (borrowed from Clojure).

   (require 'dash)
   (defun encrypt-columnar-transposition (input-str passphrase)
     (let ((key (-grade-down
                 (lambda (a b) (string> a b))
                 (split-string (upcase passphrase) "" t)))
           (my/split (lambda (x) (split-string x "" t))))
       (->> (upcase input-str)
            (replace-regexp-in-string "\\([ \t\r\n]+\\)" "")
            (funcall my/split)
            (-partition-all (length key))
            (apply #'-pad "E"))))
            (-unzip)
            (-select-by-indices key)
            (-map #'string-join)
            (string-join))))

   (encrypt-columnar-transposition
    "The Industrial Revolution and its consequences have been a
     disaster for the human race"
    "uncleted")

 => "EIUIQADFMESVANEETECENLISEESRNEUENOCESHAEIATTUVIOAEHRLDEHARUEDROCNBATRETTONSSNEHE"

 Nice.

Encryption by hand
~~~~~~~~~~~~~~~~~~

 THEINDUSTRIALREVOLUTIONANDITSCONSEQUENCESHAVEBEENADISASTERFORTHEHUMANRACE

 UNCLETED <--- keyword

 86153742 <--- alphabetical order of the keyword
 ||||||||
 vvvvvvvv

 THEINDUS
 TRIALREV
 OLUTIONA
 NDITSCON
 SEQUENCE
 SHAVEBEE
 NADISAST
 ERFORTHE
 HUMANRAC
 EEEEEEEE <--- last line padded with E (most common character)

 C -> EIUIQADFMS
 D -> VANEETECNL
 E -> ISEESRNUEN
 E -> OCESHAIATT
 L -> UVIOAHRLDE
 N -> HARUDROCNB
 T -> ATRTTONSSN
 U -> EHE

 CIPHERTEXT:

 EIUIQADFMESVANEETECENLISEESRNEUENOCESHAEIATTUVIOAEHRLDEHARUEDROCNBATRETTONSSNEHE

Footnotes
~~~~~~~~~

[1] https://en.wikipedia.org/wiki/Transposition_cipher#Columnar_transposition
[2] https://github.com/magnars/dash.el#threading-macros-1