Elements of cryptography: The symmetrical cryptography
In the previous article we saw a particular case of the cryptographyapplication, that is hashing. We saw how useful it is in coding passwords or to assure us the validity of a downloaded file. But this technique isn’t applicable when one needs to codify a message that somebody else will read since hashing is not reversible.
So we have to rely on the symmetrical cryptography that like the asymmetrical cryptography (we’ll treat it in the next article), should be able to guarantee mainly three things:
- Confidentiality: only the addressee must be able to read the message.
- Authenticity: the addressee must be sure that the sender of the message is exactly who he says he is.
- Integrity: the message, even if impossible to read must not be modifiable, corruptable by others without that results immediately clear.
Let’s begin with seeing some historical examples.
The symmetrical cryptography: a long story
The need to make a message confidential goes back to millions of years before the invention of computer science. The first technique of cryptography of which we have news, goes back to the ninth century A.D with the scitalia lacedemonic system. It consisted in winding in a helix a leather ribbon on a cylinder. Then the message was written in vertical. Once the ribbon was removed from the cylinder it was “impossible” to read the message, unless it was rewound on a cylinder with the same diameter.
In the course of time we find many examples, some of them really astonishing, like enigma, the mechanical rotor codifier invented and used by Nazis during the Second World War to cryptograph messages addressed to submarines.
The best minds of that period were reunited at Bletchley Park in England to try to decipher the messages written with this machine, but they obtained only a partial success and they didn’t fully understand the mechanism of coding of this macchine.
We also find quite trivial examples, such as Caesar’s cipher book that simply represents the deviation of the letters (the original cipher provided an offset of 3 letters).
It’s nothing but MAURIZIO substituting every letter with the following one ( so the key code here is 1).
Curiosity: we find the first case of hidden advertisement in Kubrick’s film 2001 Odyssey in Space where the name of the super computer HAL 9000 in realty becomes IBM applying Caesar’s code book with key -1.
As you can see , the principle of the symmetrical cryptography consists in having one single key with which one codifies and decodes a message (the diameter of the cylinder, the positioning of the rotors of enigma, the number of the letter shifting in Cesar’s code book).
It’s like if we could imagine to put a message in a trunk that has a lock, I have a key and the receiver has the same key. When the receiver has in his hands the trunk , he will able to open it with his key (that is the same of mines, for this reason it is called symmetrical cryptography). In this way authenticity is guaranteed ( the trunk is closed because I closed it) and confidentiality (nobody else besides me and the receiver have the key). The problem of integrity still remains.
What happens if there’s a videotape inside my trunk and this accidentally or intentionally passes through a strong magnetic field? The matter of integrity has much to do with hashing algorithms that we treated in the previous article and that we’ll see better afterwards.
Obviously, in realty, we have to codify strings and we’ll do it with functions that generally pass at least three parameters:
- The string to be encrypted.
- The encryption key.
- The modality (coding and decoding).
These functions belong to very complicated algorithms that we will not deal with. Instead we’ll see an example of the symmetrical coding of a message.
PHP puts at one’s disposal several functions belonging to the mcrypt libraries.
So let’s see an example of coding with PHP.
We’ll use the function mcrypt_cfb() that as you can see passes four parameters.
- The encryption algorithm. Here’s the list of the supported algorithms.
- The encryption key.
- The string to be encrypted.
- The modality (MCRYPT_ENCRYPT o MCRYPT_DECRYPT).
So we can move this way:
echo mcrypt_cfb(MCRYPT_DES, "key", "word to decript", MCRYPT_ENCRYPT);
to obtain this:
In realty, we’ll also obtain an error level Warning, since the fifth parameter, given as an optional, is strongly recommended and it is the initialization vector of the algorithm, which I don’t intend to dwell on, at the moment I’m interested in the principle.
Receiving this message and knowing the key (and the algorithm), all I need to do is act like this:
echo mcrypt_cfb(MCRYPT_DES, "key", "ãñòÏ#ðzz|qòÓ¦|", MCRYPT_DECRYPT);
in order to obtain as a result:
Phrase to encrypt
The evident limit of the symmetrical cryptography is the need to dispose of a key arranged between two persons that are exchanging a message. It’s obvious that this technique, in the modern computer era, is not enough. Imagine, if you would use the symmetrical cryptography for the codification of the data of your credit card for an on-line purchase. You would need to request a key to the manager of the transaction in order to codify the data, but how should the message containing the key be codified? Should it be sent via mail? It would really be too complicated, while on-line purchases have to be fast and easy.
In the next article we’ll how to get over this limit with the asymmetrical cryptography, thanks to which we don’t need to fix the encrytion key. But we’ll also see why, although this, the symmetrical cryptography is still not surpassed. Have you ever used symmetrical algorithms? For what purposes?
In tutti questi anni abbiamo ricevuto centinaia di richieste di approfondimento sulle numerose tematiche del web design vissuto da freelance. Le abbiamo affrontate volta per volta. Ma ci siamo resi conto che era necessario fare qualcosa di più. Ecco perché è nato One Year Together, un vero e proprio master per web designer freelance che apre finalmente le porte al mondo del lavoro.
Scopri One Year Together »