Cryptography
Cryptology (Greek “kryptos” = hidden, secret, “logos”= study, science)
Cryptography (“kryptos”=secret, “graphein”=writing)

Definition:

Cryptography is the study of hiding information. Cryptography is the science of concealing the content of communication between parties, where the channel between them is controlled in some way by an unfriendly third party.

Cipher (Cryptosystem)

-  a pair of algorithms that create the encryption from plaintext to ciphertext, and the reversing process (decryption from ciphertext to plaintext)
-  the algorithms have a secret parameter called key (ideally known only to Alice and Bob, and which changes from one message exchange to the next).

Cryptanalysis

-  study of methods for obtaining the meaning of encrypted information without access to the key normally required to do so (study of how to crack encryption algorithms).
-  Eve (Eavesdropper, Enemy, Evil third party) intercepts the ciphertext. She may know the cipher (the algorithms for encryption, decryption, but does not know the key). Eve can have a “passive role” (attemps to recover the plaintext, or to deduce the key), or an “active role” (alter the message sent over the channel).

History of Cryptography

Classical Crytopgraphy 
- simple methods of encryption and decryption using only pen and paper an perhaps simple mechanical aids.
- non-standard hieroglyphs carved into monuments from the Old Kingdom of Egypt (1900 BC), 
- clay tablets from Mesopotamia (1500 BC )
- Hebrew scholars - simple monoalphabetic substitution ciphers (500-600 BC) (replace first alphabet letter with last, second letter with next to last, ...)
- Ancient Greeks
     - transposition ciphers (500 BC) (Spartan Scytale  - thin strip of parchment wrapped around a cylindrical rod called scytale; the diameter of the scytale was known only by the sender and the receiver) 
     - Polybius square (205-123 BC) (pairs of numbers substitute for letters
- Romans - Caesar Cipher (50 BC, Gallic Wars) - Shift ciphers +k

Medieval Cryptography 

- polyalphabetic substitution
- Alberti’s Cipher Disk
- Vigènere cipher (~16th century) correspondents agree on a keyword, which controls letter substitution depending on which letter of the key word is used.

WW1 (ADFGVX)

- correspondents knew an encoding table for letters & ciphers and a keyword; letters A, D, F, G, V, X were chosen deliberately because they sound very differently when transmitted via Morse code
- to encipher: the plaintext is coded using the letters A,D, F, G, V, X via the chart. The resulting pre-ciphertext is then enciphered using a keyword columnar transposition cipher.
- to decipher: reverse the transposition and decode using the chart.

({{●, A, D, F, G, V, X}, {A, F, L, 1, A, O, 2}, {D, J, D, W, 3, G, U}, {F, C, I, Y, B, 4, P}, {G, R, 5, Q, 8, V, E}, {V, 6, K, 7, Z, M, X}, {X, S, N, H, 0, T, 9}}) ({{A, R, M, S}, {1, 2, 3, 4}, {D, F, G, X}, {D, F, F, D}, {A, D, A, D}, {D, F, F, D}, {X, D, □, □}})

WW2 
- cable and radio transmissions were encrypted
- Enigma - cryptographic machine used by the Germans (Enigma accepted letters as keyboard input and implemented a polyalphabetic substitution cipher using an electro-mechanical rotor machine with rotating disks; it produced output by lighting electrical lamps beneath lettered windows )

Modern Cryptography

- Ciphering operations are too complex to be made by hand; computers are needed. Complex ciphering can be obtained by composition of a certain number of simpler ciphering functions.
- Claude Shanon (1949) established solid theoretical basis for cryptology and cryptanalysis (confusion/diffusion). 
  Confusion - make the relation between the key and the ciphertext as complex as possible.
  Diffusion - data diffusion: changing one letter in the plaintext will change many letters in the ciphertext
                - key diffusion: changing even a tiny part of the key should change each bit (letter) in the ciphertext with a given probability. 
- Symmetric Key Cryptography
    - the sender and the receiver share the same key; the key is communicated separately over a different channel.
    - 1970 Data Encryption Standard (DES) symmetric block cipher
    - 2001 Advanced Encryption Standard (AES) 
    AES and Triple DES (with 128, 256-bit keys) are used in banking, ATM transactions, e-commerce, industry, mobile communications
    
- Asymmetric (public) Key Cryptography
   -  new methods of distributing cryptographic keys (no secure channel is needed)
  - asymmetric key algorithms use a pair of mathematically related keys (one public, one private), each of which decrypts the encryption performed by the other; the private key cannot be deduced from the public key by any computational method other than trial and error. Cracking the encrypted message without the private key requires a major computational task. 
  -  based on number theory
  -  Digital Signatures, Public Certificates, Authentication Protocols (SSL, SSH)
 - E-banking (secure bank transfers), E-commerce (cards), protection against collection of personal data by various companies, PGP email encryption, Operating Systems, Cryptographic Protection of classified documents, etc.

Example: The Caesar cipher

One of the oldest cryptosystems is due to Julius Caesar. The Caesar cipher is defined over the alphabet {A,B,C, ..., Z}, encoded numerically as integer numbers between 0 and 25,  {A→0, B→1, ..., Z→25}. The key k can be any integer between 0 and 25. The cipher shifts each letter in the text cyclicly over k places. The encoding function is (Subscript[E, k])(x)=x+k (mod 26) and the decoding function is (Subscript[D, k])(x)=x-k (mod 26). The notation x mod 26 means the remainder of the division of x by 26.

Define the Caeser cipher over integer numbers {0, 1, ..., 25}

Clear[CaesarEncode, CaesarDecode]

CaesarEncode[x_ ?IntegerQ, k_]:= Mod[x+k,26]
CaesarDecode[x_ ?IntegerQ, k_]:= Mod[x-k,26]

SetAttributes[CaesarEncode, Listable];
SetAttributes[CaesarDecode, Listable];

Alphabet1 = Table[i, {i,0,25}]
CaesarEncode[Alphabet1,3]

ASCI code of alphabet letters

The Mathematica functionsToCharacterCode[..] and FromCharacterCode[..] convert symbols to their ASCI code and back (letter A has value 65, letter B has value 66, .... , letter a has value 97, letter b has value 98, etc.).

FromCharacterCode[65]

FromCharacterCode[{97}]

ToCharacterCode["A"]

ToCharacterCode["AaBbCc"]

ToUpperCase["AaBbCc"]

ToCharacterCode[ToUpperCase["AaBbCc"]]

Select[{4,46,2}, (0<=#≤25)&]

Define the Caesar cipher over strings

Encode and decode a given string using the simple encoding A=0, ... , Z=25 (stripping all other characters).

CaesarEncode[x_ ?StringQ, k_]:= Module[{n,numerictext},
numerictext=ToCharacterCode[ToUpperCase[x]]-65;
numerictext = Select[numerictext, (0<=#≤25)&];
n = Mod[numerictext+k,26];
FromCharacterCode[n+65]
]

CaesarDecode[x_ ?StringQ, k_]:= Module[{n,numerictext},
numerictext = ToCharacterCode[x]-65;
n = Mod[numerictext-k,26];
FromCharacterCode[n+65]
]

?CaesarEncode

plain = "TYPE YOUR TEXT HERE";
CaesarEncode[plain,2]

CaesarDecode["VARGAQWTVGZVJGTG",2]

Cryptanalysis of the Caesar cipher

An easy way to break the ciphersystem is to try out all possible keys. This method is called exhaustive key search, or brute-force approach.

Example :  The text “TFEXIRKLCRKZFEJPFLALJKSIFBVPFLIWZIJKTZGYVI” has been encoded using a Caesar cipher with some unknown key k. Decrypt it.

ciphertext ="TFEXIRKLCRKZFEJPFLALJKSIFBVPFLIWZIJKTZGYVI";
Table[CaesarDecode[ciphertext, k], {k,0,25}]