├── .gitignore ├── week1 ├── quiz-caesar-cipher.md ├── modulo-operation-and-caesar-cipher.md ├── assets │ ├── vignere-cipher │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ ├── question-3.PNG │ │ └── question-4.PNG │ ├── caesar-cipher │ │ └── quiz-caesar-cipher.PNG │ └── modulo-operations-caesar-cipher │ │ └── quiz.PNG ├── vignere-cipher.md └── peer-graded-vignere-cipher.md ├── certificate ├── certificate.PNG └── certificate.pdf ├── week3 ├── assets │ ├── des │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ ├── question-3.PNG │ │ └── question-4.PNG │ └── block-cipher │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ └── question-3.PNG ├── ideal-block-cipher.md ├── quiz-des.md └── peer-graded-assignment-des-pseudocode.md ├── week4 ├── assets │ ├── aes │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ ├── question-3.PNG │ │ ├── question-4.PNG │ │ └── question-5.PNG │ └── 3des-4des │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ ├── question-3.PNG │ │ ├── question-4.PNG │ │ ├── question-5.PNG │ │ └── question-6.PNG ├── quiz-aes.md └── quiz-3des-4des.md ├── week2 ├── assets │ ├── rail-fence │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ ├── question-3-4.PNG │ │ ├── question-3-i.PNG │ │ └── question-4-i.PNG │ └── product-cipher │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ └── question-3.PNG ├── quiz-product-cipher.md └── quiz-rail-fence-cipher-permutation-cipher.md ├── week5 ├── assets │ └── block-cipher-operation-modes │ │ ├── question-1.PNG │ │ ├── question-2.PNG │ │ ├── question-3.PNG │ │ ├── question-4.PNG │ │ ├── question-7.PNG │ │ ├── question-8.PNG │ │ └── question-5-6.PNG └── quiz-block-cipher-operation-models.md ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | out 3 | venv 4 | dist 5 | __pycache__ 6 | -------------------------------------------------------------------------------- /week1/quiz-caesar-cipher.md: -------------------------------------------------------------------------------- 1 | ![quiz](assets/caesar-cipher/quiz-caesar-cipher.PNG) 2 | -------------------------------------------------------------------------------- /week1/modulo-operation-and-caesar-cipher.md: -------------------------------------------------------------------------------- 1 | ![quiz](assets/modulo-operations-caesar-cipher/quiz.PNG) 2 | -------------------------------------------------------------------------------- /certificate/certificate.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/certificate/certificate.PNG -------------------------------------------------------------------------------- /certificate/certificate.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/certificate/certificate.pdf -------------------------------------------------------------------------------- /week3/assets/des/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/des/question-1.PNG -------------------------------------------------------------------------------- /week3/assets/des/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/des/question-2.PNG -------------------------------------------------------------------------------- /week3/assets/des/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/des/question-3.PNG -------------------------------------------------------------------------------- /week3/assets/des/question-4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/des/question-4.PNG -------------------------------------------------------------------------------- /week4/assets/aes/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/aes/question-1.PNG -------------------------------------------------------------------------------- /week4/assets/aes/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/aes/question-2.PNG -------------------------------------------------------------------------------- /week4/assets/aes/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/aes/question-3.PNG -------------------------------------------------------------------------------- /week4/assets/aes/question-4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/aes/question-4.PNG -------------------------------------------------------------------------------- /week4/assets/aes/question-5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/aes/question-5.PNG -------------------------------------------------------------------------------- /week2/assets/rail-fence/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/rail-fence/question-1.PNG -------------------------------------------------------------------------------- /week2/assets/rail-fence/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/rail-fence/question-2.PNG -------------------------------------------------------------------------------- /week4/assets/3des-4des/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/3des-4des/question-1.PNG -------------------------------------------------------------------------------- /week4/assets/3des-4des/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/3des-4des/question-2.PNG -------------------------------------------------------------------------------- /week4/assets/3des-4des/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/3des-4des/question-3.PNG -------------------------------------------------------------------------------- /week4/assets/3des-4des/question-4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/3des-4des/question-4.PNG -------------------------------------------------------------------------------- /week4/assets/3des-4des/question-5.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/3des-4des/question-5.PNG -------------------------------------------------------------------------------- /week4/assets/3des-4des/question-6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week4/assets/3des-4des/question-6.PNG -------------------------------------------------------------------------------- /week2/assets/rail-fence/question-3-4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/rail-fence/question-3-4.PNG -------------------------------------------------------------------------------- /week2/assets/rail-fence/question-3-i.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/rail-fence/question-3-i.PNG -------------------------------------------------------------------------------- /week2/assets/rail-fence/question-4-i.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/rail-fence/question-4-i.PNG -------------------------------------------------------------------------------- /week3/assets/block-cipher/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/block-cipher/question-1.PNG -------------------------------------------------------------------------------- /week3/assets/block-cipher/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/block-cipher/question-2.PNG -------------------------------------------------------------------------------- /week3/assets/block-cipher/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week3/assets/block-cipher/question-3.PNG -------------------------------------------------------------------------------- /week1/assets/vignere-cipher/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week1/assets/vignere-cipher/question-1.PNG -------------------------------------------------------------------------------- /week1/assets/vignere-cipher/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week1/assets/vignere-cipher/question-2.PNG -------------------------------------------------------------------------------- /week1/assets/vignere-cipher/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week1/assets/vignere-cipher/question-3.PNG -------------------------------------------------------------------------------- /week1/assets/vignere-cipher/question-4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week1/assets/vignere-cipher/question-4.PNG -------------------------------------------------------------------------------- /week2/assets/product-cipher/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/product-cipher/question-1.PNG -------------------------------------------------------------------------------- /week2/assets/product-cipher/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/product-cipher/question-2.PNG -------------------------------------------------------------------------------- /week2/assets/product-cipher/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week2/assets/product-cipher/question-3.PNG -------------------------------------------------------------------------------- /week1/assets/caesar-cipher/quiz-caesar-cipher.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week1/assets/caesar-cipher/quiz-caesar-cipher.PNG -------------------------------------------------------------------------------- /week1/assets/modulo-operations-caesar-cipher/quiz.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week1/assets/modulo-operations-caesar-cipher/quiz.PNG -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-1.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-1.PNG -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-2.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-2.PNG -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-3.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-3.PNG -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-4.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-4.PNG -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-7.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-7.PNG -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-8.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-8.PNG -------------------------------------------------------------------------------- /week3/ideal-block-cipher.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/block-cipher/question-1.PNG) 2 | ![question-2](assets/block-cipher/question-2.PNG) 3 | ![question-3](assets/block-cipher/question-3.PNG) 4 | -------------------------------------------------------------------------------- /week5/assets/block-cipher-operation-modes/question-5-6.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/anishLearnsToCode/symmetric-cryptography/HEAD/week5/assets/block-cipher-operation-modes/question-5-6.PNG -------------------------------------------------------------------------------- /week2/quiz-product-cipher.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/product-cipher/question-1.PNG) 2 | ![question-1](assets/product-cipher/question-2.PNG) 3 | ![question-1](assets/product-cipher/question-3.PNG) 4 | -------------------------------------------------------------------------------- /week3/quiz-des.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/des/question-1.PNG) 2 | ![question-2](assets/des/question-2.PNG) 3 | ![question-3](assets/des/question-3.PNG) 4 | ![question-4](assets/des/question-4.PNG) 5 | -------------------------------------------------------------------------------- /week2/quiz-rail-fence-cipher-permutation-cipher.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/rail-fence/question-1.PNG) 2 | ![question-2](assets/rail-fence/question-2.PNG) 3 | ![question-3-4](assets/rail-fence/question-3-4.PNG) 4 | -------------------------------------------------------------------------------- /week4/quiz-aes.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/aes/question-1.PNG) 2 | ![question-2](assets/aes/question-2.PNG) 3 | ![question-3](assets/aes/question-3.PNG) 4 | ![question-4](assets/aes/question-4.PNG) 5 | ![question-5](assets/aes/question-5.PNG) 6 | -------------------------------------------------------------------------------- /week1/vignere-cipher.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/vignere-cipher/question-1.PNG) 2 | ![question-2](assets/vignere-cipher/question-2.PNG) 3 | ![question-3](assets/vignere-cipher/question-3.PNG) 4 | ![question-4](assets/vignere-cipher/question-4.PNG) 5 | -------------------------------------------------------------------------------- /week4/quiz-3des-4des.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/3des-4des/question-1.PNG) 2 | ![question-1](assets/3des-4des/question-2.PNG) 3 | ![question-1](assets/3des-4des/question-3.PNG) 4 | ![question-1](assets/3des-4des/question-4.PNG) 5 | ![question-1](assets/3des-4des/question-5.PNG) 6 | ![question-1](assets/3des-4des/question-6.PNG) 7 | -------------------------------------------------------------------------------- /week5/quiz-block-cipher-operation-models.md: -------------------------------------------------------------------------------- 1 | ![question-1](assets/block-cipher-operation-modes/question-1.PNG) 2 | ![question-2](assets/block-cipher-operation-modes/question-2.PNG) 3 | ![question-2](assets/block-cipher-operation-modes/question-3.PNG) 4 | ![question-2](assets/block-cipher-operation-modes/question-4.PNG) 5 | ![question-2](assets/block-cipher-operation-modes/question-5-6.PNG) 6 | ![question-2](assets/block-cipher-operation-modes/question-7.PNG) 7 | ![question-2](assets/block-cipher-operation-modes/question-8.PNG) 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 anishLearnsToCode 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 4 | documentation files (the "Software"), to deal in the Software without restriction, including without limitation the 5 | rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit 6 | persons to whom the Software is furnished to do so, subject to the following conditions: 7 | 8 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the 9 | Software. 10 | 11 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE 12 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 13 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 14 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Symmetric Cryptography 2 | 3 | [![specialization](https://img.shields.io/badge/specialization-Introdution%20to%20Applied%20Cryptography-1f72ff.svg)](https://github.com/anishLearnsToCode/intro-to-applied-cryptography) 4 | [![course-list](https://img.shields.io/badge/also%20see-Coursera%20Courses-1f72ff.svg)](https://github.com/anishLearnsToCode/course-list) 5 | 6 | This is part of the 4 course specialization 7 | [Introduction to Applied Cryptography](https://github.com/anishLearnsToCode/intro-to-applied-cryptography) 8 | by the University of Colorado. This is the third course in this specialization. 9 | 10 | ## Overview 11 | - [Week 1](#week-1) 12 | - [Week 2](#week-2) 13 | - [Week 3](#week-3) 14 | - [Week 4](#week-4) 15 | - [Week 5](#week-5) 16 | - [Certificate](#-certificate) 17 | 18 | ## Week 1 19 | - [Quiz: Caesar Cipher](week1/quiz-caesar-cipher.md) 20 | - [Quiz: Modulo Operation and Caesar Cipher](week1/modulo-operation-and-caesar-cipher.md) 21 | - [Quiz: Vigenere Cipher](week1/vignere-cipher.md) 22 | - [Honors Peer Graded Assignment: Pseudo-Code Vignere Cipher](week1/peer-graded-vignere-cipher.md) 23 | 24 | ## Week 2 25 | - [Quiz: Rail Fence Cipher and Permutation Cipher](week2/quiz-rail-fence-cipher-permutation-cipher.md) 26 | - [Quiz: Product Cipher](week2/quiz-product-cipher.md) 27 | 28 | ## Week 3 29 | - [Quiz: Ideal Block Cipher](week3/ideal-block-cipher.md) 30 | - [Peer Graded Assignment: DES Overview Pseudocode](week3/peer-graded-assignment-des-pseudocode.md) 31 | - [Quiz: DES](week3/quiz-des.md) 32 | 33 | ## Week 4 34 | - [Quiz: 3-DES and 4-DES](week4/quiz-3des-4des.md) 35 | - [Quiz: AES](week4/quiz-aes.md) 36 | 37 | ## Week 5 38 | - [Quiz: Block Cipher Operation Modes](week5/quiz-block-cipher-operation-models.md) 39 | 40 | ## [🎓 Certificate](http://coursera.org/verify/KPJPY9LZAFBP) 41 | ![certificate](certificate/certificate.PNG) 42 | -------------------------------------------------------------------------------- /week1/peer-graded-vignere-cipher.md: -------------------------------------------------------------------------------- 1 | # Honors Peer Graded Assignment: Pseudo-Code Vignere Cipher 2 | 3 | ### Which programming language, if any, are you familiar with? 4 | 5 | I am familiar with Java and Python. 6 | 7 | ### Write your answer for the pseudo-code for the Vigenere Cipher encryption. 8 | 9 | We take the plaintext as p and ciphertext as c which we produce from plaintext during encryption and 10 | we produce the plaintext back from the ciphertext using the decryption steps. We encrypt the plaintext 11 | p of arbitrary length using key stream generated from key k of arbitrary length. 12 | 13 | such that c_i = (p_i + k_i) mod 26 where k_i = key_{i mod m} where m is the length of the key. The 14 | pseudo-code is represented as: 15 | 16 | ```text 17 | m <-- length of key 18 | for index, character in plaintext: 19 | ciphertext[index] <-- (character + key[index % m]) % 26 20 | 21 | return ciphertext 22 | ``` 23 | 24 | This encryption algorithm can be represented in Python as 25 | 26 | ```python 27 | def encrypt(plaintext: str, key: str) -> str: 28 | ciphertext = '' 29 | m = len(key) 30 | for i, letter in enumerate(plaintext.lower()): 31 | ciphertext += chr((char_2_num(letter) + char_2_num(key[i % m])) % 26 + ord('a')) 32 | return ciphertext.upper() 33 | 34 | def char_2_num(character: str) -> int: 35 | return ord(character.lower()) - ord('a') 36 | ``` 37 | 38 | where `char_2_num` is a utility function. Similarly we decrypt the ciphertext using the key by the 39 | following pseudo-code: 40 | 41 | ```text 42 | m = length of key 43 | for index, character in ciphertext: 44 | plaintext[i] <-- (character - key[index % m]) % 26 45 | 46 | return plaintext 47 | ``` 48 | 49 | The algorithm in Python is represented as: 50 | 51 | ```python 52 | def decrypt(ciphertext: str, key: str) -> str: 53 | plaintext = '' 54 | m = len(key) 55 | for index, letter in enumerate(ciphertext.lower()): 56 | plaintext += chr( 57 | (char_2_num(letter) - char_2_num(key[index % m])) % 26 + ord('a')) 58 | return plaintext.lower() 59 | ``` 60 | -------------------------------------------------------------------------------- /week3/peer-graded-assignment-des-pseudocode.md: -------------------------------------------------------------------------------- 1 | # Peer Graded Assignment: DES Pseudo-Code 2 | 3 | ### Which programming language, if any, are you familiar with? 4 | 5 | I am familiar with Java and Python. 6 | 7 | ### Write your answer for the pseudo-code for the DES encryption. 8 | 9 | The DES Algorithm requires many smaller structures such as the Substitution Box, Permutation Box, 10 | The Mixer and Swapper which Compose a Single Round of Feistel Cipher etc. We describe all structures 11 | below: 12 | 13 | #### Permutation Box (PBOX) 14 | 15 | ```text 16 | PBox: 17 | key = [5 4 78 45 .... 64] // an n length key for permuting a given sequence 18 | 19 | function permute(sequence): 20 | result <-- new sequence 21 | for index, element in sequence: 22 | result[key[index]] = element 23 | endfor 24 | return result 25 | endfunction 26 | ``` 27 | 28 | #### Substitution Box (SBox) 29 | In the substitution box we will have a table that will provide us with mappings from 2 dimensional 30 | bit tuples to a 4-bit binary sequence and we obtain the 2 dimensional tuple from a given binary 31 | sequence. 32 | 33 | We compose our `SBox` with a `func` which is a function which returns `(row, column)` tuple from 34 | binary input. 35 | 36 | ```text 37 | SBox: 38 | table <-- provided by user 39 | func <-- function for getting (row, column) from binary input 40 | 41 | function substitute(binary) 42 | row, column = func(binary) 43 | return table[row][column] 44 | endfunction 45 | ``` 46 | 47 | #### Swapper 48 | Swapper is a simple Cipher part of a single round of the DES Cipher except the last round. The last round 49 | in the DES cipher contains no swapper component. The swapper imply takes in a binary sequence and 50 | splits it into 2 and then swaps both components to return a new binary sequence. 51 | 52 | ```text 53 | Swapper: 54 | function encrypt(sequence): 55 | L <-- left component of sequence 56 | R <-- right component of sequence 57 | result <-- R + L 58 | return result 59 | endfunction 60 | 61 | function decrypt(sequence): 62 | // performs the same swapping operation as the encryption part 63 | return encrypt(sequence) 64 | endfunction 65 | ``` 66 | 67 | #### Mixer 68 | This is the most important component of the single round in the DES algorithm. In the mixer we obtain a 69 | 64 bit binary sequence. We divide it into 2 parts of 32 bit each. The right part is then expanded 70 | using an expansion Permutation Box (E-PBox) and a resulting 48 bit sequence is created. 71 | 72 | The resulting 48 bit sequence is used with the key with a non-reversible mathematical 73 | function, in this case the mod function to obtain a new 48 bit sequence. 74 | 75 | 8 Substitution boxes are applied to the 48 bit sequence to reduce it down to 32 bit. Each SBox 76 | reduces 6 bit --> 4 bit. Another permutation is applied to the right block. 77 | 78 | Finally a XOR (^) operation is performed between left and right parts and the result is stored in 79 | the new left part (32 bits). The combination of these pieces are then returned as 64 bit binary 80 | sequence which then go to the Swapper. 81 | 82 | ```text 83 | Mixer: 84 | function encrypt(sequence): 85 | L <-- left part of sequence (32 bit) 86 | R <-- right part of sequence (32 bit) 87 | 88 | R1 <-- apply expansion PBox on R 89 | 90 | // apply non-invertible function 91 | R2 <-- apply non-invertible modular function on R1 using key K_i 92 | 93 | // apply substitution boxes on R2 (48 bit --> 32 bit) 94 | R3 <-- apply 8 substitution boxes on R2 95 | 96 | // apply XOR (^) on left and right 97 | L1 <-- L ^ R3 98 | 99 | // return combined result 100 | return L1 + R3 101 | endfunction 102 | 103 | // same steps as encryption 104 | function decrypt(sequence): 105 | return encrypt(sequence) 106 | endfunction 107 | ``` 108 | 109 | #### Round 110 | We now define a single round of the DES Cipher which is in fact a single round of the Feistel 111 | Cipher. 112 | 113 | ```text 114 | Round: 115 | mixer <-- the mixer component with key k_i 116 | swapper <-- The swapper component part of a single round 117 | 118 | function encrypt(sequence) 119 | ciphertext = mixer.encrypt(sequence) 120 | ciphertext = swapper.encrypt(ciphertext) 121 | return ciphertext 122 | endfunction 123 | 124 | function decrypt(sequence) 125 | plaintext = swapper.decrypt(sequence) 126 | plaintext = mixer.decrypt(plaintext) 127 | return plaintext 128 | endfunction 129 | ``` 130 | 131 | We now combine 16 such rounds to form the DES Ciphers where the last round has no Swapper component 132 | and the first 15 rounds have Swappers + Mixers. Also we have a key generator which yields 16 133 | different keys for each round. 134 | 135 | ```text 136 | DES: 137 | rounds <-- generate 15 rounds with 15 different keys using the key generator 138 | rounds <-- add a 16th round with just the Mixer component 139 | 140 | function encrypt(sequence): 141 | ciphertext <-- initial permutation on sequence 142 | for round in rounds [i = 1... 16] 143 | ciphertext = round.encrypt(ciphertext) 144 | endfor 145 | return final permutation on ciphertext 146 | endfunction 147 | 148 | 149 | function decrypt(sequence): 150 | plaintext <-- final permutation inverse on sequence 151 | for round in rounds [i = 16... 1] // going in reverse 152 | plaintext = round.decrypt(plaintext) 153 | endfor 154 | return initial permutation inverse on plaintext 155 | endfunction 156 | ``` 157 | --------------------------------------------------------------------------------