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.