Hi, Habr!
All of us quite often hear such words and phrases as “data encryption”, “secret ciphers”, “crypto-protection”, “encryption”, but not everyone understands what exactly this is about. In this post, we will understand what encryption is and consider elementary ciphers so that even people far from IT understand the essence of this phenomenon.
First of all, let's understand the terminology.
')
Encryption is a transformation of the original message that does not allow any bad people to read the data if they intercept this message. This transformation is done by special mathematical and logical algorithms, some of which we will consider below.
The original message is, in fact, what we want to encrypt. The classic example is text.
An encrypted message is an
encrypted message .
The cipher is the algorithm by which we convert the message.
A key is a component on the basis of which encryption or decryption can be performed.
The alphabet is a list of all possible characters in the original and encrypted message. Including numbers, punctuation marks, spaces, separate lowercase and uppercase letters, etc.
Now, when we speak more or less the same language, let's take simple ciphers.
The easiest cipher. Its essence is the upside down of the alphabet.
For example, we have an alphabet that fully corresponds to the usual Latin.
abcdefghijklmnopqrstu vwxyz
To implement the Atbash cipher, simply invert it. “A” will become “Z”, “B” will turn into “Y” and vice versa. At the output we get the following picture:

And now we write the necessary message on the source alphabet and the alphabet cipher
Original post: I love habr
Encrypted: r olev szyi
Here one more parameter is added - a primitive key in the form of a number from 1 to 25 (for Latin). In practice, the key will be from 4 to 10.
Again, for clarity, we take the Latin
abcdefghijklmnopqrstu vwxyz
And now let's shift each letter to the right or left by the key number of values.
For example, we will have a key of 4 and an offset to the right.
Original alphabet: abcdefghijklmnopqrstu vwxyz
Encrypted: wxyzabcdefghijklmnopq rstuv
We try to write a message:
hello world
Encrypt it and get the following incoherent text:
dahhk sknhz
Vernam's cipher (XOR-cipher)
The simplest cipher based on binary logic, which has
absolute cryptographic security. Without knowing the key, it is impossible to decipher it (proven by Claude Shannon).
The original alphabet is all the same Latin.
The message is divided into separate characters and each character is represented in binary form.
Cryptographic classics offer a
five-digit Baudot code for each letter. We will try to change this code for 8 bit / character encoding using the example of an ASCII table. We represent each letter in the form of a binary code.

Now remember the electronics course and the element "Exclusive OR", also known as XOR.
The XOR receives signals (0 or 1 each), performs a logical operation on them, and outputs one signal based on the input values.
If all signals are equal to each other (0-0 or 1-1 or 0-0-0, etc.), then at the output we get 0.
If the signals are not equal (0-1 or 1-0 or 1-0-0, etc.), then the output is 1.
Now to encrypt the message, enter the encryption text itself and the key of the same length. We translate each letter into its binary code and execute the formula
message XOR keyFor example:
Post: LONDON
key: SYSTEM
Translate them into a binary code and execute XOR:
01001100 01001111 01001110 01000100 01001111 01001110 01010011 01011001 01010011 01010100 01000101 01001101 _______________________________________________________ 00011111 00010110 00011101 00010000 00001010 00000011
In this particular example, in place of the resulting characters, we will see only an empty space, because all the characters fall into the first 32 service characters. However, if we translate the result into numbers, we get the following picture:
31 22 29 16 10 3.
It looks like a completely disconnected set of numbers, but we know.
The encryption principle is about the same as that of the Caesar cipher. Only in this case we shift the alphabet not by a certain number of positions, but by a code word.
For example, take for a change, the Cyrillic alphabet.
Let's invent a code word. For example, "Lukomorye". Pull out all the repeated characters from it. At the exit we get the word "Lukomrie".
Now enter the given word at the beginning of the alphabet, and leave the remaining characters unchanged.
And now we will write down any message and encrypt it.
" "
We end up with the following unreadable nonsense:
" "
The classic Player's cipher assumes a 5x5 matrix based on the Latin alphabet (i and j are written in one cell), a code word and further manipulation of them.
Let the code word be “HELLO”.
At first we act as with the previous code, i.e. remove the repetitions and write the word at the beginning of the alphabet.

Now take any message. For example, "I LOVE HABR AND GITHUB".
We divide it into bigrams, i.e. on pairs of characters, not including spaces.
IL OV EH AB RA ND GI TH UB.
If the message were from an odd number of characters, or there were two identical characters in the digram (LL, for example), then the X character is put in place of the missing or repeated character.
Encryption is performed according to several simple rules:
1) If the digram characters are in the matrix on one line - shift them to the right by one position. If the character was extreme in the series - it becomes the first.
For example, EH becomes LE.

2) If the digram characters are in one column, they are shifted down one position. If the character was at the very bottom of the column, then it takes the value of the highest one.
For example, if we had the LX digram, then it would become DL.
3) If the characters are not on one line, not on one column, then we build a rectangle, where our characters are the edges of the diagonal. And swap corners.
For example, the RA bigram.


By these rules, we encrypt the entire message.
IL OV EH AB RA ND GI TH UB. KO HY LE HG EU MF BP QO QG
If we remove the spaces, we get the following encrypted message:
KOHYLEHGEUMFBPQOQG
Congratulations. After reading this article, you at least understand something about encryption, and you know how to use some primitive ciphers and you can start learning some more sophisticated cipher samples, which we will discuss later.
Thanks for attention.