Engee documentation
Notebook

Substitution Cipher Model

We implement the encryption model using a simple substitution (monoalphabetic cipher).

Task description

Substitution ciphers are another basic block of modern encryption systems. Its various variants were used both in antiquity (the Caesar cipher, the cipher of simple substitution), and inside the implementations of the most modern algorithms, as the next stage of encryption.

The cipher we are implementing is called a simple substitution cipher, and its work boils down to creating an encryption table in which a single letter of the ciphertext is accepted for each letter of the plaintext. We will use only 33 characters - the uppercase part of the Cyrillic alphabet characters (including the letter Ё).

We will encrypt messages character by character, replacing each character with its corresponding ciphertext character using a substitution table.

In [ ]:
using Random

The alphabet of the original message consists of 33 characters:

In [ ]:
print( collect(1:33) )
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]

The cipher alphabet must contain all these characters, but in a pseudo-random order.:

In [ ]:
target_order = shuffle( Xoshiro(1), 1:33 );
print( target_order )
[16, 13, 10, 3, 2, 22, 26, 33, 5, 25, 31, 27, 14, 28, 7, 29, 30, 12, 11, 9, 23, 4, 17, 6, 20, 8, 24, 15, 21, 19, 1, 18, 32]

To use this table in the opposite direction, we will need to apply sorting:

In [ ]:
print( sortperm( target_order ) )
[31, 5, 4, 22, 9, 24, 15, 26, 20, 3, 19, 18, 2, 13, 28, 1, 23, 32, 30, 25, 29, 6, 21, 27, 10, 7, 12, 14, 16, 17, 11, 33, 8]

Please note that when referring to the command shuffle each time we initialize the random number generator with a certain number 1. This allows us to get the same pseudo-random sorting order of values from time to time (the same for the encryption block and for the decryption block).

image.png

This model encrypts and decrypts the message.

Permutation Blocks

Operation models Перестановки and Обратной перестановки created using blocks LookupTableND, for the convenience of those equipped with a mask with controls.

The front side of the permutation block reflects two things:

  • number Seed, which we use to initialize the random number generator
  • is the permutation direct or reverse
image.png

The parameters of the permutation block are set in the settings of its mask.

Launching the model

Let's run the model using software management tools:

In [ ]:
# Загрузим модель, если она еще не открыта на холсте
if "substitution_cipher_model"  getfield.(engee.get_all_models(), :name)
    engee.load( "$(@__DIR__)/substitution_cipher_model.engee");
end

model_data = engee.run( "substitution_cipher_model" );
In [ ]:
source_message = collect(model_data["Сообщение"]).value
cipher_text = Int.(collect(model_data["Шифротекст"]).value)
received_message = Int.(collect(model_data["Расшифровка"]).value)

println( "Отправленное сообщение: ", source_message )
println( "Зашифрованное сообщение: ", cipher_text )
println( "Полученное сообщение: ", received_message )
Отправленное сообщение: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
Зашифрованное сообщение: [16, 13, 10, 3, 2, 16, 13, 10, 3, 2, 16]
Полученное сообщение: [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1]
In [ ]:
alphabet_dict = Dict( zip( 1:33, "абвгдеёжзиёклмнопрстуфхцчшщъыьэюя" ))
print( "Расшифрованное сообщение: ", join( [alphabet_dict[d] for d in received_message], "" ) )
Расшифрованное сообщение: абвгдабвгда

Since the message is passed in a circle in this demo, we got 11 letters, which may be more or less than the number of characters in the original message.

Conclusion

We have shown how the substitution cipher works using a simplified example, where the message is sent as an array of numbers indicating the position of each letter of the original message in the Cyrillic alphabet (in lowercase). The next step is to adapt the model to send byte-by-byte messages by replacing the code in blocks. Подстановка and Обратная подстановка.

Blocks used in example