ELI5: About the different encryption standards in use.

OK. The basic principle of most block ciphers (e.g. AES and DES) is substitution and permutation.

Substitution means replace one symbol with another - in the same way as a child's crude code might replace the letter "D" with a "Y" and a letter "E" with a "Q".

Permutation means shuffle the symbols, or bits around. So, a message ABCDEFGH might become DHAGFCBE.

AES works on 16 bytes at a time. In the case of AES the operations are as follows:

  1. Substitution using a table - e.g. a byte value of 00 will be replaced with 0x63, and a byte 01 will be replaced with 0x7c, etc. See http://en.wikipedia.org/wiki/Rijndael_S-box

  2. The bytes are permuted so that ABCDEFGHIJKLMNOP becomes ABCDHEFGKLIJPMNO

  3. Mixing This step mixes each byte with 3 others, using a series of multiplications and additions. For example A = 2A + 3 H + 1 * K + 1 * P, B = 1 *A + 2 * H + 3 * K + 1 * P, etc.) This is repeated for each byte.

  4. A "round key" calculated from the main key is then XORed with the 16 bytes.

  5. The process is repeated for a total of 10 times, using a different round key each time.

DES uses a broadly similar process of substitution and permutation and mixing with the key, which is then repeated over a series of "rounds".

Asymmetric ciphers don't work this way. Instead, they work by utilising mathematical functions which are easy to calculate if you have enough information, but very difficult to solve if you don't. For example, the prime number problem I mentioned earlier.

Essentially, the magic in RSA is the finding of the key pair. By choosing two random prime numbers, p and q, you can calculate 3 numbers n, d and e. The public key is the combination of n and e, and the private key n and d.

Encryption is simply calculating C = (M ^ e) modulo n where M is your message in numerical form. Decryption is simply calculating M = (C ^ d) modulo n.

A trivial, hand-worked example is given here: http://en.wikipedia.org/wiki/RSA_(cryptosystem)#A_worked_example

There are technical issues here in that n is a vast number (hundreds or thousands of decimal digits long) and e is often many hundreds or thousands. Similarly M, your message may be very long. Doing this quickly requires clever programming tricks.

/r/explainlikeimfive Thread