During TISafe‘s Security Officer training this month, while talking about cryptography, the Scytale technique came up.
This was one of the first cryptography method with historical registry, used by the Greeks (Spartans, more specifically) to cypher messages during military campaigns thus not letting the enemy know their moves even if the message carrier gets caught, tortured or killed.
The cypher
A very simple approach implements transposition cypher with a strip of leather, cloth or paper around a baton. Baton diameter inflicts directly in the encryption result so we can say that it is this algorithm’s symmetric key
Encryption process
To encrypt the message, the sender rolls the strip of leather (or cloth, or paper, etc) around his baton and draw the message (vertically, character by character) across the strips, going back to the first strip on the next “line” when writing to the last strip, like word-wrap.
This example from Wikipedia ilustrates well the concept:
_____________________________________________________________
| | | | | | |
| H | E | L | P | M | |
__| E | I | A | M | U |__|
| | N | D | E | R | A |
| | T | T | A | C | K |
| | | | | | |
_____________________________________________________________
Decryption process
As most symmetrical-key cypher systems, the decryption process is given by reversing the encryption process with the same key applied. The person with a baton with the same diameter (the same key) would be able to roll the strip over it and decrypt the message.
Key value restrictions
For a baton with 1cm diameter, the message will look just like the original, so we will discard key values of 1. The same occurs if key value is equal to the message length.
The algorithm
The key represents the number of characters in each line of text:
- key value:
2 - 2 characters per line
- key value:
4 - 4 characters per line
For the explanation of this algorithm we’ll encrypt the following message: “HEAVYWORKS” (nice huh?)
Chunk split
The message is split into chunks of N characters, being N being the key value.
| Original | Key 2 | Key 3 | Key 4 |
|---|---|---|---|
| HEAVYWORKS | HE AV YW OR KS | HEA VYW ORK S | HEAV YWOR KS |
Key relation
All the character sequences are now ordered in columns after we splitted the message in chunks with the length of the key value. You should realized already that the number of columns will be equal to the chunk length and thus the key value.
Transposition
The transposition is made by creating new (encrypted) chunks with the characters from each column. These chunks will be of the same length of the corresponding column.
At the end of the process, all cyphered chunks are put together.
Key 2 | Key 3 | Key 4 | |
|---|---|---|---|
| Cyphered chunks | HAYOK EVWRS | HVOS EYR AWK | HYK EWS AO VR |
| Cyphered message | HAYOKEVWRS | HVOSEYRAWK | HYKEWSAOVR |
The C++ Way
The Scytale class got very simple, as the algorithm is very simple. It uses only three libraries: iostream, vector (for variable-length arrays) and cmath (for some ceil calculation).
The binary is called from command-line with the -k keylenght and -m message (quote escaped).
Under the hood
The main function fetches the options using GNU getopts and thus does some early input checking and error handling.
All OK, the key is set by Scytale’s class’ setKey method and message is passed as a parameter to the encrypt method. From this point validate method is called in order to validate if the key and messages are fit into the needs on try/catch statements.
Passing the validation, the createStack method creates the character chunk stack (oh, really!?) from the plain-text message with two nested for laces (first for rows, second for char position) that is handled over to scrambleStack in order to perform the transposition with another two netsted for laces (now the first for column and the second for row).
Finally, the encrypted message
NOTE: If validation fails, program exits with a EXIT_FAILURE error code and the error message is echoed to stderr.
Source
The source file is well documented using a kind of Javadoc syntax and can be downloaded here.
MD5 Sum
a33edf6a4c4670380ce8c8e40edcb3d7 scytale.zip
Compiling
In order to compile with g++ use the following line:
$ g++ -Wall -ansi -pedantic -o scytale scytale.cpp
NOTE: I was not able to compile this under Windows XP with MinGW‘s g++. Got compiling errors because of getopts and vector. If someone succeeds in compiling under Windows XP post a comment!
Drawbacks
This system cannot assure authenticity because it has no way to prove the author of the message so it can be crafted by the enemy that deduced, guessed or stolen the key.