Tuesday, October 15, 2013

Code 26 : Scytale

Scytale A scytale is a cylinder of a specific diameter around which a strip of paper is wrapped. The plaintext message is written on the paper while it is wound around the stick. Once the paper is unwound, the message becomes difficult to read. To decipher the message, you simply wind the paper around another stick of the same diameter.



The Scytale has already been used more than 2500 years ago and therefore it is the oldest known military ciphering method. In the year 404 B.C. only one of five messengers survived the grueling march from Persia back to the Spartan general Lysander. The messenger gave Lysander his belt and Lysander winded his belt around the so-called Scytale. Thus he was informed that the Persians planned an attack, he prepared for this attack and was able to fend it successfully.1


The belt of the messenger was composed of a stretch of leather which had characters written on it on the backside. It seemed like a meaningless sequence of characters but when the stretch of leather is winded around a piece of wood with the correct diameter, an encoded message can be read.
Fig. 1: Scytale2



The principle of the Scytale was very secure for its time. After the demise of the Greek culture, comparably secure ciphers were not used until medieval times.


The Scytale is a cipher that is based on a transposition method.
The diameter of the Scytale can be regarded as the key of the cipher.




1 Singh, Simon: "Geheime Botschaften", Carl Hanser Verlag, 1999, P.23
2 http://commons.wikimedia.org/wiki/File:Skytale.png, 2009-03-15

Spartan Scytale (c 500 B.C.)

One of the earliest encrytptions device was the Spartan Scytale (c 500 B.C.) It was used by the Spartan Military for encoding message sent between commanders. The Scytale consisted of a ribbon wrapped around a dowel of a particular diameter and length. The secret message was written on the ribbon when the ribbon was wraped on the dowel. The ribbon was then removed and only the ribbon was transported to the other field commander who had an identical dowel that could used to decode the message. If the ribbon was intercepted it look like jumble of letters.
The following table illustrate the idea. Imagine that each column wraps around the dowel one time, that is that the bottom of one column is followed by the top of the next column.
Original message
Can you attack the left flank of the army during the second hour tomorrow. We will be able to send reinforcements by noon. How many men do you have? Do you need supplies? Send your reply to the river.
Wrapped message
C|a|n| |y|o|u| |a|t|t|a|c|k| |t|h|e| |l|e|f|t| |f|l|a|n|k| |o|f| |t|
h|e| |a|r|m|y| |d|u|r|i|n|g| |t|h|e| |s|e|c|o|n|d| |h|o|u|r| |t|o|m|
o|r|r|o|w|.| |W|e| |w|i|l|l| |b|e| |a|b|l|e| |t|o| |s|e|n|d| |r|e|i|
n|f|o|r|c|e|m|e|n|t|s| |b|y| |n|o|o|n|.| |H|o|w| |m|a|n|y| |m|e|n| |
d|o| |y|o|u| |h|a|v|e|?| |D|o| |y|o|u| |n|e|e|d| |s|u|p|p|l|i|e|s|?|
 |S|e|n|d| |y|o|u|r| |r|e|p|l|y| |t|o| |t|h|e| |r|i|v|e|r|.| | | | |
Encoded message
Chond aerfoSn ro e aorynyrwcodom.eu uy m y Wehoadenautu tvrtrwse aii ?rcnlb ekglyDp olttbn yhheoy ee oot anuolsb. eel ntfceHehto oee ntwd fdo rl msiahsauvnoenpekunypr rd l.o mi ftree oens tmi ?
The key parameter in using the scytale encryption is the number of letters that can be recorded on one wrap ribbon around the dowel. Above the maximum was 6, since there are 6 rows in the wrapped meassage. The last row was padded with blank spaces before the message was encoded. We'll call this the wrap parameter. If you don't know the wrap parameter you cannot decode a message.

Spartan Scytale PHP Code

Encoding function
function encode($N,$msg){ // $N is the wrap parameter

  // PHP's way of transforming a string into an array of letters
  $plain = preg_split("//",$msg, -1, PREG_SPLIT_NO_EMPTY); 

  $M = count($plain);
  $L = floor($M/$N);   // The floor function returns the greatest
       // below its argument.

  if ($L*$N<$M) $L++;   // Add one if divsion was not even.

  $encoded = array();
  for ($i=0; $i<$L; $i++){
    for ($j=0; $j<$N; $j++){

      if (isset($plain[$i+$j*$L])){ // Adds the next letter to the end of the encoded string
 $encoded[] =  $plain[$i+$j*$L];
      } else {
 $encoded[] =  ' ';

  $encoded[] ='*';   // Flags end to mark extra spaces if padded
  $msg = join('',$encoded);  // Makes string from array of letters
  return $msg;

Decoding function
function decode($N,$msg){ // $N is the wrap parameter  

  // PHP's way of transforming a string into an array of letters
  $encrypted = preg_split("//",$msg, -1, PREG_SPLIT_NO_EMPTY));
  $M = count($encrypted);
  $L = floor($M/$N);   // Based on the wrap parameter find the length of the line 

  if ($L*$N < $M) $L++;  // Add one to L if end spaces are missing

  $decoded = array();
  for ($i=0; $i<$N; $i++){
    for ($j=0; $j<$L; $j++){
      $decoded[] =  $encrypted[$i+$j*$N];
  return join('',$decoded);

Notes for Computer Science Students

The Spartan Scytale, in the form described above, is based on the two ways that multidimensional arrays are stored in a computers memory. The plain text message is store in what is called row-major order. The rows are stored one after another in a linear order. The encrypted text is the same data but in column-major order, where the colunms are stored linearly.
In programming this is often done automatically by the complier; however, when transferring raw data from one program to another knowing how the data is stored is critical. It would be like trying to read aloud the encrypted code.


Post a Comment

Note: Only a member of this blog may post a comment.