Engee documentation
Notebook

XOR encryption model

We implement a very basic symmetric XOR (gamming) encryption algorithm and study the error rate.

Task description

Symmetric ciphers allow messages to be encrypted and decrypted using the same key. It is a very frequent component of encryption algorithms. Although it is vulnerable to frequency analysis and many other matching techniques.

The XOR (exclusive OR) operation performs the following operation on the input bits:

a b XOR(a, b)
1 1 0
0 1 1
1 0 1
0 0 0

The following scheme allows you to experiment with messages in string representation, compare them and count decryption errors.

image.png

  • The first XOR block performs encryption, followed by the ciphertext bits on the signal line.
  • The second XOR block performs decryption using the same key bytes as the first XOR block.

If transmission delays occur in the intermediate channel, the comparison of Message and Ciphertext in the Error Rate Calculation block can be offset (see block settings).

Running the model

The model can be started using software control commands:

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

model_data = engee.run( "xor_cipher_model" );

Output the number of decoding errors:

In [ ]:
print( "Количество ошибок расшифровки: ", Int32(model_data["Кол-во ошибок"].value[end]) )
Количество ошибок расшифровки: 0

Let's see what bits came after decryption:

In [ ]:
out_vector = collect(model_data["Расшифровка"]).value;
received_bytes = [ UInt8(evalpoly(2, reverse(out_vector[1+8*start:8*start+8]))) for start in 0:div(length(out_vector),8)-1 ]
Out[0]:
25-element Vector{UInt8}:
 0xd0
 0xa1
 0xd0
 0xbe
 0xd0
 0xbe
 0xd0
 0xb1
 0xd1
 0x89
 0xd0
 0xb5
 0xd0
 0xbd
 0xd0
 0xb8
 0xd0
 0xb5
 0xd0
 0xa1
 0xd0
 0xbe
 0xd0
 0xbe
 0xd0

To assemble a Unicode string from bytes:

In [ ]:
String( received_bytes )
Out[0]:
"СообщениеСоо\xd0"

**We get the same message out as we sent in.

The result of the model can be studied without scripts, it is enough to study the information on the Graphics panel.

image.png

As we can see, the error rate does not increase because the same key bits are used for encryption and decryption, and because there is no noise in the channel.

Conclusion

We have created an environment to study the simple cryptographic algorithm XOR, also called gamming. The model performs encryption and decryption, it is easy to add noise and bias, and as further steps we can try frequency analysis.