# FSA Lab №2
Performed by Vyacheslav Goreev (SB-1)
### Task №1
Given the codebook with day-keys and rotor settings. For example, for this particular day have the plugboard settings `H/R – A/J – X/E – O/P – R/F`, rotor setting `3 — 2 — 1` and rotor orientations `S — K — A`
1. We have to swap the letters `H` and `A` by connecting them on the plugboard, swap A and J, and the same with the other letter pairs. So now, if we encode the message `HAXXOR`, this would be encoded as `RJEEPF`.
2. We have to arrange rotors in given sequence `3 — 2 — 1`
3. We have to rotate the rotors so that they will have letters `S — K — A` selected in correspondent rotors.
4. Type the letter
5. The encoded letter will appear at lamp board.
6. Append encoded letter to the message string
7. Repeat 4-6 steps until done
### Task №2
If we consider Viola machine with $r=10$ rotors each with 30 characters chosen among $n=50$ rotors; a plugboard with $w<=15$ wires all plugged; and 1 static reflector with 5 options to select
1. The rotors can be ordered in ${n!\over(n-r)!}=37,276,043,023,296,000$ ways
2. The relative position of the rotor and their internal wiring can be set to 30 values; for $30^r=590,490,000,000,000$ combinations.
3. On the reflector $\sum_{v=0}^{w} {30!\over(30-2v)!\cdot2^v\cdot v!}$
$0$ wires has $73096577329197271449600000$ combinations
$1$ wires has $365482886645986357248000000$ combinations
$2$ wires has $822336494953469303808000000$ combinations
$3$ wires has $1096448659937959071744000000$ combinations
$4$ wires has $959392577445714187776000000$ combinations
$5$ wires has $575635546467428512665600000$ combinations
$6$ wires has $239848144361428546944000000$ combinations
$7$ wires has $68528041246122441984000000$ combinations
$8$ wires has $12849007733647957872000000$ combinations
$9$ wires has $1427667525960884208000000$ combinations
$10$ wires has $71383376298044210400000$ combinations
$11$ wires has $3244698922638373200000$ combinations
$12$ wires has $135195788443265550000$ combinations
$13$ wires has $5199838017048675000$ combinations
$14$ wires has $185708500608881250$ combinations
$15$ wires has $6190283353629375$ combinations
So in total there is $4,215,120,372,309,660,462,559,535,625$ combinations for $w <= 15$ wires
4. There is 5 reflectors so there is 5 times more variants to make permutation
5. So the total number of combinations is $s={n!\over(n-r)!}\cdot 30^r \cdot {\sum_{v=0}^{w} {30!\over(30-2v)!\cdot2^v\cdot v!} * 5}=$ $463,897,825,992,878,889,462,162,403,678,166,141,015,454,000,000,000,000,000,000$
6. To make things easier it is $4.638978259928789e+59$
---
If we consider typical Enigma machine with $r=3$ rotors chosen among $n=5$ rotors; a plugboard with $w=10$ wires all plugged; and 1 static reflector
1. The rotors can be ordered in ${n!\over(n-r)!}=60$ ways
2. The relative position of the rotor and their internal wiring can be set to 26 values; for $26^r=17,576$ combinations.
3. On the reflector, each of the $2w$ extremities of the wires can be plugged into any of 26 positions not otherwise occupied, with the two extremities of a given wire equivalent, as well as the wires themselves; for ${26!\over(26-2w)!\cdot2^w\cdot w!}=108,531,557,954,820,000$
4. So the total number of combinations is $s={n!\over(n-r)!}\cdot26^r\cdot{26!\over(26-2w)!\cdot2^w\cdot w!}=158,962,555,217,826,360,000$
The typical Enigma is $2918283650871111025772059714178425125170$ times easier to break.
### Task №3
1. John the Ripper
2. Hashcat
I have selected Hashcat as it probably the most widely used password cracking tool out there
### Task №4
```
hashcat -m 1800 -a 0 -o cracked.txt hashedpassword.txt rockyou-60.txt --force -d 1,2
```
The cracked password is `kissme`
### Task №5
In a stream cipher, the plaintext is encrypted one bit at a time. In a block cipher, the plaintext is broken into blocks of a set length and the bits in each block are encrypted together.
#### Block cipher
###### Advantages
1. The ease of implementation and less restrictive requirements (e.g. random generation)
2. Integrity protection mechanisms (like the MAC in the GCM mode of operation). This allows the recipient to verify that the message was not tampered with in transit.
###### Disadvantages
1. Block ciphers are slower and less memory efficient than stream ciphers.
2. High memory requirements of the cipher to store the padded plaintext and ciphertext.
3. Transmission errors often cause the rest of the ciphertext to be unrecoverable.
#### Stream cipher
###### Advantages
1. Stream ciphers are usually best for cases where the amount of data is either unknown, or continuous - such as network streams
2. Stream ciphers are typically faster than block ones.
3. Takes less memory
4. Stream ciphers are more difficult to implement correctly, and prone to weaknesses based on usage - since the principles are similar to one-time pad, the keystream has very strict requirements. On the other hand, that's usually the tricky part, and can be offloaded to e.g. an external box.
###### Disadvantages
1. Do not provide integrity protection or authentication.
### Task №6
##### Electronic Code Book (ECB) Mode
Electronic Code Book (ECB) is the simplest block cipher mode of operation. In this mode, as shown in the Figure below, each block of plaintext is encrypted separately. The main **disadvantage to this mode is that identical plaintexts encrypted with the same key create identical ciphertexts, which allows an attacker to learn some information about the encrypted message based solely on the ciphertext**.
###### ECB Mode

