Cryptography
Day 0 - Digital System Representations
Today’s Schedule
10:30 - 11:00 → Binary, bits, bytes
11:10 - 11:45 → Intro to Crypto
-- Lunch --
1:00-2:00 Intro to Cryptography (cont)
2:00-3:30 One Time Pad (if there is time!)
System
Behavior
OR
Function
Inputs
Outputs
Input
Output
Why Binary?
Now we can pretty much represent anything by:
+
_
+
_
1
0
1
0
Bit & Byte
0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
Bits
Byte
Bitwise Operators
AND (&)
OR (|)
XOR (^)
NOT (~)
1 & 1 = 1
1 & 0 = 0
0 & 1 = 0
0 & 0 = 0
1 | 1 = 1
1 | 0 = 1
0 | 1 = 1
0 | 0 = 0
1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0
~ 1 = 0
~ 0 = 1
Bitwise Operations
AND (&) 0 0 1 1� 1 0 1 0� ------------------------
0 0 1 0
OR (|) 0 0 1 1� 1 0 1 0� ------------------------
1 0 1 1
XOR (^) 0 0 1 1� 1 0 1 0� ------------------------
1 0 0 1
NOT (~) 0 1 � -----------
1 0
What does this byte represent?
0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
Decimal Numbers Review
Decimal Numbers Review
4 4 4, 4 4 4, 4 4 4
* 10 8
* 10 7
* 10 6
* 10 5
* 10 4
* 10 3
* 10 2
* 10 1
* 10 0
Base-10 Decimal System
1 thousands + 2 hundreds + 3 tens + 4 ones
1 * 1000 + 2 * 100 + 3 * 10 + 4 * 1
1 * 103 + 2 * 102 + 3 * 101 + 4 * 100
Base-2 Binary Number System
1
10
11
111
1111
24 23 22 21 20
Base-2 Binary Number System
→ 16 + 4 + 2 → (1 * 16) + (0 * 8) + (1 * 4) + (1 * 2) + (0 * 1)
→ (1 * 24 ) + (0 * 23 ) + (1 * 22 ) + (1 * 21 ) + (0 * 20 )
⇒ 2210 → 1 0 1 1 0 2
Convert from Binary to Decimal
� �1 0 1 1
1 * 23 + 0 * 22 + 1 * 21 + 1 * 20
1 * 8 + 0 * 4 + 1 * 2 + 1 * 1
8 + 0 + 2 + 1
11
Convert from Decimal to Binary
11
__ * 23 + __ * 22 + __* 21 + __ * 20
1 * 23 + 0 * 22 + 1 * 21 + 1 * 20
1 0 1 1
Decimal → Binary
Decimal → Binary
---------------------------------------------------------------------------------�n ÷ 2 quotient remainder num�---------------------------------------------------------------------------------
97 ÷ 2 48 1 1
48 ÷ 2 24 0 01
24 ÷ 2 12 0 001
12 ÷ 2 6 0 0001
6 ÷ 2 3 0 00001
3 ÷ 2 1 1 100001
1 ÷ 2 0 (Stop) 1 1100001
Rules:
Decimal → Binary
------------------------------------------------------------------�n ÷ 2 quotient remainder�------------------------------------------------------------------
97 ÷ 2 48 1
48 ÷ 2 24 0
24 ÷ 2 12 0
12 ÷ 2 6 0
6 ÷ 2 3 0
3 ÷ 2 1 1
1 ÷ 2 0 (Stop) 1
Binary number: 1 1 0 0 0 0 1
Most Significant Bit (MSB)
Least Significant Bit (LSB)
Summary
Binary → Language of Computers
Decimal → Language of Humans
Breakout Rooms:
Convert 73 into binary
Convert 11011010 into decimal
How do we represent “Codebreakers”?
Characters
Sequencing with integers [a→ 0, b→ 1, c→ 2, …., z→ 25]
C: 2, o: 14, d: 3, e: 4, �b: 1, r: 17, e: 4, a: 0, k: 10, e: 4, r: 17, s: 18
ASCII
Character → ASCII
Note that UPPER and lower letters are considered to be different characters
What’s the ASCII representation of
H E L L O ?
72 69 76 76 79 63
ASCII → Character
Note that UPPER and lower letters are considered to be different characters
What does this spell?
72 69 76 76 79 33
H E L L O !
Character <=> Binary?
New Functions! ord() and chr()
ord(<character>)
Takes in a single character input and returns its decimal ASCII code
>>> ord(‘Q’)
81
chr(<integer>)
Reverses ord(), gives you a character for a decimal input
>>> chr(81)
‘Q’