# Information Security: 3. Classical Encryption Technique

## 3.1 Symmetric Cipher Model
- Steps:
 - Plaintext input
 - Encryption algorithm: Y = E(K, X)
 - Ciphertext
 - Decryption algorithm: X = D(K, Y)
 - Plaintext output
- uses same key (symmetric)
- classic model

### Attacking Crypto System
1. Cryptanalysis
2. Brute-force attack

### An Encryption Algorithm is said...
1. Unconditionally secure
 - unsolvable
2. Competitionally secure
 - solvable, but high cost & time consuming

## 3.2 Substitution Techniques
### 3.2.1 Caesar Cipher
- c = E(k, p) = (p + k) mod 26
- p = D(k, c) = (c - k) mod 26
  
### 3.2.2 Monoalphabetic Cipher
- key space: 26!
- brute force -> impossible
- use relative frequency of letters in english text
- Digram (substitute double)
  
### 3.2.3 Playfair Cipher
- Key and table
 - if key = monarchy, then table is...
```
m  o  n  a  r
c  h  y  b  d
e  f  g  ij k
l  p  q  s  t
u  v  w  x  z
```
- Plaintext
 - split by every 2 letters (if odd, insert 'x' anywhere)
 - ballon -> ba lx lo on
- Encryption: if two letters on the...
 - same row: right (ex: ux -> vz)
 - same column: below (ex: is -> sx)
 - else: make rectangle, go horizontal (ex: hs -> bp)
- Not much effective

### 3.2.4 Hill Cipher
- Matrix math
 - Inverse element: A + I = E
```
A = 5 8
    17 3
B = 9 2
    1 15
AB = 53 130
     156 79
AB mod 26 = 1 0
            0 1
```
- 0~25 on alphabets (if bigger, mod 26)
- Encryption: C = PK mod 26
 - Key: m*m matrix => K
 - Plaintext: 1*m matrix
 - Cipher: 1*m matrix
 - Example
```
K = (5 8)
       (17 3)
P = "HI" = (7 8)
C = PK mod 26 = (171 80) mod 26 = (15 2) = "PC"
```
- Decryption: P = C (K^-1 mod 26)
```
(15 2) (9 2)
         (1 15)
= (137 60) mod 26
= (7 8)
= "HI"
```
- Is it good?
 - still has relative frequency problem
 - known plaintext attack

### Known plaintext attack
- Y = XK
- If X and Y are known, the key is K = X^-1 Y.

### 3.2.5 Vigenere Cipher
- From Caesar cipher, when len(K) != len(P) ...
```
K = k[0] k[1] ... k[m-1]
P = p[0] p[1] ... p[n-1]
C = c[0] c[1] ... c[n-1]
C = (p[0]+k[0])mod26 ... (p[m-1]+k[m-1])mod26 (p[m]+k[0])mod26 ...
```
- Example
```
K = "deceptive"
P = "wearediscoveredsaveyourself"
C =
    wearedisc overedsav eyourself
    deceptive deceptive deceptive +
    _______________________________
    zicv...
```
- Is it good?
 - still has relative frequency problem
 - all we need to know to decode cipher is the length of K.

- Variant
```
C =
    wearedisc overedsav eyourself
    deceptive wearedesc overedsav +
    _______________________________
    ...
```

### Relative Frequencies
![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1662447530334/HYL8dhqPZ.png align="left")

### 3.2.7 Vernam Cipher
> c[i] = p[i] ⊕ k[i]
> p[i] = c[i] ⊕ k[i]  
  
```
p[i] = c[i] ⊕ k[i]
 = (p[i] ⊕ k[i]) ⊕ k[i]
 = p[i] ⊕ (k[i] ⊕ k[i])
 = p[i] ⊕ 0
 = p[i]
```
- if c and p are known, k can be also found.

### 3.2.8 One-Time Pad
> cA = E(A, kA)
> cB = E(B, kB)
> ...
  
- In practice,
 - generate random key each time
 - or share keys

## 3.3. Transposition Techniques
### 3.3.1 Rail Fence
- example
```
meetmeaftertheparty
->
m e n a t r h p r y
e t e f e t r e a t
```

### 3.3.2
- example
```
P = meetmeaftertheparty
K = 4312567, xy
table: 4 3 1 2 5 6 7
       m e e t m e a
       f t e r t h e
       p a r t y x y
C = eer/trt/eta/mfp/mty/ehx/aey
```
- If this is not enough, encode the first cipher in the same way again.