###### Possible attack
A cut-and-paste attack is an assault on the integrity of a security system in which the attacker substitutes a section of ciphertext (encrypted text) with a different section that looks like (but is not the same as) the one removed. The substituted section appears to decrypt normally, along with the authentic sections, but results in plaintext (unencrypted text) that serves a particular purpose for the attacker. Essentially, the attacker cuts one or more sections from the ciphertext and reassembles these sections so that the decrypted data will result in coherent but invalid information. Cut-and-paste is a type of message modification attack: the attacker removes a message from network traffic, alters it, and reinserts it. This is called an active attack, because it involves an attempts to change information; in comparison, a passive attack, such as password sniffing, seeks information but does not itself modify the valid information, although it may be used in conjunction with an active form of attack for various purposes.
##### Cipher Block Chaining (CBC) Mode
In the cipher block chaining (CBC) mode of operation, an initialization vector (IV) is XOR-ed with the plaintext prior to encryption. For the first round of encryption, this is a random, public value. For subsequent rounds, it is the ciphertext of the previous round. This is intended to **fix the issue with ECB mode where identical plaintext blocks create identical ciphertext blocks**.

##### Cipher Feedback (CFB) Mode
The cipher feedback (CFB) mode differs from the previous two in that the plaintext never passes through the encryption algorithm at all. Instead an initialization vector (IV) is encrypted and the result is exclusive-ored with the plaintext to create the ciphertext of a block. This is the equivalent of encrypting the plaintext with a one-time pad generating from the encryption of the IV. Similar to CBC mode, this IV is a random value for the first block and the previous block's ciphertext.

##### Counter (CTR) Mode
The counter (CTR) mode of operation differs from the all of the others that we have seen so far. Similar to ECB mode, every encryption operation is completely separate, which is **useful for parallelization of encryption** (since each block can be encrypted simultaneously). Counter mode also uses a non-plaintext output to encryption (like the feedback modes), but, instead of an initialization vector, it uses a combination of a nonce and a counter. The nonce is a random number used for all blocks of an encryption operation and the counter is exactly what it sounds like: a value that starts at zero for block zero and increments to one for block one and so on.
This combination **guarantees that the same values will not pass through the encryption algorithm in the same encryption session** (where every block will have the same nonce but different counter values) or the same blocks in different sessions (where every block will have the same counter value but difference nonces). Similar to the feedback modes of operation (OFB and CFB), the plaintext is exclusive-ored with the output of the encryption operation to produce the ciphertext.

### Task №7
**Confusion** means that each binary digit (bit) of the ciphertext should depend on several parts of the key, obscuring the connections between the two.
*Confusion = Substitution*
Caesar Cipher
a --> b
**Diffusion** means that if we change a single bit of the plaintext, then (statistically) half of the bits in the ciphertext should change, and similarly, if we change one bit of the ciphertext, then approximately one half of the plaintext bits should change. Since a bit can have only two states, when they are all re-evaluated and changed from one seemingly random position to another, half of the bits will have changed state.
*Diffusion = Transposition or Permutation*
DES
abcd --> dacb
#### Difference between them
| № | CONFUSION | DIFFUSION
| ---- | -------- | -------- |
| 1. | Confusion is a cryptographic technique which is used to create faint cipher texts. | While diffusion is used to create cryptic plain texts. |
| 2. | This technique is possible through substitution algorithm. | While it is possible through transportation algorithm. |
| 3. | In confusion, if one bit within the secret’s modified, most or all bits within the cipher text also will be modified. | While in diffusion, if one image within the plain text is modified, many or all image within the cipher text also will be modified |
| 4. | In confusion, vagueness is increased in resultant. | While in diffusion, redundancy is increased in resultant. |
| 5. | Both stream cipher and block cipher uses confusion. | Only block cipher uses diffusion. |
| 6. | The relation between the cipher text and the key is masked by confusion. | While The relation between the cipher text and the plain text is masked by diffusion. |