Version 13 of rot13

Updated 2002-12-10 12:56:07

This is an encryption algorithm commonly used for hiding information in usenet posts that is open to all but not immediately readable. An example is posting about film endings, or example answers.

The algorithm is actually a caesar cipher with a 13 place rotation. This number is special because in an alphabet with 26 letters, caesar 13 and caesar -13 use the same map, so the same call can be used for encoding and decoding. (RS)

Using tr from Example scripts everybody should have, a rot13 cipher can be written as easily as

 puts [tr A-Za-z N-ZA-Mn-za-m {Guvf vf n grfg}]

So, using rot13, "fnynq" is caesar salad. --CLN


KBK - Any monoalphabetic substitution cipher is, of course, trivial to break. Solving cryptograms written in such ciphers is well within the compass of Tcl's capabilities.


FW gives a version that doesn't require any additional procedures, and rotates by any amount (specified by an optional second argument, defaults to 13 of course) -- And supports negative rotation amounts, now, too.

 proc rot {text {amount 13}} {
   if {abs($amount) > 25} {
     set amount [expr {$amount % 26}]
   }

   set alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
   set res ""
   set length [string length $text]
   set find_command [expr {$amount > 0 ? "first" : "last"}]

   for {set index 0} {$index < $length} {incr index} {
     set char [string index $text $index]
     set pos [string $find_command $char $alphabet]
     append res [expr {$pos == -1 ? $char : [string index $alphabet [expr {$pos + $amount}]]}]
   }
   return $res
 }

See also vignere, caesar, uuencode, base64

Category Package Category Cryptography