Version 0 of Cryptography: transposition

Updated 2003-05-05 06:52:12

if 0 {Richard Suchenwirth 2003-05-04 - Reading Fred B. Wrixon's "Codes, cyphers & other...", I wanted to try out some simple encryption techniques in Tcl. To start, here is transposition: rearrange the letters in a fixed-length substring, e.g.

 transpose "world" 53214 -> drowl

The pattern may be any string of non-repeating characters, because each of them will be used as variable name.

Decoding ("detransposition" of) the resulting string goes simply by setting the third (optional) argument to numeric nonzero, e.g. 1.

}

 proc transpose {string pattern {decode 0}} {
    set res ""
    set patlist [split $pattern ""]
    if !$decode {
       set in [lsort $patlist]
       set out $patlist
    } else {
       set in $patlist
       set out [lsort $patlist]
    }
    foreach $in [split $string ""] {
       foreach pat $out {
          append res [set $pat]
       }
    }
    set res
 }

if 0 { Occasionally the end of the message is disturbed, so best add some padding characters.

 % transpose "secret message" 3142
 csre emtseasge
 % transpose [transpose "secret message" 3142] 3142 1
 secret messaeg
 % transpose [transpose "secret messaging" 3142] 3142 1
 secret messaging

Category Cryptography| Arts and crafts of Tcl-Tk programming }