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. This is only a mild encryption, as the characters of the input text are kept (so frequency statistics of single characters has no problems); best combine it with a substitution, e.g. [Caesar]. } proc transpose {string pattern {decode 0}} { set lpat [string length $pattern] append string [string repeat " " [expr {$lpat-[string length $string]%$lpat}]] set patlist [split $pattern ""] if !$decode { set in [lsort $patlist] set out $patlist } else { set in $patlist set out [lsort $patlist] } set res "" foreach $in [split $string ""] { foreach pat $out { append res [set $pat] } } set res } if 0 { Testing: % transpose "secret message" 3142 csre emtseas g e % transpose [transpose "secret message" 3142] 3142 1 secret message ---- [Category Cryptography]| [Arts and crafts of Tcl-Tk programming] }